-
Notifications
You must be signed in to change notification settings - Fork 2
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
7 changed files
with
143 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Request as ExpressRequest, Response, NextFunction } from 'express' | ||
import jwt, { JwtPayload } from 'jsonwebtoken' | ||
import { USER_SCHEMA } from '@/models/user_model' | ||
import { log } from 'console' | ||
|
||
interface User { | ||
name?: string | ||
email: string | ||
password: string | ||
} | ||
|
||
interface Request extends ExpressRequest { | ||
user?: User | ||
} | ||
|
||
export async function protect_route( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) { | ||
try { | ||
const token = req.cookies.token | ||
if (!token) { | ||
res.status(401).json({ error: 'Unauthorized' }) | ||
return | ||
} | ||
const secret = process.env.JWT_SECRET | ||
if (!secret) { | ||
res.status(500).json({ error: 'JWT secret is undefined' }) | ||
return | ||
} | ||
const decoded = jwt.verify(token, secret) as JwtPayload | ||
req.user = (await USER_SCHEMA.findById(decoded.user_id).select( | ||
'-password' | ||
)) as User | ||
|
||
next() | ||
} catch (error) { | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} |
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,24 @@ | ||
export {} | ||
|
||
declare global { | ||
namespace Express { | ||
export interface Request { | ||
job?: { | ||
id: string | ||
title: string | ||
description: string | ||
company: string | ||
location: string | ||
salary: number | ||
created_at: string | ||
updated_at: string | ||
} | ||
user?: { | ||
_id?: string | ||
name?: string | ||
email: string | ||
password: string | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.