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') {
    if (!window.localStorage.containsKey('token')) {
      throw Exception(
          'Cannot revive token from localStorage - there is none.');
    }

    var token = json.decode(window.localStorage['token']!);
    credentials ??= {'token': token};
  }

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