Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ add flag to disable getSession after signIn on local / refresh provider #702

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
b577d1f
feat: :sparkles: add flag to disable getSession after signIn on local…
bitfactory-frank-spee Mar 5, 2024
48deba6
feat: :sparkles: add flag to disable getSession after signIn on refre…
bitfactory-frank-spee Mar 5, 2024
3d10095
docs: :sparkles: add flag to disable getSession after signIn on local…
bitfactory-frank-spee Mar 5, 2024
681fdb0
fix: :bug: sync refresh provider redirect external with local provider
bitfactory-frank-spee Mar 5, 2024
e59026b
Merge branch 'main' into feature/optional-get-session-on-sign-in
bitfactory-frank-spee Apr 25, 2024
ac9415f
refactor: :recycle: rename withGetSession boolean to withSession
bitfactory-frank-spee Jun 7, 2024
695b437
Merge branch 'sidebase:main' into feature/optional-get-session-on-sig…
bitfactory-frank-spee Jun 7, 2024
14c2618
Merge branch 'main' into feature/optional-get-session-on-sign-in
bitfactory-frank-spee Nov 5, 2024
d77805e
docs: :memo: add example to disable getSession with signIn local prov…
bitfactory-frank-spee Nov 5, 2024
930aae3
feat: :label: add withSession flag to SecondarySignInOptions interface
bitfactory-frank-spee Nov 5, 2024
7739fbc
revert: style: :art: remove markdownlint md034 url change
bitfactory-frank-spee Nov 29, 2024
87f5838
docs: :memo: improve type documentation
bitfactory-frank-spee Nov 29, 2024
156fd57
refactor: :recycle: rename withSession to callGetSession
bitfactory-frank-spee Nov 29, 2024
d5c28ef
Merge branch 'feature/optional-get-session-on-sign-in' of https://git…
bitfactory-frank-spee Nov 29, 2024
32f3e80
Merge branch 'main' into feature/optional-get-session-on-sign-in
bitfactory-frank-spee Nov 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/content/3.application-side/2.session-access-and-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

The `useAuth` composable is your main gateway to accessing and manipulating session-state and data. Here's the main methods you can use:
::code-group

```ts [authjs]
const {
status,
Expand Down Expand Up @@ -52,6 +53,7 @@ await signOut()
// Trigger a sign-out and send the user to the sign-out page afterwards
await signOut({ callbackUrl: '/signout' })
```

```ts [local]
const {
status,
Expand Down Expand Up @@ -103,12 +105,16 @@ 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-in without calling getSession directly. You have to manually call it to get session data.
await signIn(credentials, { withGetSession: false })
zoey-kaiser marked this conversation as resolved.
Show resolved Hide resolved

// Trigger a sign-out
await signOut()

// Trigger a sign-out and send the user to the sign-out page afterwards
await signOut({ callbackUrl: '/signout' })
```

```ts [refresh]
const {
status,
Expand Down Expand Up @@ -150,6 +156,9 @@ 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-in without calling getSession directly. You have to manually call it to get session data.
await signIn(credentials, { withGetSession: false })

// Trigger a refresh, this will set token to new value
await refresh()

Expand All @@ -159,17 +168,20 @@ await signOut()
// Trigger a sign-out and send the user to the sign-out page afterwards
await signOut({ callbackUrl: '/signout' })
```

::

## `SessionData`

As described above you can use:

```ts
const { data } = useAuth()
```

to access the session-data of the currently logged in user. Depending on the provider you use, this data will be typed differently:
::code-group

```ts [authjs]
interface SessionData {
user?: {
Expand All @@ -180,6 +192,7 @@ interface SessionData {
expires: ISODateString;
}
```

```ts [local]
// Option A: No explicit configuration
inferface SessionData {
Expand All @@ -194,6 +207,7 @@ inferface SessionData {
role: 'admin' | 'guest' | 'account'
}
```

::

### About `auth.provider.sessionDataType`
Expand All @@ -208,10 +222,12 @@ Calling `getSession` will by default **only** refetch the current session if the
Passing the `{ force: true }` option will always update the current session:

::code-group

```ts [local]
// force update the current session
await getSession({ force: true })
```

::

## Automatic session refreshing
Expand All @@ -223,13 +239,15 @@ You can also pass the `callbackUrl` option to both the `signIn`, the `signOut` a

You can use it like:
::code-group

```ts [authjs]
await signIn(undefined, { callbackUrl: '/protected' })

await signOut({ callbackUrl: '/protected' })

await getSession({ callbackUrl: '/protected' })
```

```ts [local]
const credentials = { username: 'bernd', password: 'hunter2' }
await signIn(credentials, { callbackUrl: '/protected' })
Expand All @@ -238,6 +256,7 @@ await signOut(credentials, { callbackUrl: '/protected' })

await getSession(credentials, { callbackUrl: '/protected' })
```

```ts [refresh]
const credentials = { username: 'bernd', password: 'hunter2' }
await signIn(credentials, { callbackUrl: '/protected' })
Expand All @@ -246,13 +265,15 @@ await signOut(credentials, { callbackUrl: '/protected' })

await getSession(credentials, { callbackUrl: '/protected' })
```

::

## `useAuthState` Composable

The `useAuthState` composable is the underlying storage layer to access the session-state and data. Here's the main methods and properties you can use:

::code-group

```ts [authjs]
const {
status,
Expand All @@ -273,6 +294,7 @@ data.value
// Time at which the session was last refreshed, either `undefined` if no refresh was attempted or a `Date`-object of the time the refresh happened
lastRefreshedAt.value
```

```ts [local]
const {
status,
Expand Down Expand Up @@ -354,6 +376,7 @@ setToken('new token')
// Helper method to quickly delete the token and refresh Token cookie (alias for rawToken.value = null and rawRefreshToken.value = null)
clearToken()
```

::

::alert{type="warning"}
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 @@ -35,9 +35,12 @@ const signIn: SignInFunc<Credentials, any> = async (credentials, signInOptions,
const { rawToken } = useAuthState()
rawToken.value = extractedToken

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

if (withGetSession) {
await nextTick(getSession)
}

const { callbackUrl, redirect = true, external } = signInOptions ?? {}
if (redirect) {
const urlToNavigateTo = callbackUrl ?? await getRequestURLWN(nuxt)
return navigateTo(urlToNavigateTo, { external })
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/composables/refresh/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ const signIn: ReturnType<typeof useLocalAuth>['signIn'] = async (
rawToken.value = extractedToken
rawRefreshToken.value = extractedRefreshToken

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

if (withGetSession) {
await nextTick(getSession)
}

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

Expand Down