Skip to content

Commit

Permalink
feat(app): added redirect path cookie for use after signin (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthewenderle authored Jan 26, 2024
1 parent 0caf5c1 commit 1b78a05
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/content/2.get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ Default:
login: '/login',
callback: '/confirm',
exclude: [],
cookieRedirect: false,
}
```
- `login`: User will be redirected to this path if not authenticated or after logout.
- `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`.
- `exclude`: Routes to exclude from the redirect. `['/foo', '/bar/*']` will exclude the `foo` page and all pages in your `bar` folder.
- `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.
### `cookieName`
Expand Down
10 changes: 9 additions & 1 deletion docs/content/3.authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,17 @@ The redirect URL must be configured in your Supabase dashboard under `Authentica
```vue [pages/confirm.vue]
<script setup lang="ts">
const user = useSupabaseUser()
// Pull in the config to get cookie details
const config = useRuntimeConfig().public.supabase;
const { cookieName } = config;
const redirectPath = useCookie(`${cookieName}-redirect-path`).value;
watch(user, () => {
if (user.value) {
return navigateTo('/')
useCookie(`${cookieName}-redirect-path`).value = null // Clear the cookie
return navigateTo(redirectPath || '/'); // Redirect or go to protected page
}
}, { immediate: true })
Expand Down
6 changes: 3 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export default defineNuxtModule<ModuleOptions>({
redirectOptions: {
login: '/login',
callback: '/confirm',
exclude: []
exclude: [],
cookieRedirect: false
},
cookieName: 'sb',
cookieOptions: {
Expand Down Expand Up @@ -151,8 +152,7 @@ export default defineNuxtModule<ModuleOptions>({

// ensure callback URL is not using SSR
const mergedOptions = nuxt.options.runtimeConfig.public.supabase
if (mergedOptions.redirect &&
mergedOptions.redirectOptions.callback) {
if (mergedOptions.redirect && mergedOptions.redirectOptions.callback) {
const routeRules: { [key: string]: any } = {}
routeRules[mergedOptions.redirectOptions.callback] = { ssr: false }
nuxt.options.nitro = defu(nuxt.options.nitro, {
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/plugins/auth-redirect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSupabaseUser } from '../composables/useSupabaseUser'
import { defineNuxtPlugin, addRouteMiddleware, defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo } from '#imports'
import { defineNuxtPlugin, addRouteMiddleware, defineNuxtRouteMiddleware, useCookie, useRuntimeConfig, navigateTo } from '#imports'

export default defineNuxtPlugin({
name: 'auth-redirect',
Expand All @@ -8,7 +8,8 @@ export default defineNuxtPlugin({
'global-auth',
defineNuxtRouteMiddleware((to) => {
const config = useRuntimeConfig().public.supabase
const { login, callback, exclude } = config.redirectOptions
const { login, callback, exclude, cookieRedirect } = config.redirectOptions
const { cookieName, cookieOptions } = config

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

const user = useSupabaseUser()
if (!user.value) {
if (cookieRedirect) { useCookie(`${cookieName}-redirect-path`, cookieOptions).value = to.fullPath }
return navigateTo(login)
}
}),
Expand Down
1 change: 1 addition & 0 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface RedirectOptions {
login: string
callback: string
exclude?: string[]
cookieRedirect?: boolean
}

0 comments on commit 1b78a05

Please sign in to comment.