handleAuth method

Future handleAuth(
  1. WebSocketAction action,
  2. WebSocketContext socket
)

Authenticates a WebSocketContext.

Implementation

Future handleAuth(WebSocketAction action, WebSocketContext socket) async {
  if (allowAuth != false &&
      action.eventName == authenticateAction &&
      action.params['query'] is Map &&
      action.params['query']['jwt'] is String) {
    try {
      var auth = socket.request.container!.make<AngelAuth>();
      var jwt = action.params['query']['jwt'] as String;
      AuthToken token;

      token = AuthToken.validate(jwt, auth.hmac);
      var user = await auth.deserializer(token.userId);
      socket.request
        ..container!.registerSingleton<AuthToken>(token)
        ..container!.registerSingleton(user, as: user.runtimeType);
      socket._onAuthenticated.add(null);
      socket.send(authenticatedEvent,
          {'token': token.serialize(auth.hmac), 'data': user});
    } catch (e, st) {
      _log.severe('Authentication failed');
      catchError(e, st, socket);
    }
  } else {
    socket.sendError(AngelHttpException.badRequest(
        message: 'No JWT provided for authentication.'));
  }
}