loginWithEmailPassword static method
Authenticate with email and password
Implementation
static Future<AuthConfig> loginWithEmailPassword({
required String email,
required String password,
required String supabaseUrl,
required String supabaseAnonKey,
}) async {
final url = Uri.parse('$supabaseUrl/auth/v1/token?grant_type=password');
final response = await http.post(
url,
headers: {
'apikey': supabaseAnonKey,
'Content-Type': 'application/json',
},
body: jsonEncode({
'email': email,
'password': password,
}),
);
if (response.statusCode != 200) {
final errorBody = jsonDecode(response.body) as Map<String, dynamic>;
final errorMessage = errorBody['error_description'] as String? ??
errorBody['message'] as String? ??
'Authentication failed';
throw Exception(errorMessage);
}
final data = jsonDecode(response.body) as Map<String, dynamic>;
final accessToken = data['access_token'] as String;
final refreshToken = data['refresh_token'] as String?;
final expiresIn = data['expires_in'] as int? ?? 3600;
final user = data['user'] as Map<String, dynamic>?;
final expiresAt = DateTime.now().add(Duration(seconds: expiresIn));
return AuthConfig(
type: AuthType.jwt,
token: accessToken,
refreshToken: refreshToken,
expiresAt: expiresAt,
user: user != null
? UserInfo(
email: user['email'] as String? ?? email,
userId: user['id'] as String? ?? '',
)
: null,
);
}