updated packages, did things and maybe finally fixed login issue

This commit is contained in:
Kima
2024-11-12 23:27:14 +01:00
parent 939761695f
commit 2dafe5ed02
12 changed files with 258 additions and 22 deletions

View File

@@ -68,6 +68,8 @@ Future loginAPI({
gradeDelay: 0,
),
role: Role.parent,
accessToken: '',
accessTokenExpire: DateTime.now(),
refreshToken: '',
);
@@ -154,6 +156,8 @@ Future loginAPI({
name: student.name,
student: student,
role: JwtUtils.getRoleFromJWT(res["access_token"])!,
accessToken: res["access_token"],
accessTokenExpire: DateTime.now(),
refreshToken: '',
);
@@ -235,6 +239,15 @@ Future newLoginAPI({
if (res != null) {
if (kDebugMode) {
print(res);
// const splitSize = 1000;
// RegExp exp = RegExp(r"\w{" "$splitSize" "}");
// // String str = "0102031522";
// Iterable<Match> matches = exp.allMatches(res.toString());
// var list = matches.map((m) => m.group(0));
// list.forEach((e) {
// print(e);
// });
}
if (res.containsKey("error")) {
@@ -267,6 +280,9 @@ Future newLoginAPI({
name: student.name,
student: student,
role: role,
accessToken: res["access_token"],
accessTokenExpire:
DateTime.now().add(Duration(seconds: (res["expires_in"] - 30))),
refreshToken: res["refresh_token"],
);

View File

@@ -40,6 +40,12 @@ Future<void> syncAll(BuildContext context) {
StatusProvider statusProvider =
Provider.of<StatusProvider>(context, listen: false);
// check if access token isn't expired
// if (user.user?.accessToken == null) {
// lock = false;
// return Future.value();
// }
List<Future<void>> tasks = [];
int taski = 0;
@@ -50,6 +56,25 @@ Future<void> syncAll(BuildContext context) {
}
tasks = [
// refresh login
syncStatus(() async {
print(user.user?.accessTokenExpire);
if (user.user == null) return;
if (user.user!.accessTokenExpire.isBefore(DateTime.now())) {
String authRes = await Provider.of<KretaClient>(context, listen: false)
.refreshLogin() ??
'';
if (authRes != 'success') {
print('ERROR: failed to refresh login');
lock = false;
return Future.value();
}
} else {
print('INFO: access token is not expired');
}
}()),
syncStatus(Provider.of<GradeProvider>(context, listen: false).fetch()),
syncStatus(Provider.of<TimetableProvider>(context, listen: false)
.fetch(week: Week.current())),