refreshToken static method
Refresh access token using refresh token
Implementation
static Future<AuthConfig> refreshToken({
required String refreshToken,
required String supabaseUrl,
required String supabaseAnonKey,
}) async {
final url =
Uri.parse('$supabaseUrl/auth/v1/token?grant_type=refresh_token');
final response = await http.post(
url,
headers: {
'apikey': supabaseAnonKey,
'Content-Type': 'application/json',
},
body: jsonEncode({
'refresh_token': refreshToken,
}),
);
if (response.statusCode != 200) {
throw Exception('Token refresh failed');
}
final data = jsonDecode(response.body) as Map<String, dynamic>;
final accessToken = data['access_token'] as String;
final newRefreshToken = data['refresh_token'] as String? ?? refreshToken;
final expiresIn = data['expires_in'] as int? ?? 3600;
final user = data['user'] as Map<String, dynamic>?;
final expiresAt = DateTime.now().add(Duration(seconds: expiresIn));
// Get existing auth to preserve user info
final existingConfig = ConfigManager.loadConfig();
final existingUser = existingConfig?.auth?.user;
return AuthConfig(
type: AuthType.jwt,
token: accessToken,
refreshToken: newRefreshToken,
expiresAt: expiresAt,
user: user != null
? UserInfo(
email: user['email'] as String? ?? existingUser?.email ?? '',
userId: user['id'] as String? ?? existingUser?.userId ?? '',
)
: existingUser,
);
}