Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
init(api: ApiContainer) {
const notAuthenticated = new ApiErrorResponse(
{},
"You are not authenticated!",
"WBY_NOT_AUTHENTICATED",
401
);
/**
* Identity profile
*/
api
.get("Auth.Me", "/me", async ({ req }) => {
if (!req.identity) {
return notAuthenticated;
}
return new ApiResponse(await req.identity.toJSON(req.query._fields));
})
const error = `"expiresOn" function must be configured for "${strategy}" strategy!`;
invariant(typeof expiresOn === "function", error);
let expiration = expiresOn(req);
if (expiration instanceof Date) {
expiration = Math.floor(expiration.getTime() / 1000);
}
return new ApiResponse({
token: await authentication.createToken(identity, expiration),
identity: await identity.toJSON(req.query._fields),
expiresOn: expiration
});
} catch (e) {
const response = new ApiErrorResponse({}, e.message);
if (e instanceof AuthenticationError) {
response.errorCode = "WBY_INVALID_CREDENTIALS";
response.statusCode = 401;
} else {
response.errorCode = "WBY_INTERNAL_ERROR";
response.statusCode = 500;
}
return response;
}
})
.setPublic();