Skip to content

Commit

Permalink
feat: Added option to set callbackUrl as external (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoey-kaiser authored Oct 23, 2023
1 parent 6190dec commit 87e448a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ await signIn(credentials)
// Trigger a sign-in with a redirect afterwards
await signIn(credentials, { callbackUrl: '/protected' })

// Trigger a sign-in with a redirect afterwards to an external page (if set, this will cause a hard refresh of the page)
await signIn(credentials, { callbackUrl: 'https://sidebase.io', external: true })

// Trigger a sign-out
await signOut()

Expand Down
12 changes: 6 additions & 6 deletions src/runtime/composables/local/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ const signIn: SignInFunc<Credentials, any> = async (credentials, signInOptions,

await nextTick(getSession)

const { callbackUrl, redirect = true } = signInOptions ?? {}
const { callbackUrl, redirect = true, external } = signInOptions ?? {}
if (redirect) {
const urlToNavigateTo = callbackUrl ?? await getRequestURLWN(nuxt)
return navigateTo(urlToNavigateTo, { external: true })
return navigateTo(urlToNavigateTo, { external })
}
}

Expand All @@ -57,9 +57,9 @@ const signOut: SignOutFunc = async (signOutOptions) => {

const res = await _fetch(nuxt, path, { method, headers })

const { callbackUrl, redirect = true } = signOutOptions ?? {}
const { callbackUrl, redirect = true, external } = signOutOptions ?? {}
if (redirect) {
await navigateTo(callbackUrl ?? await getRequestURLWN(nuxt), { external: true })
await navigateTo(callbackUrl ?? await getRequestURLWN(nuxt), { external })
}

return res
Expand Down Expand Up @@ -89,12 +89,12 @@ const getSession: GetSessionFunc<SessionData | null | void> = async (getSessionO
loading.value = false
lastRefreshedAt.value = new Date()

const { required = false, callbackUrl, onUnauthenticated } = getSessionOptions ?? {}
const { required = false, callbackUrl, onUnauthenticated, external } = getSessionOptions ?? {}
if (required && data.value === null) {
if (onUnauthenticated) {
return onUnauthenticated()
} else {
await navigateTo(callbackUrl ?? await getRequestURLWN(nuxt), { external: true })
await navigateTo(callbackUrl ?? await getRequestURLWN(nuxt), { external })
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,23 @@ export interface SecondarySignInOptions extends Record<string, unknown> {
* @default true
*/
redirect?: boolean
/** Is this callback URL an external one. Setting this to true, allows you to redirect to external urls, however a hard refresh will be done.
*
* @default false
*/
external?: boolean
}

export interface SignOutOptions {
callbackUrl?: string
redirect?: boolean
external?: boolean
}

export type GetSessionOptions = Partial<{
required?: boolean
callbackUrl?: string
external?: boolean,
onUnauthenticated?: () => void
/** Whether to refetch the session even if the token returned by useAuthState is null.
*
Expand Down

0 comments on commit 87e448a

Please sign in to comment.