login method

Future<Student?> login({
  1. bool autologin = false,
})

Implementation

Future<Student?> login({bool autologin = false}) async {
  try {
    String loginUrl = "https://www.lectio.dk/lectio/$gymId/login.aspx";
    var loginGet = await request<String>(loginUrl);
    await clearCookies();
    BeautifulSoup bs = BeautifulSoup(loginGet.data as String);
    Map<String, String?> extracted =
        extractASPData(bs, "m\$Content\$submitbtn2");

    extracted["m\$Content\$username"] = username;
    extracted["m\$Content\$password"] = password;
    if (autologin) {
      extracted[r'm$Content$AutologinCbx'] = "on";
    }
    await request<String>(loginUrl,
        data: extracted,
        options: Options(
            followRedirects: false,
            method: "POST",
            contentType: "application/x-www-form-urlencoded",
            headers: {
              "Cache-Control": "no-cache",
              "Referer": "https://www.lectio.dk"
            })).timeout(const Duration(seconds: 5));
    await addCookies(
        Uri.https("www.lectio.dk"), [Cookie("isloggedin3", "Y")]);

    var forsideSoup = await request<String>(
      "https://www.lectio.dk/lectio/$gymId/forside.aspx",
    );

    var student =
        checkLoggedIn(BeautifulSoup(forsideSoup.data as String), gymId);
    if (student == null) {
      throw InvalidCredentialsError();
    }
    setAutologin();
    return student;
  } catch (e) {
    return null;
  }
}