Melody AuthMelody Auth
  • Vue SDK

Vue SDK

Installation

npm install @melody-auth/vue --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 { createApp } from 'vue'
import { AuthProvider } from '@melody-auth/vue'
import App from './App.vue'

const app = createApp(App)

app.use(
  AuthProvider,
  {
    clientId: import.meta.env.VITE_AUTH_SPA_CLIENT_ID,
    redirectUri: import.meta.env.VITE_CLIENT_URI,
    serverUri: import.meta.env.VITE_AUTH_SERVER_URI,
    storage: 'localStorage',
  },
)

app.mount('#app')

isAuthenticated

Indicates if the current user is authenticated.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { isAuthenticated } = useAuth()
</script>

<template>
  <div v-if="isAuthenticating">
    <Spinner />
  </div>
  <div v-else>
    <div v-if="isAuthenticated">
      <button
        Logout
      </button>
    </div>
    <div v-else>
      <button>
        Login
      </button>
    </div>
  </div>
</template>

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 flowN/ANo
<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { loginRedirect } = useAuth()

function handleLogin() {
  loginRedirect({ locale: 'en' })
}
</script>

<template>
  <button @click="handleLogin">
    Login with Redirect
  </button>
</template>

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 flowN/ANo
<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { loginPopup } = useAuth()

function handleLogin() {
  loginPopup({ locale: 'en' })
}
</script>

<template>
  <button @click="handleLogin">
    Login with Popup
  </button>
</template>

logoutRedirect

Triggers the logout flow.

ParameterTypeDescriptionDefaultRequired
postLogoutRedirectUristringThe URL to redirect users after logoutN/ANo
<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { logoutRedirect } = useAuth()

function handleLogout() {
  logoutRedirect({ postLogoutRedirectUri: 'http://localhost:3000/' })
}
</script>

<template>
  <button @click="handleLogout">
    Logout
  </button>
</template>

acquireToken

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

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { acquireToken, isAuthenticated } = useAuth()

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

<template>
  <button v-if="isAuthenticated" @click="fetchUserInfo">
    Fetch User Info
  </button>
</template>

acquireUserInfo

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

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { acquireUserInfo, isAuthenticated } = useAuth()

async function fetchPublicInfo() {
  const userInfo = await acquireUserInfo()
}
</script>

<template>
  <button v-if="isAuthenticated" @click="fetchPublicInfo">
    Fetch Public User Info
  </button>
</template>

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.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { isAuthenticating } = useAuth()
</script>

<template>
  <div v-if="isAuthenticating">
    <Spinner />
  </div>
</template>

isLoadingToken

Indicates whether the SDK is acquiring the token.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { isLoadingToken } = useAuth()
</script>

<template>
  <div v-if="isLoadingToken">
    <Spinner />
  </div>
</template>

isLoadingUserInfo

Indicates whether the SDK is acquiring the user's information.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { isLoadingUserInfo } = useAuth()
</script>

<template>
  <div v-if="isLoadingUserInfo">
    <Spinner />
  </div>
</template>

authenticationError

Indicates whether there is an error during authentication.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { authenticationError } = useAuth()
</script>

<template>
  <div v-if="authenticationError">
    <Alert message="Authentication error" />
  </div>
</template>

acquireTokenError

Indicates whether there is an error during token acquisition.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { acquireTokenError } = useAuth()
</script>

<template>
  <div v-if="acquireTokenError">
    <Alert message="Error acquiring token" />
  </div>
</template>

acquireUserInfoError

Indicates whether there is an error while acquiring user information.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { acquireUserInfoError } = useAuth()
</script>

<template>
  <div v-if="acquireUserInfoError">
    <Alert message="Error acquiring user info" />
  </div>
</template>

idToken

The id_token of the current user.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { idToken } = useAuth()
</script>

<template>
  <div>
    <pre>{{ idToken }}</pre>
  </div>
</template>

account

Decoded account information from the id_token.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { account } = useAuth()
</script>

<template>
  <div>
    <pre>{{ account }}</pre>
  </div>
</template>

loginError

Indicates whether there is a login process related error.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { loginError } = useAuth()
</script>

<template>
  <div v-if="loginError">
    <Alert message="Login error" />
  </div>
</template>

logoutError

Indicates whether there is a logout process related error.

<script setup lang="ts">
import { useAuth } from '@melody-auth/vue'
const { logoutError } = useAuth()
</script>

<template>
  <div v-if="logoutError">
    <Alert message="Logout error" />
  </div>
</template>
Last Updated:: 4/29/25, 3:44 AM
Contributors: Baozier