Melody AuthMelody Auth
  • Angular SDK

Angular SDK

Installation

npm install @melody-auth/angular --save

AuthProvider

Provide the auth related configs to your application bootstrap.

ParameterTypeDescriptionDefaultRequired
clientIdstringThe auth clientId your frontend connects toN/AYes
redirectUristringThe URL to redirect users after successful authenticationN/AYes
serverUristringThe URL where you host the melody auth serverN/AYes
scopesstring[]Permission scopes to request for user accessN/ANo
storage'sessionStorage' | 'localStorage'Storage type for authentication tokens'localStorage'No
onLoginSuccess(attr: { locale?: string; state?: string }) => voidFunction to execute after a successful loginN/ANo
import { bootstrapApplication } from '@angular/platform-browser'
import { provideAuth } from '@melody-auth/angular'
import { appConfig } from './app/app.config'
import { AppComponent } from './app/app.component'

bootstrapApplication(
  AppComponent,
  {
    providers: [
      provideAuth({
        clientId: environment.clientId,
        redirectUri: environment.redirectUri,
        serverUri: environment.serverUri,
        storage: 'localStorage',
      }),
      ...appConfig.providers,
    ],
  },
)

isAuthenticated

Indicates if the current user is authenticated.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <p>Is Authenticated: {{ authService.isAuthenticated }}</p>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

loginRedirect

Triggers a new authentication flow by redirecting to the auth server.

ParameterTypeDescriptionDefaultRequired
localestringSpecifies the locale to use in the authentication flowN/ANo
statestringSpecifies the state to use in the authentication flow if you prefer not to use a randomly generated stringN/ANo
policystringSpecifies the policy to use in the authentication flow'sign_in_or_sign_up'No
orgstringSpecifies the organization to use in the authentication flow, the value should be the slug of the organizationN/ANo
import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <button (click)="onLoginRedirect()">Login</button>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}

  onLoginRedirect () {
    this.authService.loginRedirect()
  }
}

loginPopup

Triggers a new authentication flow in a popup.

ParameterTypeDescriptionDefaultRequired
localestringSpecifies the locale to use in the authentication flowN/ANo
statestringSpecifies the state to use in the authentication flow if you prefer not to use a randomly generated stringN/ANo
orgstringSpecifies the organization to use in the authentication flow, the value should be the slug of the organizationN/ANo
import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <button (click)="onLoginPopup()">Login</button>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}

  onLoginPopup () {
    this.authService.loginPopup()
  }
}

logoutRedirect

Triggers the logout flow.

ParameterTypeDescriptionDefaultRequired
postLogoutRedirectUristringThe URL to redirect users after logoutN/ANo
import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <button (click)="onLogout()">Logout</button>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}

  onLogout () {
    this.authService.logoutRedirect({ postLogoutRedirectUri: 'http://localhost:3000' })
  }
}

acquireToken

Gets the user's accessToken, or refreshes it if expired.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <button (click)="fetchResource()">Acquire Token</button>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}

  async fetchResource () {
    const accessToken = await this.authService.acquireToken()
    // Use the token to fetch protected resources
    await fetch('/...', {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    })
  }
}

acquireUserInfo

Gets the user's public info from the auth server.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <button (click)="fetchUserInfo()">Fetch User Info</button>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}

  async fetchUserInfo () {
    const userInfo = await this.authService.acquireUserInfo()
  }
}

userInfo

The current user's details. Be sure to invoke acquireUserInfo before accessing userInfo.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.userInfo | json }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

isAuthenticating

Indicates whether the SDK is initializing and attempting to obtain the user's authentication state.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.isAuthenticating }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

isLoadingToken

Indicates whether the SDK is acquiring token.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.isLoadingToken }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

isLoadingUserInfo

Indicates whether the SDK is acquiring user info.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.isLoadingUserInfo }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

authenticationError

Indicates whether there is an authentication process related error.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.authenticationError }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

acquireTokenError

Indicates whether there is an acquireToken process related error.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.acquireTokenError }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

acquireUserInfoError

Indicates whether there is an acquireUserInfo process related error.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.acquireUserInfoError }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

idToken

The id_token of the current user.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.idToken }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

account

Decoded account information from id_token.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.account }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

loginError

Indicates whether there is an login process related error.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.loginError }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}

logoutError

Indicates whether there is an login process related error.

import { CommonModule } from '@angular/common'
import { Component } from '@angular/core'
import { AuthService } from '@melody-auth/angular'

@Component({
  selector: 'auth-component',
  template: `
    <div>{{ authService.logoutError }}</div>
  `,
  imports: [CommonModule],
})

export class AuthActionsComponent {
  constructor (private authService: AuthService) {}
}
Last Updated:: 4/29/25, 3:44 AM
Contributors: Baozier