Skip to content

Commit 1b78a05

Browse files
feat(app): added redirect path cookie for use after signin (#327)
1 parent 0caf5c1 commit 1b78a05

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

docs/content/2.get-started.md

+2
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ Default:
9898
login: '/login',
9999
callback: '/confirm',
100100
exclude: [],
101+
cookieRedirect: false,
101102
}
102103
```
103104
- `login`: User will be redirected to this path if not authenticated or after logout.
104105
- `callback`: This is the path the user will be redirect to after supabase login redirection. Should match configured `redirectTo` option of your [signIn method](https://supabase.com/docs/reference/javascript/auth-signinwithoauth). Should also be configured in your Supabase dashboard under `Authentication -> URL Configuration -> Redirect URLs`.
105106
- `exclude`: Routes to exclude from the redirect. `['/foo', '/bar/*']` will exclude the `foo` page and all pages in your `bar` folder.
107+
- `cookieRedirect`: Sets a cookie containing the path an unauthenticated user tried to access. The cookie can then be used on the [`/confirm`](https://supabase.nuxtjs.org/authentication#confirm-page-confirm) page to redirect the user to the page they previously tried to visit.
106108
107109
### `cookieName`
108110

docs/content/3.authentication.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ The redirect URL must be configured in your Supabase dashboard under `Authentica
6565
```vue [pages/confirm.vue]
6666
<script setup lang="ts">
6767
const user = useSupabaseUser()
68+
69+
// Pull in the config to get cookie details
70+
const config = useRuntimeConfig().public.supabase;
71+
const { cookieName } = config;
72+
const redirectPath = useCookie(`${cookieName}-redirect-path`).value;
73+
74+
6875
watch(user, () => {
6976
if (user.value) {
70-
return navigateTo('/')
77+
useCookie(`${cookieName}-redirect-path`).value = null // Clear the cookie
78+
return navigateTo(redirectPath || '/'); // Redirect or go to protected page
7179
}
7280
}, { immediate: true })
7381

src/module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ export default defineNuxtModule<ModuleOptions>({
102102
redirectOptions: {
103103
login: '/login',
104104
callback: '/confirm',
105-
exclude: []
105+
exclude: [],
106+
cookieRedirect: false
106107
},
107108
cookieName: 'sb',
108109
cookieOptions: {
@@ -151,8 +152,7 @@ export default defineNuxtModule<ModuleOptions>({
151152

152153
// ensure callback URL is not using SSR
153154
const mergedOptions = nuxt.options.runtimeConfig.public.supabase
154-
if (mergedOptions.redirect &&
155-
mergedOptions.redirectOptions.callback) {
155+
if (mergedOptions.redirect && mergedOptions.redirectOptions.callback) {
156156
const routeRules: { [key: string]: any } = {}
157157
routeRules[mergedOptions.redirectOptions.callback] = { ssr: false }
158158
nuxt.options.nitro = defu(nuxt.options.nitro, {

src/runtime/plugins/auth-redirect.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useSupabaseUser } from '../composables/useSupabaseUser'
2-
import { defineNuxtPlugin, addRouteMiddleware, defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo } from '#imports'
2+
import { defineNuxtPlugin, addRouteMiddleware, defineNuxtRouteMiddleware, useCookie, useRuntimeConfig, navigateTo } from '#imports'
33

44
export default defineNuxtPlugin({
55
name: 'auth-redirect',
@@ -8,7 +8,8 @@ export default defineNuxtPlugin({
88
'global-auth',
99
defineNuxtRouteMiddleware((to) => {
1010
const config = useRuntimeConfig().public.supabase
11-
const { login, callback, exclude } = config.redirectOptions
11+
const { login, callback, exclude, cookieRedirect } = config.redirectOptions
12+
const { cookieName, cookieOptions } = config
1213

1314
// Do not redirect on login route, callback route and excluded routes
1415
const isExcluded = [...exclude, login, callback]?.some((path) => {
@@ -19,6 +20,7 @@ export default defineNuxtPlugin({
1920

2021
const user = useSupabaseUser()
2122
if (!user.value) {
23+
if (cookieRedirect) { useCookie(`${cookieName}-redirect-path`, cookieOptions).value = to.fullPath }
2224
return navigateTo(login)
2325
}
2426
}),

src/runtime/types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export interface RedirectOptions {
1818
login: string
1919
callback: string
2020
exclude?: string[]
21+
cookieRedirect?: boolean
2122
}

0 commit comments

Comments
 (0)