Effect CommunityEC
Effect Community4w ago
1 reply
branperr

Trouble Decoding Redacted Token in Auth Middleware

Edit: I found the issue. I was accidentally attaching an object instead of a string to the header, so it didn't have anything to do with the Effect code.



I'm trying to use the Auth middleware, but I can't seem to decode the redacted token. When I try to get the value, it comes out as [object Object] in the terminal. Any idea as to what might cause this? It's in a node server.

Here's a basic outline of the code:

export class AuthUser extends Context.Tag("AuthUser")<
  AuthUser,
  typeof api.Session.Type
>() {}

export class AuthMiddleware extends HttpApiMiddleware.Tag<AuthMiddleware>()(
  "AuthMiddleware",
  {
    failure: Unauthorized,
    provides: AuthUser,
    security: {
      validateSessionToken: HttpApiSecurity.bearer,
    },
    optional: false,
  },
) {}

const AuthLive = Layer.effect(
  AuthMiddleware,
  Effect.gen(function* () {
    yield* Effect.log("creating Authorization middleware");

    return {
      validateSessionToken: (bearerToken) =>
        Effect.gen(function* () {
          // bearerToken itself logs a <redacted>
          // the value below logs as [object Object]
          yield* Effect.log(
            "checking bearer token",
            Redacted.value(bearerToken),
          );
          return {...};
        }),
    };
  }),
);

// how the token is attached on the client just in case

const client = yield* HttpApiClient.make(MyAPI, {
      baseUrl: url,
      /**
       * Add headers to client
       */
      transformClient: (client) =>
        HttpClient.mapRequest(client, (request) =>
          HttpClientRequest.setHeaders(request, {
            Authorization: `Bearer ${token}`,
          }),
        ),
    });
Was this page helpful?