login function

String login({
  1. required Entry authDatabase,
  2. required String username,
  3. required String password,
})

Implementation

String login({
  required Entry authDatabase,
  required String username,
  required String password,
}){
  DbObject? account = _getAccount(
    authDatabase: authDatabase,
    username: username,
  );
  if(account == null){
    throw "No account found asociated with the username $username.";
  }else{
    //Create access token if password is a match
    if(password == account.view()["password"]){
      List<String> accessToken = authDatabase.select().insert(
        key: "accessTokens",
        value: [
          {
            "accountUUID": account.uuid,
            //TODO: Implement auto removal when expired in the future.
            "creation": DateTime.now().toUtc().toString(),
          },
        ],
      );
      //Create a reference to link, find and delete when needed.
      account.insert(
        key: "accessTokens",
        value: [
          accessToken.first,
        ],
      );
      return accessToken.first;
    }else{
      throw "Incorrect credentials.";
    }
  }
}