-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement JWT authentication with access & refresh tokens, OTP handli…
…ng, and auth middleware
- Loading branch information
Showing
16 changed files
with
210 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import ITokenService from "../../interface/services/ITokenService"; | ||
import jwt, { JwtPayload } from "jsonwebtoken"; | ||
|
||
export default class TokenService implements ITokenService { | ||
private signToken(payload:object,secret:string,expiresIn:string):string{ | ||
return jwt.sign(payload,secret,{expiresIn}); | ||
} | ||
private verifyToken(token:string,secret:string):JwtPayload{ | ||
try { | ||
return jwt.verify(token,secret) as JwtPayload | ||
} catch (error) { | ||
throw new Error("Invalid token") | ||
} | ||
} | ||
|
||
createRefreshToken(email: string, id: string): string { | ||
return this.signToken({ email, id }, process.env.REFRESH_TOKEN_SECRET!, "7d"); | ||
} | ||
|
||
verifyRefreshToken(token: string): { email: string; id: string } { | ||
const decoded = this.verifyToken(token, process.env.REFRESH_TOKEN_SECRET!); | ||
return { email: decoded.email, id: decoded.id }; | ||
} | ||
|
||
createAccessToken(email: string, id: string): string { | ||
return this.signToken({ email, id }, process.env.ACCESS_TOKEN_SECRET!, "15m"); | ||
} | ||
|
||
verifyAccessToken(token: string): { email: string; id: string } { | ||
const decoded = this.verifyToken(token, process.env.ACCESS_TOKEN_SECRET!); | ||
return { email: decoded.email, id: decoded.id }; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IPatient } from "../../domain/entities/Patient"; | ||
|
||
export default interface ITokenService { | ||
createRefreshToken(email: string, id: string): string; | ||
verifyRefreshToken(token: string): { email: string; id: string }; | ||
createAccessToken(email: string, id: string): string; | ||
verifyAccessToken(token: string): { email: string; id: string }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
server/src/presentation/middlewares/PatientAuthMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NextFunction, Response } from "express"; | ||
import ITokenService from "../../interface/services/ITokenService"; | ||
import { CustomRequest } from "../../types"; | ||
|
||
export default class PatientAuthMiddleware { | ||
constructor(private tokenService: ITokenService) {} | ||
|
||
exec = (req: CustomRequest, res: Response, next: NextFunction) => { | ||
try { | ||
const authHeader = req.headers.authorization || req.headers.Authorization; | ||
const tokenString = Array.isArray(authHeader) ? authHeader[0] : authHeader; | ||
|
||
if (!tokenString?.startsWith("Bearer ")) { | ||
return res.status(401).json({ message: "Unauthorized: No or invalid token provided" }); | ||
} | ||
|
||
const token = tokenString.split(" ")[1]; | ||
|
||
if (!token) { | ||
return res.status(401).json({ message: "Unauthorized: Token is missing" }); | ||
} | ||
|
||
const decodedToken = this.tokenService.verifyAccessToken(token); | ||
req.patient = { | ||
email: decodedToken.email, | ||
id: decodedToken.id, | ||
}; | ||
next(); | ||
} catch (error) { | ||
res.status(401).json({ message: "Forbidden" }); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
server/src/presentation/routers/patient/PatientAuthRoutes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import express from "express"; | ||
import PatientRepository from "../../../infrastructure/database/repositories/PatientRepository"; | ||
import PasswordService from "../../../infrastructure/services/PasswordService"; | ||
import RegisterPatientUseCase from "../../../use_case/patient/RegisterPatientUseCase"; | ||
import PatientController from "../../controllers/PatientController"; | ||
import LoginPatientUseCase from "../../../use_case/patient/LoginPatientUseCase"; | ||
import EmailService from "../../../infrastructure/services/EmailService"; | ||
import OtpRepository from "../../../infrastructure/database/repositories/OtpRepository"; | ||
import TokenService from "../../../infrastructure/services/TokenService"; | ||
import PatientAuthMiddleware from "../../middlewares/PatientAuthMiddleware"; | ||
|
||
const route = express(); | ||
|
||
const emailService = new EmailService(); | ||
const tokenService = new TokenService(); | ||
const otpRepository = new OtpRepository(); | ||
const passwordService = new PasswordService(); | ||
const patientRepository = new PatientRepository(); | ||
const registerPatientUseCase = new RegisterPatientUseCase(patientRepository, passwordService); | ||
const loginPatientUseCase = new LoginPatientUseCase(patientRepository, passwordService, emailService, otpRepository,tokenService); | ||
const patientController = new PatientController(registerPatientUseCase, loginPatientUseCase); | ||
|
||
const patientAuthMiddleWare = new PatientAuthMiddleware (tokenService); | ||
|
||
route.post("/", (req, res, next) => { | ||
patientController.register(req, res, next); | ||
}); | ||
route.post("/login", (req, res, next) => { | ||
patientController.login(req, res, next); | ||
}); | ||
route.post("/otp-verification", (req, res, next) => { | ||
patientController.validateOtp(req, res, next); | ||
}); | ||
route.get("/refresh",(req,res,next)=>{ | ||
patientController.refreshAccessToken(req,res,next); | ||
}); | ||
route.post('/logout',patientAuthMiddleWare.exec,(req,res,next)=>{ | ||
patientController.logout(req,res,next) | ||
}); | ||
|
||
|
||
export default route; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Request } from "express"; | ||
|
||
export interface CustomRequest extends Request { | ||
patient?: { | ||
email: string; | ||
id: string; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
23d59ce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
avm-care – ./
avm-care-sinans-projects-8d312afe.vercel.app
avm-care.vercel.app
avm-care-git-main-sinans-projects-8d312afe.vercel.app