1+ // data/helpers/auth_helper.dart
2+
3+ import 'package:supabase_flutter/supabase_flutter.dart' ;
4+ import 'package:flutter_timezone/flutter_timezone.dart' ;
5+ import '../models/user_model.dart' ;
6+ import 'package:task_management_app/main.dart' ;
7+
8+ class AuthHelper {
9+
10+
11+ Future <UserModel ?> login (String email, String password) async {
12+ try {
13+ final response = await supabase.auth.signInWithPassword (
14+ email: email,
15+ password: password,
16+ );
17+
18+ if (response.user != null ) {
19+ final userId = response.user! .id;
20+
21+ final timezoneObj = await FlutterTimezone .getLocalTimezone ();
22+ final String currentTimezone = timezoneObj.toString ();
23+
24+ final profileData = await supabase
25+ .from ('profile' )
26+ .update ({'timezone' : currentTimezone})
27+ .eq ('id' , userId)
28+ .select ()
29+ .single ();
30+
31+ return UserModel .fromJson (profileData, email);
32+ }
33+ return null ;
34+ } on AuthException catch (e) {
35+ print ('Supabase Auth Error: ${e .message }' );
36+ rethrow ;
37+ } catch (e) {
38+ print ('Unknown Error: $e ' );
39+ rethrow ;
40+ }
41+ }
42+
43+ Future <UserModel ?> register (String email, String password, String username) async {
44+ try {
45+ final timezoneObj = await FlutterTimezone .getLocalTimezone ();
46+ final String currentTimezone = timezoneObj.toString ();
47+ final response = await supabase.auth.signUp (
48+ email: email,
49+ password: password,
50+ data: {
51+ 'username' : username,
52+ 'timezone' : currentTimezone, // Data is already a safe String
53+ }
54+ );
55+
56+ if (response.user != null ) {
57+ final userId = response.user! .id;
58+
59+ // Step 2: Insert directly into the 'profile' table
60+ final profileData = await supabase
61+ .from ('profile' )
62+ .select ()
63+ .eq ('id' , userId)
64+ .single ();
65+
66+ return UserModel .fromJson (profileData, email);
67+ }
68+ return null ;
69+ } on AuthException catch (e) {
70+ print ('Supabase Sign Up Error: ${e .message }' );
71+ rethrow ;
72+ }
73+ }
74+
75+ Future <void > sendPasswordReset (String email) async {
76+ try {
77+ await supabase.auth.resetPasswordForEmail (email);
78+ } on AuthException catch (e) {
79+ print ('Lỗi Gửi OTP: ${e .message }' );
80+ rethrow ;
81+ }
82+ }
83+
84+ Future <bool > verifyOTP (String email, String otpCode) async {
85+ try {
86+ // Authenticate OTP for recovery type
87+ final response = await supabase.auth.verifyOTP (
88+ email: email,
89+ token: otpCode,
90+ type: OtpType .recovery,
91+ );
92+ // Returns true if authentication is successful and session is created
93+ return response.session != null ;
94+ } on AuthException catch (e) {
95+ print ('Lỗi Xác thực OTP: ${e .message }' );
96+ return false ;
97+ }
98+ }
99+ Future <void > updatePassword (String newPassword) async {
100+ try {
101+ // After successful verifyOTP, user is automatically logged in
102+ // At this point, just call updateUser to change the password
103+ await supabase.auth.updateUser (UserAttributes (password: newPassword));
104+ } on AuthException catch (e) {
105+ print ('Lỗi Đổi Pass: ${e .message }' );
106+ rethrow ;
107+ }
108+ }
109+ }
0 commit comments