Skip to content

Commit 77e60c0

Browse files
committed
feat: slightly better docs
1 parent e0ce5a5 commit 77e60c0

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

playground/nuxt.config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ export default defineNuxtConfig({
99
auth: {
1010
nextAuth: {
1111
options: {
12-
providers: [GithubProvider({
13-
clientId: 'enter-your-client-id-here',
14-
clientSecret: 'enter-your-client-secret-here'
15-
})]
12+
providers: [
13+
GithubProvider({
14+
clientId: 'enter-your-client-id-here',
15+
clientSecret: 'enter-your-client-secret-here'
16+
})
17+
]
1618
}
1719
}
1820
}

playground/pages/index.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<template>
22
<div>
3-
<button @click="signIn">
3+
<button @click="signIn({ callbackUrl: '/'})">
44
sign in with github
55
</button>
6+
<button @click="signIn({ callbackUrl: '/protected/inline' })">
7+
sign in with redirect
8+
</button>
69
<button @click="signOut">
710
sign out
811
</button>

src/runtime/composables/useSession.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ interface UseSessionOptions {
1313
onUnauthenticated?: () => void
1414
}
1515

16-
type SignInOptions = Partial<UseSessionOptions>
16+
type GetSessionOptions = Partial<UseSessionOptions>
17+
18+
interface SignInOptions {
19+
/**
20+
* Specify to which URL the user will be redirected after signing in. Defaults to the page URL the sign-in is initiated from.
21+
*
22+
* [Documentation](https://next-auth.js.org/getting-started/client#specifying-a-callbackurl)
23+
*/
24+
callbackUrl?: string
25+
/** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option) */
26+
redirect?: boolean
27+
}
1728

1829
interface SignOutOptions {
1930
callbackUrl?: string
@@ -57,12 +68,23 @@ export default async (initialGetSessionOptions: UseSessionOptions = {}) => {
5768
const data = useState<SessionData>('session:data', () => undefined)
5869
const status = useState<SessionStatus>('session:status', () => 'unauthenticated')
5970

60-
const signIn = () => {
61-
const url = joinPathToBase('signin')
62-
window.location.href = url
71+
// TODO: Improve typing, make `provider` a literal union instead
72+
const signIn = async (options?: Pick<SignInOptions, 'callbackUrl'>) => {
73+
const providers = await getProviders()
74+
75+
if (!providers) {
76+
window.location.href = joinPathToBase('error')
77+
return
78+
}
79+
80+
const { callbackUrl = window.location.href } = options ?? {}
81+
window.location.href = `${joinPathToBase('signin')}?${new URLSearchParams({
82+
callbackUrl
83+
})}`
84+
85+
// TODO: Add support for credential and mail flows, see https://github.com/nextauthjs/next-auth/blob/733fd5f2345cbf7c123ba8175ea23506bcb5c453/packages/next-auth/src/react/index.tsx#L226-L228
6386
}
6487

65-
// TODO: add callback url support https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/react/index.tsx#L284
6688
const signOut = async ({ callbackUrl }: SignOutOptions) => {
6789
const csrfTokenResult = await getCsrfToken()
6890

@@ -98,7 +120,7 @@ export default async (initialGetSessionOptions: UseSessionOptions = {}) => {
98120

99121
const getCsrfToken = () => _fetch<{ csrfToken: string }>('csrf')
100122
const getProviders = () => _fetch<AppProvider[]>('providers')
101-
const getSession = (getSessionOptions?: SignInOptions) => {
123+
const getSession = (getSessionOptions?: GetSessionOptions) => {
102124
const { required, callbackUrl, onUnauthenticated } = defu(getSessionOptions || {}, {
103125
required: true,
104126
callbackUrl: undefined,

0 commit comments

Comments
 (0)