Skip to content

feat: add signOut() scope option #713

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

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions src/GoTrueAdminApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ export default class GoTrueAdminApi {
/**
* Removes a logged-in session.
* @param jwt A valid, logged-in JWT.
* @param scope The logout sope.
*/
async signOut(jwt: string): Promise<{ data: null; error: AuthError | null }> {
async signOut(
jwt: string,
scope: 'global' | 'local' | 'others' = 'global'
): Promise<{ data: null; error: AuthError | null }> {
try {
await _request(this.fetch, 'POST', `${this.url}/logout`, {
await _request(this.fetch, 'POST', `${this.url}/logout?scope=${scope}`, {
headers: this.headers,
jwt,
noResolveJson: true,
Expand Down
15 changes: 10 additions & 5 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type {
SignInWithPasswordlessCredentials,
SignUpWithPasswordCredentials,
SignInWithSSO,
SignOut,
Subscription,
SupportedStorage,
User,
Expand Down Expand Up @@ -1075,15 +1076,17 @@ export default class GoTrueClient {
*
* For server-side management, you can revoke all refresh tokens for a user by passing a user's JWT through to `auth.api.signOut(JWT: string)`.
* There is no way to revoke a user's access token jwt until it expires. It is recommended to set a shorter expiry on the jwt for this reason.
*
* If using others scope, no `SIGNED_OUT` event is fired!
*/
async signOut(): Promise<{ error: AuthError | null }> {
async signOut({ scope }: SignOut = { scope: 'global' }): Promise<{ error: AuthError | null }> {
const { data, error: sessionError } = await this.getSession()
if (sessionError) {
return { error: sessionError }
}
const accessToken = data.session?.access_token
if (accessToken) {
const { error } = await this.admin.signOut(accessToken)
const { error } = await this.admin.signOut(accessToken, scope)
if (error) {
// ignore 404s since user might not exist anymore
// ignore 401s since an invalid or expired JWT should sign out the current session
Expand All @@ -1092,9 +1095,11 @@ export default class GoTrueClient {
}
}
}
await this._removeSession()
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('SIGNED_OUT', null)
if (scope !== 'others') {
await this._removeSession()
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('SIGNED_OUT', null)
}
return { error: null }
}

Expand Down
14 changes: 14 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,17 @@ export type PageParams = {
/** Number of items returned per page */
perPage?: number
}

export type SignOut = {
/**
* Determines which sessions should be
* logged out. Global means all
* sessions by this account. Local
* means only this session. Others
* means all other sessions except the
* current one. When using others,
* there is no sign-out event fired on
* the current session!
*/
scope?: 'global' | 'local' | 'others'
}