auth<T> function

Middleware auth<T>(
  1. Auth<T> auth
)

auth middleware allow you to hook to get token, apiKey or something else in order to check if credential are correct. The function that check if credential are correct return a record that look like this (bool, T) where T is defined by you.

example on how to use it: (bool, Identifiable) apiKeyAuth(Request request, String value) { return (value == 'GOOGLE', Identifiable('GOOGLE')); }

addMiddleware( auth(ApiKeyAuth(apiKeyAuth)), )

Implementation

Middleware auth<T>(Auth<T> auth) {
  return (handler) {
    return (request) async {
      final (authorized, data) = await auth.authenticate(request);
      if (authorized) {
        if (data == null) {
          return handler(request);
        }
        return handler(request.set<T>(() => data));
      }
      return Response.unauthorized('');
    };
  };
}