Skip to content

Commit 3619096

Browse files
committed
feat: add signOut() scopes option
1 parent 964f2fd commit 3619096

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/GoTrueAdminApi.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ export default class GoTrueAdminApi {
5555
/**
5656
* Removes a logged-in session.
5757
* @param jwt A valid, logged-in JWT.
58+
* @param scope The logout sope.
5859
*/
59-
async signOut(jwt: string): Promise<{ data: null; error: AuthError | null }> {
60+
async signOut(
61+
jwt: string,
62+
scope: 'global' | 'local' | 'others' = 'global'
63+
): Promise<{ data: null; error: AuthError | null }> {
6064
try {
61-
await _request(this.fetch, 'POST', `${this.url}/logout`, {
65+
await _request(this.fetch, 'POST', `${this.url}/logout?scope=${scope}`, {
6266
headers: this.headers,
6367
jwt,
6468
noResolveJson: true,

src/GoTrueClient.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import type {
5151
SignInWithPasswordlessCredentials,
5252
SignUpWithPasswordCredentials,
5353
SignInWithSSO,
54+
SignOut,
5455
Subscription,
5556
SupportedStorage,
5657
User,
@@ -1075,15 +1076,17 @@ export default class GoTrueClient {
10751076
*
10761077
* 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)`.
10771078
* 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.
1079+
*
1080+
* If using others scope, no `SIGNED_OUT` event is fired!
10781081
*/
1079-
async signOut(): Promise<{ error: AuthError | null }> {
1082+
async signOut({ scope }: SignOut = { scope: 'global' }): Promise<{ error: AuthError | null }> {
10801083
const { data, error: sessionError } = await this.getSession()
10811084
if (sessionError) {
10821085
return { error: sessionError }
10831086
}
10841087
const accessToken = data.session?.access_token
10851088
if (accessToken) {
1086-
const { error } = await this.admin.signOut(accessToken)
1089+
const { error } = await this.admin.signOut(accessToken, scope)
10871090
if (error) {
10881091
// ignore 404s since user might not exist anymore
10891092
// ignore 401s since an invalid or expired JWT should sign out the current session
@@ -1092,9 +1095,11 @@ export default class GoTrueClient {
10921095
}
10931096
}
10941097
}
1095-
await this._removeSession()
1096-
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
1097-
await this._notifyAllSubscribers('SIGNED_OUT', null)
1098+
if (scope !== 'others') {
1099+
await this._removeSession()
1100+
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
1101+
await this._notifyAllSubscribers('SIGNED_OUT', null)
1102+
}
10981103
return { error: null }
10991104
}
11001105

src/lib/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,3 +1010,17 @@ export type PageParams = {
10101010
/** Number of items returned per page */
10111011
perPage?: number
10121012
}
1013+
1014+
export type SignOut = {
1015+
/**
1016+
* Determines which sessions should be
1017+
* logged out. Global means all
1018+
* sessions by this account. Local
1019+
* means only this session. Others
1020+
* means all other sessions except the
1021+
* current one. When using others,
1022+
* there is no sign-out event fired on
1023+
* the current session!
1024+
*/
1025+
scope?: 'global' | 'local' | 'others'
1026+
}

0 commit comments

Comments
 (0)