Skip to content

Commit 3981a72

Browse files
committed
feat: Upgrade anonymous users to regular users automatically
1 parent d8ef470 commit 3981a72

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

lib/src/components/supa_email_auth.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,27 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
227227
);
228228
widget.onSignInComplete.call(response);
229229
} else {
230-
final response = await supabase.auth.signUp(
231-
email: _emailController.text.trim(),
232-
password: _passwordController.text.trim(),
233-
emailRedirectTo: widget.redirectTo,
234-
data: _resolveData(),
235-
);
230+
final user = supabase.auth.currentUser;
231+
late final AuthResponse response;
232+
if (user?.isAnonymous == true) {
233+
await supabase.auth.updateUser(
234+
UserAttributes(
235+
email: _emailController.text.trim(),
236+
password: _passwordController.text.trim(),
237+
data: _resolveData(),
238+
),
239+
emailRedirectTo: widget.redirectTo,
240+
);
241+
final newSession = supabase.auth.currentSession;
242+
response = AuthResponse(session: newSession);
243+
} else {
244+
response = await supabase.auth.signUp(
245+
email: _emailController.text.trim(),
246+
password: _passwordController.text.trim(),
247+
emailRedirectTo: widget.redirectTo,
248+
data: _resolveData(),
249+
);
250+
}
236251
widget.onSignUpComplete.call(response);
237252
}
238253
} on AuthException catch (error) {

lib/src/components/supa_phone_auth.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,21 @@ class _SupaPhoneAuthState extends State<SupaPhoneAuth> {
107107
);
108108
widget.onSuccess(response);
109109
} else {
110-
final response = await supabase.auth
111-
.signUp(phone: _phone.text, password: _password.text);
110+
late final AuthResponse response;
111+
final user = supabase.auth.currentUser;
112+
if (user?.isAnonymous == true) {
113+
await supabase.auth.updateUser(
114+
UserAttributes(
115+
phone: _phone.text,
116+
password: _password.text,
117+
),
118+
);
119+
} else {
120+
response = await supabase.auth.signUp(
121+
phone: _phone.text,
122+
password: _password.text,
123+
);
124+
}
112125
if (!mounted) return;
113126
widget.onSuccess(response);
114127
}

lib/src/components/supa_socials_auth.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,17 @@ class _SupaSocialsAuthState extends State<SupaSocialsAuth> {
337337
}
338338
}
339339

340+
final user = supabase.auth.currentUser;
341+
if (user?.isAnonymous == true) {
342+
await supabase.auth.linkIdentity(
343+
socialProvider,
344+
redirectTo: widget.redirectUrl,
345+
scopes: widget.scopes?[socialProvider],
346+
queryParams: widget.queryParams?[socialProvider],
347+
);
348+
return;
349+
}
350+
340351
await supabase.auth.signInWithOAuth(
341352
socialProvider,
342353
redirectTo: widget.redirectUrl,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ environment:
1111
dependencies:
1212
flutter:
1313
sdk: flutter
14-
supabase_flutter: ^2.3.4
14+
supabase_flutter: ^2.5.6
1515
email_validator: ^2.0.1
1616
font_awesome_flutter: ^10.6.0
1717
google_sign_in: ^6.2.1

0 commit comments

Comments
 (0)