login method
Initiates the login process using Spotify's OAuth2 authentication.
Implementation
Future<void> login() async {
// Request authorization code from Spotify
final authResponse = await client.requestAuthorization(
clientId: clientId,
);
// Extract authorization code from response
final authCode = authResponse.code;
// Request access token and refresh token using the authorization code
final tokenResponse = await client.requestAccessToken(
code: authCode.toString(),
clientId: clientId,
clientSecret: clientSecret,
);
// Store refresh token
refreshToken = tokenResponse.refreshToken;
// Store OAuth credentials in secure storage
final credentails = {
'token': tokenResponse.accessToken,
'refresh_token': tokenResponse.refreshToken,
'expires_in': tokenResponse.expiresIn,
'created_at': DateTime.now().millisecondsSinceEpoch,
};
await storage.write(
key: 'credentials',
value: jsonEncode(credentails),
);
// Update login status and access token
_loggedIn = true;
accessToken = tokenResponse.accessToken;
// Reconfigure Dio with the new access token
_dio = Dio(
BaseOptions(
headers: {
'Authorization': 'Bearer $accessToken',
},
),
);
}