Skip to content

Commit

Permalink
feat: Upgrade anonymous users to regular users automatically (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
dshukertjr authored Jun 20, 2024
1 parent 9fe1fd2 commit 2d545d1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
27 changes: 21 additions & 6 deletions lib/src/components/supa_email_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,27 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
);
widget.onSignInComplete.call(response);
} else {
final response = await supabase.auth.signUp(
email: _emailController.text.trim(),
password: _passwordController.text.trim(),
emailRedirectTo: widget.redirectTo,
data: _resolveData(),
);
final user = supabase.auth.currentUser;
late final AuthResponse response;
if (user?.isAnonymous == true) {
await supabase.auth.updateUser(
UserAttributes(
email: _emailController.text.trim(),
password: _passwordController.text.trim(),
data: _resolveData(),
),
emailRedirectTo: widget.redirectTo,
);
final newSession = supabase.auth.currentSession;
response = AuthResponse(session: newSession);
} else {
response = await supabase.auth.signUp(
email: _emailController.text.trim(),
password: _passwordController.text.trim(),
emailRedirectTo: widget.redirectTo,
data: _resolveData(),
);
}
widget.onSignUpComplete.call(response);
}
} on AuthException catch (error) {
Expand Down
17 changes: 15 additions & 2 deletions lib/src/components/supa_phone_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,21 @@ class _SupaPhoneAuthState extends State<SupaPhoneAuth> {
);
widget.onSuccess(response);
} else {
final response = await supabase.auth
.signUp(phone: _phone.text, password: _password.text);
late final AuthResponse response;
final user = supabase.auth.currentUser;
if (user?.isAnonymous == true) {
await supabase.auth.updateUser(
UserAttributes(
phone: _phone.text,
password: _password.text,
),
);
} else {
response = await supabase.auth.signUp(
phone: _phone.text,
password: _password.text,
);
}
if (!mounted) return;
widget.onSuccess(response);
}
Expand Down
11 changes: 11 additions & 0 deletions lib/src/components/supa_socials_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ class _SupaSocialsAuthState extends State<SupaSocialsAuth> {
}
}

final user = supabase.auth.currentUser;
if (user?.isAnonymous == true) {
await supabase.auth.linkIdentity(
socialProvider,
redirectTo: widget.redirectUrl,
scopes: widget.scopes?[socialProvider],
queryParams: widget.queryParams?[socialProvider],
);
return;
}

await supabase.auth.signInWithOAuth(
socialProvider,
redirectTo: widget.redirectUrl,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ environment:
dependencies:
flutter:
sdk: flutter
supabase_flutter: ^2.3.4
supabase_flutter: ^2.5.6
email_validator: ^2.0.1
font_awesome_flutter: ^10.6.0
google_sign_in: ^6.2.1
Expand Down

0 comments on commit 2d545d1

Please sign in to comment.