Skip to content

Commit 94a1cce

Browse files
feat(gotrue): add scope to signOut (#530)
* feat(gotrue): add scope to signOut * fix: always remove session * Update packages/gotrue/lib/src/constants.dart Co-authored-by: Tyler <[email protected]> * fix: typo --------- Co-authored-by: Tyler <[email protected]>
1 parent 0a37cd4 commit 94a1cce

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

packages/gotrue/lib/src/constants.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ enum OtpType {
5656
recovery,
5757
emailChange
5858
}
59+
60+
///Determines which sessions should be logged out.
61+
///
62+
///[global] means all sessions by this account will be signed out.
63+
///
64+
///[local] means only this session will be signed out.
65+
///
66+
///[others] means all other sessions except the current one will be signed out. When using others, there is no [AuthChangeEvent.signedOut] event fired on the current session!
67+
enum SignOutScope { global, local, others }

packages/gotrue/lib/src/gotrue_admin_api.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:gotrue/gotrue.dart';
2+
import 'package:gotrue/src/constants.dart';
23
import 'package:gotrue/src/fetch.dart';
34
import 'package:gotrue/src/types/auth_response.dart';
45
import 'package:gotrue/src/types/fetch_options.dart';
@@ -28,9 +29,17 @@ class GoTrueAdminApi {
2829
}
2930

3031
/// Removes a logged-in session.
31-
Future<void> signOut(String jwt) async {
32-
final options =
33-
GotrueRequestOptions(headers: _headers, noResolveJson: true, jwt: jwt);
32+
Future<void> signOut(
33+
String jwt, {
34+
SignOutScope scope = SignOutScope.global,
35+
}) async {
36+
final options = GotrueRequestOptions(
37+
headers: _headers,
38+
noResolveJson: true,
39+
jwt: jwt,
40+
query: {'scope': scope.name},
41+
);
42+
3443
await _fetch.request(
3544
'$_url/logout',
3645
RequestMethodType.post,

packages/gotrue/lib/src/gotrue_client.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,13 +618,23 @@ class GoTrueClient {
618618
}
619619

620620
/// Signs out the current user, if there is a logged in user.
621-
Future<void> signOut() async {
621+
///
622+
/// If using [SignOutScope.others] scope, no [AuthChangeEvent.signedOut] event is fired!
623+
Future<void> signOut({
624+
SignOutScope scope = SignOutScope.global,
625+
}) async {
622626
final accessToken = currentSession?.accessToken;
623-
_removeSession();
624-
_notifyAllSubscribers(AuthChangeEvent.signedOut);
627+
628+
if (scope != SignOutScope.others) {
629+
_removeSession();
630+
await _asyncStorage?.removeItem(
631+
key: '${Constants.defaultStorageKey}-code-verifier');
632+
_notifyAllSubscribers(AuthChangeEvent.signedOut);
633+
}
634+
625635
if (accessToken != null) {
626636
try {
627-
await admin.signOut(accessToken);
637+
await admin.signOut(accessToken, scope: scope);
628638
} on AuthException catch (error) {
629639
// ignore 401s since an invalid or expired JWT should sign out the current session
630640
// ignore 404s since user might not exist anymore

0 commit comments

Comments
 (0)