Melody AuthMelody Auth
  • Auth Server Setup
  • Admin Panel Setup
  • Email Provider Setup
  • SMS Provider Setup
  • External Identity Providers

    • Social Sign-In Provider Setup
    • OIDC SSO Setup
    • SAML SSO Setup
  • Main Features

    • Authentication
    • JWT & JWKS
    • Multi-Factor Authentication
    • Role-Based Access Control
    • Policies
    • Organizations
  • Additional Features

    • User Attributes
    • App Banners
    • Organization Groups
    • Impersonation
    • Log Management
  • Customization

    • Auth Server Configuration
    • Branding
    • Localization
  • Frontend SDKs

    • React SDK
    • Angular SDK
    • Vue SDK
    • Web SDK
  • Backend APIs

    • S2S API Setup
    • S2S API Swagger
    • Embedded Auth API Setup
    • Embedded Auth API Swagger
  • Deployment Pipelines
  • Rotate JWT Secret
  • English
  • zh-CN
  • Auth Server Setup
  • Admin Panel Setup
  • Email Provider Setup
  • SMS Provider Setup
  • External Identity Providers

    • Social Sign-In Provider Setup
    • OIDC SSO Setup
    • SAML SSO Setup
  • Main Features

    • Authentication
    • JWT & JWKS
    • Multi-Factor Authentication
    • Role-Based Access Control
    • Policies
    • Organizations
  • Additional Features

    • User Attributes
    • App Banners
    • Organization Groups
    • Impersonation
    • Log Management
  • Customization

    • Auth Server Configuration
    • Branding
    • Localization
  • Frontend SDKs

    • React SDK
    • Angular SDK
    • Vue SDK
    • Web SDK
  • Backend APIs

    • S2S API Setup
    • S2S API Swagger
    • Embedded Auth API Setup
    • Embedded Auth API Swagger
  • Deployment Pipelines
  • Rotate JWT Secret
  • English
  • zh-CN
  • Web SDK

Web SDK

The Web SDK is a library that allows you to easily integrate Melody Auth into your web application that uses vanilla JavaScript.

Installation

npm install @melody-auth/web --save

Configuration

The Web SDK functions accept a shared configuration object.

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
import {
  triggerLogin,
  loadCodeAndStateFromUrl,
  exchangeTokenByAuthCode,
  exchangeTokenByRefreshToken,
  getUserInfo,
  logout,
} from '@melody-auth/web'

const config = {
  clientId: '<CLIENT_ID>',
  redirectUri: '<CLIENT_REDIRECT_URI>',
  serverUri: '<AUTH_SERVER_URI>',
  // Optional
  scopes: ['openid', 'profile'],
  storage: 'localStorage',
}

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
await triggerLogin('redirect', config, {
  locale: 'en',
  // state: 'your-predictable-state',
  // policy: 'sign_in_or_sign_up',
  // org: 'your-org-slug',
})

loginPopup

Triggers a new authentication flow in a popup window. When the user completes authentication, your authorizePopupHandler is invoked with { state, code }. You must exchange the code for tokens.

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
authorizePopupHandler(data: { state: string; code: string }) => voidHandler called when the popup posts back auth codeN/ANo
await triggerLogin('popup', config, {
  locale: 'en',
  authorizePopupHandler: async ({ state, code }) => {
    await exchangeTokenByAuthCode(code, state, config)
    // tokens have now been processed (see notes below)
  },
})

handleRedirectCallback

When the user returns from the auth server via redirect, read the code and state from the URL and exchange them for tokens.

const { code, state } = loadCodeAndStateFromUrl()
await exchangeTokenByAuthCode(code, state, config)

What gets stored

After a successful exchangeTokenByAuthCode call:

  • The refresh token and id token are persisted using the configured storage under StorageKey.RefreshToken and StorageKey.IdToken.
  • The access token is returned from the exchange function but is not persisted. Use it immediately, or reacquire later via refresh token.

acquireToken

Gets a fresh access token using the stored refresh token when needed.

import { getStorage, StorageKey } from '@melody-auth/shared'

const storage = getStorage(config.storage)
const refreshTokenRaw = storage.getItem(StorageKey.RefreshToken)
const refreshToken = refreshTokenRaw && JSON.parse(refreshTokenRaw).refreshToken

if (!refreshToken) throw new Error('No refresh token found')

const { accessToken, expiresIn, expiresOn } = await exchangeTokenByRefreshToken(
  config,
  refreshToken,
)

acquireUserInfo

Gets the user's public info from the auth server using a valid access token.

const userInfo = await getUserInfo(config, { accessToken })

logoutRedirect

Logs the user out. When localOnly is false and a refresh token is present, a remote logout request is sent first to obtain the post-logout redirect. Local tokens are cleared and the browser is redirected.

ParameterTypeDescription
postLogoutRedirectUristringThe URL to redirect users after logout
localOnlybooleanIf true, only clears local tokens and redirects without remote logout
import { getStorage, StorageKey } from '@melody-auth/shared'

const storage = getStorage(config.storage)
const idTokenRaw = storage.getItem(StorageKey.IdToken)
const refreshTokenRaw = storage.getItem(StorageKey.RefreshToken)
const accessToken = /* obtain via exchangeTokenByRefreshToken(...) */
const refreshToken = refreshTokenRaw && JSON.parse(refreshTokenRaw).refreshToken

await logout(
  config,
  accessToken ?? '',
  refreshToken ?? null,
  'http://localhost:3000/',
  false, // set to true to skip remote logout
)

Example app

See the minimal Vite example using the Web SDK: https://github.com/ValueMelody/melody-auth-examples/tree/main/vite-web-example.

Last Updated: 8/10/25, 1:30 AM
Contributors: Baozier