authenticate method

  1. @override
Future<AngelAuthResult> authenticate({
  1. String? type,
  2. dynamic credentials,
  3. String authEndpoint = '/auth',
})
override

Authenticates against the server.

This is designed with package:angel_auth in mind.

The type is appended to the authEndpoint, ex. local becomes /auth/local.

The given credentials are sent to server as-is; the request body is sent as JSON.

Implementation

@override
Future<AngelAuthResult> authenticate(
    {String? type, credentials, String authEndpoint = '/auth'}) async {
  if (type == null || type == 'token') {
    var storedToken = window.localStorage.getItem('token');
    if (storedToken == null) {
      throw Exception(
          'Cannot revive token from localStorage - there is none.');
    }

    var token = json.decode(storedToken);
    credentials ??= {'token': token};
  }

  final result = await super.authenticate(
      type: type, credentials: credentials, authEndpoint: authEndpoint);
  window.localStorage.setItem('token', json.encode(authToken = result.token));
  window.localStorage.setItem('user', json.encode(result.data));
  return result;
}