Skip to content

Commit

Permalink
updated middleware for code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
AydanPirani committed Aug 17, 2023
1 parent 70c4be0 commit 0d2fd2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/middleware/select-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ const githubOptions: AuthenticateOptions = {session: false};


export function SelectAuthProvider(provider: string): RequestHandler {

console.log("|%s|", provider);
if (provider == "google") {
return authenticateFunction("google", googleOptions);
}

if (provider == "github") {
return authenticateFunction("github", githubOptions);
switch (provider) {
case "google": return authenticateFunction("google", googleOptions);
case "github": return authenticateFunction("github", githubOptions);
}

throw new Error("Provider not found!");
Expand Down
12 changes: 10 additions & 2 deletions src/middleware/verify-jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response, NextFunction} from "express";

import Constants from "../constants.js";
import { decodeJwtToken } from "../services/auth/auth-lib.js";
import jsonwebtoken from "jsonwebtoken";

export function verifyJwt(req: Request, res: Response, next: NextFunction): void {
const token: string | undefined = req.headers.authorization;
Expand All @@ -15,7 +16,14 @@ export function verifyJwt(req: Request, res: Response, next: NextFunction): void
res.locals.payload = decodeJwtToken(token);
next();
} catch (error) {
res.status(Constants.FORBIDDEN).send({error: error as string});
next("route");
if (error instanceof jsonwebtoken.TokenExpiredError) {
console.log("token expired!");
res.status(Constants.FORBIDDEN).send("TOKEN EXPIRED");
next("router");
} else {
console.log(error);
res.status(Constants.FORBIDDEN).send({error: error as string});
next("router");
}
}
}

0 comments on commit 0d2fd2c

Please sign in to comment.