Skip to content

Commit 200264b

Browse files
Copilotmaurovanetti
andcommitted
Refactor: extract form submission logic into separate methods
- Extract _signInWithMagicLink() method in SupaMagicAuth - Extract _submitForm() method in SupaPhoneAuth - Extract _updatePassword() method in SupaResetPassword - Fix use_build_context_synchronously warnings by properly checking context.mounted - Fix sort_child_properties_last warnings by placing child parameter last - Reduces code duplication and improves maintainability Co-authored-by: maurovanetti <402070+maurovanetti@users.noreply.github.com>
1 parent 9b8f3bd commit 200264b

File tree

3 files changed

+127
-219
lines changed

3 files changed

+127
-219
lines changed

lib/src/components/supa_magic_auth.dart

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,45 @@ class _SupaMagicAuthState extends State<SupaMagicAuth> {
7070
super.dispose();
7171
}
7272

73+
Future<void> _signInWithMagicLink() async {
74+
if (!_formKey.currentState!.validate()) {
75+
return;
76+
}
77+
setState(() {
78+
_isLoading = true;
79+
});
80+
try {
81+
await supabase.auth.signInWithOtp(
82+
email: _email.text,
83+
emailRedirectTo: widget.redirectUrl,
84+
);
85+
if (!mounted) return;
86+
if (context.mounted) {
87+
context.showSnackBar(widget.localization.checkYourEmail);
88+
}
89+
} on AuthException catch (error) {
90+
if (widget.onError != null) {
91+
widget.onError?.call(error);
92+
} else if (context.mounted) {
93+
context.showErrorSnackBar(error.message);
94+
}
95+
} catch (error) {
96+
if (widget.onError != null) {
97+
widget.onError?.call(error);
98+
} else if (context.mounted) {
99+
context.showErrorSnackBar(
100+
'${widget.localization.unexpectedError}: $error',
101+
);
102+
}
103+
} finally {
104+
if (mounted) {
105+
setState(() {
106+
_isLoading = false;
107+
});
108+
}
109+
}
110+
}
111+
73112
@override
74113
Widget build(BuildContext context) {
75114
final localization = widget.localization;
@@ -96,43 +135,13 @@ class _SupaMagicAuthState extends State<SupaMagicAuth> {
96135
controller: _email,
97136
onFieldSubmitted: (_) async {
98137
if (widget.enableAutomaticFormSubmission) {
99-
if (!_formKey.currentState!.validate()) {
100-
return;
101-
}
102-
setState(() {
103-
_isLoading = true;
104-
});
105-
try {
106-
await supabase.auth.signInWithOtp(
107-
email: _email.text,
108-
emailRedirectTo: widget.redirectUrl,
109-
);
110-
if (context.mounted) {
111-
context.showSnackBar(localization.checkYourEmail);
112-
}
113-
} on AuthException catch (error) {
114-
if (widget.onError == null && context.mounted) {
115-
context.showErrorSnackBar(error.message);
116-
} else {
117-
widget.onError?.call(error);
118-
}
119-
} catch (error) {
120-
if (widget.onError == null && context.mounted) {
121-
context.showErrorSnackBar(
122-
'${localization.unexpectedError}: $error',
123-
);
124-
} else {
125-
widget.onError?.call(error);
126-
}
127-
}
128-
setState(() {
129-
_isLoading = false;
130-
});
138+
await _signInWithMagicLink();
131139
}
132140
},
133141
),
134142
spacer(16),
135143
ElevatedButton(
144+
onPressed: _signInWithMagicLink,
136145
child: (_isLoading)
137146
? SizedBox(
138147
height: 16,
@@ -146,40 +155,6 @@ class _SupaMagicAuthState extends State<SupaMagicAuth> {
146155
localization.continueWithMagicLink,
147156
style: const TextStyle(fontWeight: FontWeight.bold),
148157
),
149-
onPressed: () async {
150-
if (!_formKey.currentState!.validate()) {
151-
return;
152-
}
153-
setState(() {
154-
_isLoading = true;
155-
});
156-
try {
157-
await supabase.auth.signInWithOtp(
158-
email: _email.text,
159-
emailRedirectTo: widget.redirectUrl,
160-
);
161-
if (context.mounted) {
162-
context.showSnackBar(localization.checkYourEmail);
163-
}
164-
} on AuthException catch (error) {
165-
if (widget.onError == null && context.mounted) {
166-
context.showErrorSnackBar(error.message);
167-
} else {
168-
widget.onError?.call(error);
169-
}
170-
} catch (error) {
171-
if (widget.onError == null && context.mounted) {
172-
context.showErrorSnackBar(
173-
'${localization.unexpectedError}: $error',
174-
);
175-
} else {
176-
widget.onError?.call(error);
177-
}
178-
}
179-
setState(() {
180-
_isLoading = false;
181-
});
182-
},
183158
),
184159
spacer(10),
185160
],

lib/src/components/supa_phone_auth.dart

Lines changed: 53 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,57 @@ class _SupaPhoneAuthState extends State<SupaPhoneAuth> {
5555
super.dispose();
5656
}
5757

58+
Future<void> _submitForm() async {
59+
if (!_formKey.currentState!.validate()) {
60+
return;
61+
}
62+
final localization = widget.localization;
63+
final isSigningIn = widget.authAction == SupaAuthAction.signIn;
64+
try {
65+
if (isSigningIn) {
66+
final response = await supabase.auth.signInWithPassword(
67+
phone: _phone.text,
68+
password: _password.text,
69+
);
70+
if (!mounted) return;
71+
widget.onSuccess(response);
72+
} else {
73+
late final AuthResponse response;
74+
final user = supabase.auth.currentUser;
75+
if (user?.isAnonymous == true) {
76+
await supabase.auth.updateUser(
77+
UserAttributes(phone: _phone.text, password: _password.text),
78+
);
79+
} else {
80+
response = await supabase.auth.signUp(
81+
phone: _phone.text,
82+
password: _password.text,
83+
);
84+
}
85+
if (!mounted) return;
86+
widget.onSuccess(response);
87+
}
88+
} on AuthException catch (error) {
89+
if (widget.onError != null) {
90+
widget.onError?.call(error);
91+
} else if (context.mounted) {
92+
context.showErrorSnackBar(error.message);
93+
}
94+
} catch (error) {
95+
if (widget.onError != null) {
96+
widget.onError?.call(error);
97+
} else if (context.mounted) {
98+
context.showErrorSnackBar('${localization.unexpectedError}: $error');
99+
}
100+
}
101+
if (mounted) {
102+
setState(() {
103+
_phone.text = '';
104+
_password.text = '';
105+
});
106+
}
107+
}
108+
58109
@override
59110
Widget build(BuildContext context) {
60111
final localization = widget.localization;
@@ -100,114 +151,17 @@ class _SupaPhoneAuthState extends State<SupaPhoneAuth> {
100151
controller: _password,
101152
onFieldSubmitted: (_) async {
102153
if (widget.enableAutomaticFormSubmission) {
103-
// Trigger form validation and submission
104-
if (!_formKey.currentState!.validate()) {
105-
return;
106-
}
107-
try {
108-
if (isSigningIn) {
109-
final response = await supabase.auth.signInWithPassword(
110-
phone: _phone.text,
111-
password: _password.text,
112-
);
113-
widget.onSuccess(response);
114-
} else {
115-
late final AuthResponse response;
116-
final user = supabase.auth.currentUser;
117-
if (user?.isAnonymous == true) {
118-
await supabase.auth.updateUser(
119-
UserAttributes(
120-
phone: _phone.text,
121-
password: _password.text,
122-
),
123-
);
124-
} else {
125-
response = await supabase.auth.signUp(
126-
phone: _phone.text,
127-
password: _password.text,
128-
);
129-
}
130-
if (!mounted) return;
131-
widget.onSuccess(response);
132-
}
133-
} on AuthException catch (error) {
134-
if (widget.onError == null && context.mounted) {
135-
context.showErrorSnackBar(error.message);
136-
} else {
137-
widget.onError?.call(error);
138-
}
139-
} catch (error) {
140-
if (widget.onError == null && context.mounted) {
141-
context.showErrorSnackBar(
142-
'${localization.unexpectedError}: $error',
143-
);
144-
} else {
145-
widget.onError?.call(error);
146-
}
147-
}
148-
setState(() {
149-
_phone.text = '';
150-
_password.text = '';
151-
});
154+
await _submitForm();
152155
}
153156
},
154157
),
155158
spacer(16),
156159
ElevatedButton(
160+
onPressed: _submitForm,
157161
child: Text(
158162
isSigningIn ? localization.signIn : localization.signUp,
159163
style: const TextStyle(fontWeight: FontWeight.bold),
160164
),
161-
onPressed: () async {
162-
if (!_formKey.currentState!.validate()) {
163-
return;
164-
}
165-
try {
166-
if (isSigningIn) {
167-
final response = await supabase.auth.signInWithPassword(
168-
phone: _phone.text,
169-
password: _password.text,
170-
);
171-
widget.onSuccess(response);
172-
} else {
173-
late final AuthResponse response;
174-
final user = supabase.auth.currentUser;
175-
if (user?.isAnonymous == true) {
176-
await supabase.auth.updateUser(
177-
UserAttributes(
178-
phone: _phone.text,
179-
password: _password.text,
180-
),
181-
);
182-
} else {
183-
response = await supabase.auth.signUp(
184-
phone: _phone.text,
185-
password: _password.text,
186-
);
187-
}
188-
if (!mounted) return;
189-
widget.onSuccess(response);
190-
}
191-
} on AuthException catch (error) {
192-
if (widget.onError == null && context.mounted) {
193-
context.showErrorSnackBar(error.message);
194-
} else {
195-
widget.onError?.call(error);
196-
}
197-
} catch (error) {
198-
if (widget.onError == null && context.mounted) {
199-
context.showErrorSnackBar(
200-
'${localization.unexpectedError}: $error',
201-
);
202-
} else {
203-
widget.onError?.call(error);
204-
}
205-
}
206-
setState(() {
207-
_phone.text = '';
208-
_password.text = '';
209-
});
210-
},
211165
),
212166
spacer(10),
213167
],

0 commit comments

Comments
 (0)