loginUserWithToken method

Future<AnybillResult<void>> loginUserWithToken(
  1. {required String? accessToken,
  2. required String? refreshToken,
  3. String? expiresIn,
  4. String? tokenType}
)

Login the user with the token information.

Parameters:

  • accessToken : The access token used to authenticate to the API.
  • refreshToken : The refresh token used to re-authenticate.
  • expiresIn : Lifetime used to check if the current token is still valid. Currently unused.
  • tokenType : Type of the access token. Defaults to 'bearer'.

Returns a AnybillResult.success that contains the status code and the user information in its data parameter.

Throws a AnybillResult.failure with the specified information gained from the API. This could be a DioException when a generic errors occurred or a different Exception for critical errors that couldn't be caught.

Implementation

Future<AnybillResult<void>> loginUserWithToken({
  /// Access token from the user
  required String? accessToken,

  /// Refresh token from the user
  required String? refreshToken,

  /// Time in seconds until the access token expires.
  String? expiresIn,

  /// Type of the access token. Defaults to 'bearer'.
  String? tokenType,
}) async {
  AnybillLogger.info("Tried to login user with token");
  try {
    await _tokenProvider.saveToken(
      accessToken: accessToken,
      refreshToken: refreshToken,
      expiresIn: expiresIn,
      tokenType: tokenType,
    );
    // Make a request to check if the token information saved here is valid
    final userValid = await getUserInformation();

    if (userValid is Failure<UserInformationDto>) {
      await _tokenProvider.deleteAuthInformation();

      return AnybillResult.failure(
        code: userValid.code,
        type: userValid.type,
      );
    }

    return AnybillResult.success(HttpStatus.ok);
  }
  // ----------------------------------------------------------------
  catch (error, stacktrace) {
    AnybillLogger.error(
      error: error,
      stacktrace: stacktrace,
      library: "AuthProvider",
      event: "loginUserWithToken",
    );
    return AnybillResult.failure(
      code: HttpStatus.internalServerError,
      type: AnybillErrorType.networkError,
    );
  }
}