login method Null safety
Gets the JWT and refresh token through the API
Will throw an Exception if the password and user do not match OR API returns a 400
var client = MDClient()
client.login('myuser','mypassword')
Implementation
Future<void> login(String username, String password) async {
var res = await http.post(Uri.parse('https://api.mangadex.org/auth/login'),
body: '{"username":"$username","password":"$password"}',
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.userAgentHeader: 'mangadex_dart_api/1.0'
});
if (res.statusCode == 401) {
throw 'User and Password does not match';
} else if (res.statusCode == 400) {
throw 'Bad request';
}
var data = jsonDecode(res.body);
if (res.statusCode == 403 && res.headers['X-Captcha-Sitekey'] != null) {
throw CaptchaException(res.headers['X-Captcha-Sitekey'].toString(),
message:
'You need to solve a captcha, check `.sitekey` for the sitekey.');
}
token = data['token']['session'];
refresh = data['token']['refresh'];
}