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
    • User Invite
    • 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
    • User Invite
    • 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
  • React SDK

React SDK

Installation

npm install @melody-auth/react --save

AuthProvider

Wrap your application inside AuthProvider component to provide the auth related context to your application components.

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 { AuthProvider } from '@melody-auth/react'

export default function RootLayout ({ children }: {
  children: React.ReactNode;
}) {
  return (
    <html lang='en'>
      <AuthProvider
        clientId={process.env.CLIENT_ID ?? ''}
        redirectUri={process.env.REDIRECT_URI ?? ''}
        serverUri={process.env.SERVER_URI ?? ''}
      >
        <body>
          {children}
        </body>
      </AuthProvider>
    </html>
  )
}

isAuthenticated

Indicates if the current user is authenticated.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isAuthenticating, isAuthenticated } = useAuth()

  if (isAuthenticating) return <Spinner />
  if (isAuthenticated) return <Button>Logout</Button>
  if (!isAuthenticated) return <Button>Login</Button>
}

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 { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isAuthenticated, loginRedirect } = useAuth()

  const handleLogin = () => {
    loginRedirect({
      locale: 'en',
      state: JSON.stringify({ info: Math.random() })
    })
  }

  if (!isAuthenticated) {
    return <Button onClick={handleLogin}>Login</Button>
  }
}

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 { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isAuthenticated, loginPopup } = useAuth()

  const handleLogin = () => {
    loginPopup({
      locale: 'en',
      state: JSON.stringify({ info: Math.random() })
    })
  }

  if (!isAuthenticated) {
    return <Button onClick={handleLogin}>Login</Button>
  }
}

logoutRedirect

Triggers the logout flow.

ParameterTypeDescriptionDefaultRequired
postLogoutRedirectUristringThe URL to redirect users after logoutN/ANo

Note: postLogoutRedirectUri must match one of the app's registered redirect URIs (configured in the admin panel). If it does not match, the user is still logged out, but the browser is redirected to the app's first registered redirect URI as a safe fallback.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isAuthenticated, logoutRedirect } = useAuth()

  const handleLogout = () => {
    logoutRedirect({ postLogoutRedirectUri: 'http://localhost:3000/' })
  }

  if (isAuthenticated) {
    return <Button onClick={handleLogout}>Logout</Button>
  }
}

acquireToken

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

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isAuthenticated, acquireToken } = useAuth()

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

  if (isAuthenticated) {
    return <Button onClick={handleFetchUserInfo}>Fetch User Info</Button>
  }
}

acquireUserInfo

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

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isAuthenticated, acquireUserInfo } = useAuth()

  const handleFetchUserInfo = () => {
    const userInfo = await acquireUserInfo()
  }

  if (isAuthenticated) {
    return <Button onClick={handleFetchUserInfo}>Fetch User Info</Button>
  }
}

userInfo

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

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { userInfo } = useAuth()

  <div>{JSON.stringify(userInfo)}</div>
}

isAuthenticating

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

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isAuthenticating } = useAuth()

  if (isAuthenticating) return <Spinner />
}

isLoadingToken

Indicates whether the SDK is acquiring token.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isLoadingToken } = useAuth()

  if (isLoadingToken) return <Spinner />
}

isLoadingUserInfo

Indicates whether the SDK is acquiring user info.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { isLoadingUserInfo } = useAuth()

  if (isLoadingUserInfo) return <Spinner />
}

authenticationError

Indicates whether there is an authentication process related error.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { authenticationError } = useAuth()

  if (authenticationError) return <Alert />
}

acquireTokenError

Indicates whether there is an acquireToken process related error.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { acquireTokenError } = useAuth()

  if (acquireTokenError) return <Alert />
}

acquireUserInfoError

Indicates whether there is an acquireUserInfo process related error.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { acquireUserInfoError } = useAuth()

  if (acquireUserInfoError) return <Alert />
}

idToken

The id_token of the current user.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { idToken } = useAuth()
}

account

Decoded account information from id_token.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { account } = useAuth()
}

loginError

Indicates whether there is an login process related error.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { loginError } = useAuth()

  if (loginError) return <Alert />
}

logoutError

Indicates whether there is an login process related error.

import { useAuth } from '@melody-auth/react'

export default function Home () {
  const { logoutError } = useAuth()

  if (logoutError) return <Alert />
}

Example app

See the minimal React example using the React SDK: https://github.com/ValueMelody/melody-auth-examples/tree/main/vite-react-demo.

Last Updated: 4/22/26, 9:27 PM
Contributors: Baozier