mobile favicon

Authentication

Authenticate your user thanks to the PKCE Oauth protocol that enables secure exchange of refresh and access tokens between an application and the authorization server

With the default options, the module requires a log-in page and a confirm page to handle the PKCE authorization code flow. If you want to understand how it works under the hood, you can read this section.

All you need to do is to create a login.vue and confirm.vue page in the pages folder.

For advanced users who want to implement the auth behaviour themselves, you can disable or override the redirect options.

Log-in page - /login

Each time a user is trying to access a page that needs authentication, he will automatically be redirected to the configured log in page. If you want to allow access to "public" page, you just need to add them in the exclude redirect option. Alternatively, you can enable the redirect only for certain routes using the include redirect option.

Ensure to activate the authentication providers you want in the Supabase Dashboard under Authentication -> Providers.

The log-in page initiates the log-in method(s) you choose from the available authorization methods provided by Supabase, it could looks like:

pages/login.vue
<script setup lang="ts">
const supabase = useSupabaseClient()
const email = ref('')

const signInWithOtp = async () => {
  const { error } = await supabase.auth.signInWithOtp({
    email: email.value,
    options: {
      emailRedirectTo: 'http://localhost:3000/confirm',
    }
  })
  if (error) console.log(error)
}
</script>
<template>
  <div>
    <button @click="signInWithOtp">
      Sign In with E-Mail
    </button>
    <input
      v-model="email"
      type="email"
    />
  </div>
</template>

Once the authorization flow is triggered using the auth wrapper of the useSupabaseClient composable, the session management is handled automatically and the user will be redirected to the page you specify in the redirect option (/confirm by default).

Confirm page - /confirm

The confirmation page receives the supabase callback which contains session information. The supabase client automatically detects and handles this, and once the session is confirmed the user value will automatically be updated. From there you can redirect to the appropriate page.

The redirect URL must be configured in your Supabase dashboard under Authentication -> URL Configuration -> Redirect URLs.
pages/confirm.vue
<script setup lang="ts">
const user = useSupabaseUser()

watch(user, () => {
  if (user.value) {
      // Redirect to protected page
      return navigateTo('/')
  }
}, { immediate: true })
</script>

<template>
  <div>Waiting for login...</div>
</template>

Redirect path

You can easily handle redirection to the initial requested route after login using the useSupabaseCookieRedirect composable and the saveRedirectToCookie option.

By setting the saveRedirectToCookie option to true, the module will automatically save the current path to a cookie when the user is redirected to the login page. When the user logs in, you can then retrieve the saved path from the cookie and redirect the user to it on the /confirm page:

pages/confirm.vue
<script setup lang="ts">
const user = useSupabaseUser()
const redirectInfo = useSupabaseCookieRedirect()

watch(user, () => {
  if (user.value) {
    // Get redirect path, and clear it from the cookie
    const path = redirectInfo.pluck()
    // Redirect to the saved path, or fallback to home
    return navigateTo(path || '/') 
  }
}, { immediate: true })
</script>

<template>
  <div>Waiting for login...</div>
</template>
If you want to manually set the redirect path, you can do so by disabling saveRedirectToCookie, and then set the value using the useSupabaseCookieRedirect composable directly.

Made with Nuxt Studio