Skip to content

Commit 2d545d1

Browse files
authored
feat: Upgrade anonymous users to regular users automatically (#100)
1 parent 9fe1fd2 commit 2d545d1

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
@@ -244,12 +244,27 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
244244
);
245245
widget.onSignInComplete.call(response);
246246
} else {
247-
final response = await supabase.auth.signUp(
248-
email: _emailController.text.trim(),
249-
password: _passwordController.text.trim(),
250-
emailRedirectTo: widget.redirectTo,
251-
data: _resolveData(),
252-
);
247+
final user = supabase.auth.currentUser;
248+
late final AuthResponse response;
249+
if (user?.isAnonymous == true) {
250+
await supabase.auth.updateUser(
251+
UserAttributes(
252+
email: _emailController.text.trim(),
253+
password: _passwordController.text.trim(),
254+
data: _resolveData(),
255+
),
256+
emailRedirectTo: widget.redirectTo,
257+
);
258+
final newSession = supabase.auth.currentSession;
259+
response = AuthResponse(session: newSession);
260+
} else {
261+
response = await supabase.auth.signUp(
262+
email: _emailController.text.trim(),
263+
password: _passwordController.text.trim(),
264+
emailRedirectTo: widget.redirectTo,
265+
data: _resolveData(),
266+
);
267+
}
253268
widget.onSignUpComplete.call(response);
254269
}
255270
} 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
@@ -338,6 +338,17 @@ class _SupaSocialsAuthState extends State<SupaSocialsAuth> {
338338
}
339339
}
340340

341+
final user = supabase.auth.currentUser;
342+
if (user?.isAnonymous == true) {
343+
await supabase.auth.linkIdentity(
344+
socialProvider,
345+
redirectTo: widget.redirectUrl,
346+
scopes: widget.scopes?[socialProvider],
347+
queryParams: widget.queryParams?[socialProvider],
348+
);
349+
return;
350+
}
351+
341352
await supabase.auth.signInWithOAuth(
342353
socialProvider,
343354
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)