Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export default defineNuxtConfig({
exclude: ['/unprotected', '/public/*']
},
},
nitro: {
routeRules: {
'/clientonly': { ssr: false }
},
},
})
5 changes: 5 additions & 0 deletions playground/pages/clientonly.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
This page is rendered without SSR.
</div>
</template>
17 changes: 7 additions & 10 deletions playground/pages/confirm.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<script setup lang="ts">
if (process.server) {
console.log(
'ERROR: It is very important to make sure that the redirect route right after login works without any server-side rendering. This is because the server-side rendering will not have the user data available. This is why we need to redirect to a client-side route right after login. This is a limitation of Supabase. If you are using Nuxt, you can use the no-ssr component to make sure that the redirect route is not server-side rendered.',
)
}
const user = useSupabaseUser()

watch(user, () => {
if (process.server) {
console.log(
'ERROR: It is very important to make sure that the redirect route right after login works without any server-side rendering. This is because the server-side rendering will not have the user data available. This is why we need to redirect to a client-side route right after login. This is a limitation of Supabase. If you are using Nuxt, you can use the no-ssr component to make sure that the redirect route is not server-side rendered.',
)
}
const user = useSupabaseUser()
if (user.value) {
return navigateTo('/user')
await navigateTo('/user')
}
})
</script>
<template>
<div>Waiting for login...</div>
Expand Down
2 changes: 0 additions & 2 deletions src/runtime/composables/useSupabaseUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { User } from '@supabase/supabase-js'
import type { Ref } from 'vue'
import { useSupabaseClient } from './useSupabaseClient'
import { useState } from '#imports'

export const useSupabaseUser = () => {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/plugins/auth-redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export default defineNuxtPlugin({
setup() {
addRouteMiddleware(
'global-auth',
defineNuxtRouteMiddleware(async to => {
defineNuxtRouteMiddleware(to => {
const config = useRuntimeConfig().public.supabase
const { login, callback, exclude } = config.redirectOptions

// Do not redirect on login route, callback route and excluded routes
const isExcluded = [...exclude, login, callback]?.some((path) => {
const regex = new RegExp(`^${path.replace(/\*/g, ".*")}$`)
const isExcluded = [...exclude, login, callback]?.some(path => {
const regex = new RegExp(`^${path.replace(/\*/g, '.*')}$`)
return regex.test(to.path)
})
if (isExcluded) return
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/plugins/supabase.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineNuxtPlugin({

// Handle auth event client side
supabaseClient.auth.onAuthStateChange(async (event, session) => {
// Update user based on received session
if (session) {
if (JSON.stringify(user.value) !== JSON.stringify(session.user)) {
user.value = session.user;
Expand All @@ -22,6 +23,7 @@ export default defineNuxtPlugin({
user.value = null
}

// Use cookies to share session state between server and client
if (event === 'SIGNED_IN' || event === 'TOKEN_REFRESHED') {
useCookie(`${cookieName}-access-token`, cookieOptions).value = session?.access_token
useCookie(`${cookieName}-refresh-token`, cookieOptions).value = session?.refresh_token
Expand All @@ -36,10 +38,13 @@ export default defineNuxtPlugin({
}
})

// Attempt to retrieve existing session from local storage
await supabaseClient.auth.getSession()
Copy link
Collaborator

@larbish larbish Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call fix the client rendered page issue since it ensures the session/user is set on client side refresh. It means we need to update the confirm.vue page in the playground and the documentation (also mention it in the release):

const user = useSupabaseUser()
if (user.value) {
  await navigateTo('/user')
}

should be enough, we don't need to watch it no more.


return {
provide: {
supabase: {
client: supabaseClient
client: supabaseClient,
},
},
}
Expand Down