-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_helper.dart
More file actions
160 lines (144 loc) · 4.49 KB
/
auth_helper.dart
File metadata and controls
160 lines (144 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// services/helpers/auth_helper.dart
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:flutter_timezone/flutter_timezone.dart';
import '../models/user_model.dart';
import 'package:task_management_app/main.dart';
class AuthHelper {
Future<UserModel?> login(String email, String password) async {
try {
final response = await supabase.auth.signInWithPassword(
email: email,
password: password,
);
final user = response.user;
if (user != null) {
final userId = user.id;
final userMetadata = user.userMetadata ?? {};
final String? username = userMetadata['username']?.toString();
final timezoneObj = await FlutterTimezone.getLocalTimezone();
final String currentTimezone = timezoneObj.toString();
final profileData = await supabase
.from('profile')
.upsert({
'id': userId,
if (username != null && username.isNotEmpty) 'username': username,
'timezone': currentTimezone,
})
.select()
.single();
return UserModel.fromJson(profileData, email);
}
return null;
} on AuthException catch (e) {
print('Supabase Auth Error: ${e.message}');
rethrow;
} catch (e) {
print('Unknown Error: $e');
rethrow;
}
}
Future<bool> loginWithGoogle() async {
try {
return await supabase.auth.signInWithOAuth(
OAuthProvider.google,
redirectTo: 'taskapp://login-callback', // back to app
);
} on AuthException catch (e) {
print('Lỗi đăng nhập Google: ${e.message}');
return false;
}
}
Future<bool> loginWithFacebook() async {
try {
return await supabase.auth.signInWithOAuth(
OAuthProvider.facebook,
redirectTo: 'taskapp://login-callback', // back to app
);
} on AuthException catch (e) {
print('Lỗi đăng nhập Facebook: ${e.message}');
return false;
}
}
Future<UserModel?> register(String email, String password, String username) async {
try {
final timezoneObj = await FlutterTimezone.getLocalTimezone();
final String currentTimezone = timezoneObj.toString();
final response = await supabase.auth.signUp(
email: email,
password: password,
data: {
'username': username,
'timezone': currentTimezone, // Data is already a safe String
}
);
final user = response.user;
if (user != null) {
if (response.session == null) {
// Email confirmation may be required; skip profile write until login.
return UserModel(
id: user.id,
email: email,
username: username,
timezone: currentTimezone,
);
}
final profileData = await supabase
.from('profile')
.upsert({
'id': user.id,
'username': username,
'timezone': currentTimezone,
})
.select()
.single();
return UserModel.fromJson(profileData, email);
}
return null;
} on AuthException catch (e) {
print('Supabase Sign Up Error: ${e.message}');
rethrow;
}
}
Future<void> sendPasswordReset(String email) async {
try {
await supabase.auth.resetPasswordForEmail(email);
} on AuthException catch (e) {
print('Lỗi Gửi OTP: ${e.message}');
rethrow;
}
}
Future<bool> verifyOTP(String email, String otpCode) async {
try {
// Authenticate OTP for recovery type
final response = await supabase.auth.verifyOTP(
email: email,
token: otpCode,
type: OtpType.recovery,
);
// Returns true if authentication is successful and session is created
return response.session != null;
} on AuthException catch (e) {
print('Lỗi Xác thực OTP: ${e.message}');
return false;
}
}
Future<void> updatePassword(String newPassword) async {
try {
// After successful verifyOTP, user is automatically logged in
// At this point, just call updateUser to change the password
await supabase.auth.updateUser(UserAttributes(password: newPassword));
} on AuthException catch (e) {
print('Lỗi Đổi Pass: ${e.message}');
rethrow;
}
}
}
// SignOut session
Future<void> signOut() async {
try {
await supabase.auth.signOut();
} catch (e) {
print('Lỗi đăng xuất: $e');
rethrow;
}
}