-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
259 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text eol=lf |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,28 @@ | ||
import { | ||
Strategy as JwtStrategy, | ||
ExtractJwt, | ||
StrategyOptions, | ||
} from 'passport-jwt' | ||
import { prisma } from '../utils' | ||
|
||
const options: StrategyOptions = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: process.env.JWT_SECRET || 'your_jwt_secret', | ||
} | ||
|
||
export const jwtStrategy = new JwtStrategy( | ||
options, | ||
async (jwtPayload, done) => { | ||
try { | ||
const user = await prisma.user.findUnique({ | ||
where: { id: jwtPayload.id }, | ||
}) | ||
if (!user) { | ||
throw new Error('User not found') | ||
} | ||
return done(null, user) | ||
} catch (error) { | ||
return done(error, false) | ||
} | ||
}, | ||
) |
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,24 @@ | ||
import { NextFunction, Request, Response } from 'express' | ||
import passport from 'passport' | ||
import { errorResponse } from '../utils' | ||
|
||
const verifyCallback = | ||
(req: Request, res: Response, next: NextFunction) => | ||
async (err: unknown, user: Express.User | null) => { | ||
if (err) { | ||
return errorResponse(res, err, 500, 'Unauthorized') | ||
} | ||
if (!user) { | ||
return errorResponse(res, err, 401, 'Unauthorized') | ||
} | ||
req.user = user | ||
next() | ||
} | ||
|
||
export const auth = (req: Request, res: Response, next: NextFunction) => { | ||
passport.authenticate( | ||
'jwt', | ||
{ session: false }, | ||
verifyCallback(req, res, next), | ||
)(req, res, next) | ||
} |
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.