loginWithCredentials function
Implementation
Future<void> loginWithCredentials(String email, String password) async {
Dio dio = Dio();
final String authUrl = Configuration.AuthUrl;
final String url = '$authUrl/login';
try {
final response = await dio.post(
url,
data: {
'email': email,// change this to username
'password': password,
},
);
if (response.statusCode == 200) {
final String accessToken = response.data['accessToken'];
if (accessToken.isNotEmpty) {
final FlutterSecureStorage secureStorage = FlutterSecureStorage();
await secureStorage.write(key: "opt_access_token", value: accessToken);
} else {
throw 'Invalid access token';
}
} else {
throw response.data['message'] ?? 'Registration failed';
}
} on DioException catch (e) {
String errorMessage = 'Error during login';
if (e.response != null && e.response?.data != null) {
errorMessage = e.response?.data['message'] ?? errorMessage;
}
throw errorMessage; // Throw only the error message
} catch (e) {
throw 'Error during login'; // Catch any other unknown errors
}
}