Skip to content

Commit

Permalink
fix: optional auth
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Nov 14, 2023
1 parent be00a3e commit ef08573
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions server/middlewares/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ export default function auth(options: AuthenticationOptions = {}) {
token = ctx.cookies.get("accessToken");
}

if (!token && options.optional !== true) {
throw AuthenticationError("Authentication required");
}
try {
if (!token) {
throw AuthenticationError("Authentication required");
}

let user: User | null;
let type: AuthenticationType;
let user: User | null;
let type: AuthenticationType;

if (token) {
if (ApiKey.match(String(token))) {
type = AuthenticationType.API;
let apiKey;
Expand Down Expand Up @@ -146,8 +146,12 @@ export default function auth(options: AuthenticationOptions = {}) {
getRootSpanFromRequestContext(ctx)
);
}
} else {
ctx.state.auth = {};
} catch (err) {
if (options.optional) {
ctx.state.auth = {};
} else {
throw err;
}
}

return next();
Expand Down

0 comments on commit ef08573

Please sign in to comment.