Effect CommunityEC
Effect Community3y ago
4 replies
mepuka

Token decoding and user authentication process

const decodeToken = Http.response.schemaBodyJson(TokenResponse);
    const authenticateRequestBody = Http.request.schemaBody(
      UserLogin.pipe(S.omit("defaultAppId"))
    );
    const authenticateRequestClient = defaultClient.pipe(
      Http.client.mapRequest(Http.request.prependUrl(apiConfig.dev_api_url)),
      Http.client.mapRequest(Http.request.acceptJson),
      Http.client.mapRequest(
        Http.request.setHeader("some-header", apiConfig.default_app_id)
      )
    );

    const authenticate = (userLogin: UserLogin) => {
      const request = authenticateRequestBody(
        Http.request.post("/auth"),
        userLogin
      );

      return request.pipe(
        E.flatMap((request) => authenticateRequestClient(request)),
        E.flatMap((response) => {
          return decodeToken(response);
        }),
        E.tap((token) => makeTokenFile(token.data)),

        E.mapError((error) => new AuthError({ message: JSON.stringify(error) }))
      );
    };


In the above what would be the best way to catch a non ok response? I thought it would be passed through as a ResponseError but it's instead making it to the decodeToken where it gives a ParseError (if the token isn't returned from say a 401)
Was this page helpful?