Skip to content

Commit

Permalink
feat: add flag to disable getSession after signIn on local provider (
Browse files Browse the repository at this point in the history
…#702)

* feat: ✨ add flag to disable getSession after signIn on local provider

* feat: ✨ add flag to disable getSession after signIn on refresh provider

* docs: ✨ add flag to disable getSession after signIn on local/refresh provider

* fix: 🐛 sync refresh provider redirect external with local provider

* refactor: ♻️ rename withGetSession boolean to withSession

* docs: 📝 add example to disable getSession with signIn local provider

* feat: 🏷️ add withSession flag to SecondarySignInOptions interface

* revert: style: 🎨 remove markdownlint md034 url change

* docs: 📝 improve type documentation

Co-authored-by: Marsel Shaikhin <[email protected]>

* refactor: ♻️ rename withSession to callGetSession

---------

Co-authored-by: Marsel Shaikhin <[email protected]>
  • Loading branch information
bitfactory-frank-spee and phoenix-ru authored Dec 19, 2024
1 parent 0d9ae7a commit b5af548
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/guide/application-side/session-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ await signIn(credentials, { callbackUrl: '/protected' })

// Trigger a signIn with a redirect to an external page afterwards
await signIn(credentials, { callbackUrl: 'https://nuxt.org', external: true })

// Trigger a signIn without calling getSession directly. You have to manually call it to get session data.
await signIn(credentials, { callGetSession: false })
```

:::
Expand Down Expand Up @@ -321,6 +324,7 @@ setToken('new token')
// Helper method to quickly delete the token cookie (alias for rawToken.value = null)
clearToken()
```

:::

:::warning Local provider:
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/composables/local/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ const signIn: SignInFunc<Credentials, any> = async (credentials, signInOptions,
rawRefreshToken.value = extractedRefreshToken
}

await nextTick(getSession)
const { redirect = true, external, callGetSession = true } = signInOptions ?? {}

if (callGetSession) {
await nextTick(getSession)
}

const { redirect = true, external } = signInOptions ?? {}
let { callbackUrl } = signInOptions ?? {}
if (typeof callbackUrl === 'undefined') {
const redirectQueryParam = useRoute()?.query?.redirect
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,12 @@ export interface SecondarySignInOptions extends Record<string, unknown> {
* @default false
*/
external?: boolean
/**
* Whether `getSession` needs to be called after a successful sign-in. When set to false, you can manually call `getSession` to obtain the session data.
*
* @default true
*/
callGetSession?: boolean
}

export interface SignUpOptions extends SecondarySignInOptions {
Expand Down

0 comments on commit b5af548

Please sign in to comment.