diff --git a/filcnaplo/lib/api/client.dart b/filcnaplo/lib/api/client.dart index bf290be..0a78545 100644 --- a/filcnaplo/lib/api/client.dart +++ b/filcnaplo/lib/api/client.dart @@ -53,9 +53,14 @@ class FilcAPI { .map((json) => School.fromJson(json)) .toList(); schools.add(School( - city: "Tiszabura", - instituteCode: "supporttest-reni-tiszabura-teszt01", - name: "FILC Éles Reni tiszabura-teszt", + city: "Stockholm", + instituteCode: "refilc-test-sweden", + name: "reFilc Test SE - Leo Ekström High School", + )); + schools.add(School( + city: "Madrid", + instituteCode: "refilc-test-spain", + name: "reFilc Test ES - Emilio Obrero University", )); return schools; } else { diff --git a/filcnaplo/lib/api/login.dart b/filcnaplo/lib/api/login.dart index 4afc7ce..0b9543e 100644 --- a/filcnaplo/lib/api/login.dart +++ b/filcnaplo/lib/api/login.dart @@ -1,6 +1,7 @@ // ignore_for_file: avoid_print, use_build_context_synchronously import 'package:filcnaplo/utils/jwt.dart'; +import 'package:filcnaplo_kreta_api/models/school.dart'; import 'package:filcnaplo_kreta_api/providers/absence_provider.dart'; import 'package:filcnaplo_kreta_api/providers/event_provider.dart'; import 'package:filcnaplo_kreta_api/providers/exam_provider.dart'; @@ -20,6 +21,7 @@ import 'package:filcnaplo_kreta_api/models/week.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:filcnaplo/api/nonce.dart'; +import 'package:uuid/uuid.dart'; enum LoginState { missingFields, @@ -47,87 +49,139 @@ Future loginAPI({ void Function(User)? onLogin, void Function()? onSuccess, }) async { - Provider.of(context, listen: false).userAgent = - Provider.of(context, listen: false).config.userAgent; + Future testLogin(School school) async { + var user = User( + username: username, + password: password, + instituteCode: instituteCode, + name: 'Teszt Lajos', + student: Student( + birth: DateTime.now(), + id: const Uuid().v4(), + name: 'Teszt Lajos', + school: school, + yearId: '1', + parents: ['Teszt András', 'Teszt Linda'], + ), + role: Role.parent, + ); - Map headers = { - "content-type": "application/x-www-form-urlencoded", - }; + if (onLogin != null) onLogin(user); - String nonceStr = await Provider.of(context, listen: false) - .getAPI(KretaAPI.nonce, json: false); + // store test user in db + await Provider.of(context, listen: false) + .store + .storeUser(user); + Provider.of(context, listen: false).addUser(user); + Provider.of(context, listen: false).setUser(user.id); - Nonce nonce = getNonce(nonceStr, username, instituteCode); - headers.addAll(nonce.header()); + if (onSuccess != null) onSuccess(); - Map? res = await Provider.of(context, listen: false) - .postAPI(KretaAPI.login, - headers: headers, - body: User.loginBody( - username: username, - password: password, - instituteCode: instituteCode, - )); - if (res != null) { - if (res.containsKey("error")) { - if (res["error"] == "invalid_grant") { - return LoginState.invalidGrant; - } - } else { - if (res.containsKey("access_token")) { - try { - Provider.of(context, listen: false).accessToken = - res["access_token"]; - Map? studentJson = - await Provider.of(context, listen: false) - .getAPI(KretaAPI.student(instituteCode)); - Student student = Student.fromJson(studentJson!); - var user = User( - username: username, - password: password, - instituteCode: instituteCode, - name: student.name, - student: student, - role: JwtUtils.getRoleFromJWT(res["access_token"])!, - ); + return LoginState.success; + } - if (onLogin != null) onLogin(user); + // if institute matches one of test things do test login + if (instituteCode == 'refilc-test-sweden') { + School school = School( + city: "Stockholm", + instituteCode: "refilc-test-sweden", + name: "reFilc Test SE - Leo Ekström High School", + ); - // Store User in the database - await Provider.of(context, listen: false) - .store - .storeUser(user); - Provider.of(context, listen: false).addUser(user); - Provider.of(context, listen: false).setUser(user.id); + await testLogin(school); + } else if (instituteCode == 'refilc-test-spain') { + School school = School( + city: "Madrid", + instituteCode: "refilc-test-spain", + name: "reFilc Test ES - Emilio Obrero University", + ); - // Get user data + await testLogin(school); + } else { + // normal login from here + Provider.of(context, listen: false).userAgent = + Provider.of(context, listen: false).config.userAgent; + + Map headers = { + "content-type": "application/x-www-form-urlencoded", + }; + + String nonceStr = await Provider.of(context, listen: false) + .getAPI(KretaAPI.nonce, json: false); + + Nonce nonce = getNonce(nonceStr, username, instituteCode); + headers.addAll(nonce.header()); + + Map? res = await Provider.of(context, listen: false) + .postAPI(KretaAPI.login, + headers: headers, + body: User.loginBody( + username: username, + password: password, + instituteCode: instituteCode, + )); + if (res != null) { + if (res.containsKey("error")) { + if (res["error"] == "invalid_grant") { + return LoginState.invalidGrant; + } + } else { + if (res.containsKey("access_token")) { try { - await Future.wait([ - Provider.of(context, listen: false).fetch(), - Provider.of(context, listen: false) - .fetch(week: Week.current()), - Provider.of(context, listen: false).fetch(), - Provider.of(context, listen: false).fetch(), - Provider.of(context, listen: false).fetchAll(), - Provider.of(context, listen: false).fetch(), - Provider.of(context, listen: false).fetch(), - Provider.of(context, listen: false).fetch(), - ]); + Provider.of(context, listen: false).accessToken = + res["access_token"]; + Map? studentJson = + await Provider.of(context, listen: false) + .getAPI(KretaAPI.student(instituteCode)); + Student student = Student.fromJson(studentJson!); + var user = User( + username: username, + password: password, + instituteCode: instituteCode, + name: student.name, + student: student, + role: JwtUtils.getRoleFromJWT(res["access_token"])!, + ); + + if (onLogin != null) onLogin(user); + + // Store User in the database + await Provider.of(context, listen: false) + .store + .storeUser(user); + Provider.of(context, listen: false).addUser(user); + Provider.of(context, listen: false).setUser(user.id); + + // Get user data + try { + await Future.wait([ + Provider.of(context, listen: false).fetch(), + Provider.of(context, listen: false) + .fetch(week: Week.current()), + Provider.of(context, listen: false).fetch(), + Provider.of(context, listen: false).fetch(), + Provider.of(context, listen: false).fetchAll(), + Provider.of(context, listen: false).fetch(), + Provider.of(context, listen: false).fetch(), + Provider.of(context, listen: false).fetch(), + ]); + } catch (error) { + print("WARNING: failed to fetch user data: $error"); + } + + if (onSuccess != null) onSuccess(); + + return LoginState.success; } catch (error) { - print("WARNING: failed to fetch user data: $error"); + print("ERROR: loginAPI: $error"); + // maybe check debug mode + // ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("ERROR: $error"))); + return LoginState.failed; } - - if (onSuccess != null) onSuccess(); - - return LoginState.success; - } catch (error) { - print("ERROR: loginAPI: $error"); - // maybe check debug mode - // ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("ERROR: $error"))); - return LoginState.failed; } } } } + return LoginState.failed; }