-
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.
store jwt and add it in auth header for api requests
- Loading branch information
1 parent
8e151d9
commit a6edd2a
Showing
9 changed files
with
83 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Request } from 'express'; | ||
|
||
export interface AuthedRequest extends Request { | ||
jwt?: 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
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 |
---|---|---|
@@ -1,27 +1,35 @@ | ||
import { RequestHandler } from 'express'; | ||
import JWT from 'jsonwebtoken'; | ||
|
||
import { AuthedRequest } from '../interfaces/authed-request'; | ||
import { JWTPayloadWithUser } from '../interfaces/jwt-payload-with-user'; | ||
import { logger } from '../utils/logger'; | ||
|
||
export const ensureAuthenticated: RequestHandler = (req, res, next) => { | ||
export const ensureAuthenticated: RequestHandler = (req: AuthedRequest, res, next) => { | ||
logger.debug('checking if user is authenticated...'); | ||
|
||
if (!req.cookies.jwt) { | ||
logger.error('JWT cookie not found'); | ||
return res.redirect('/auth/login'); | ||
} | ||
try { | ||
if (!req.cookies.jwt) { | ||
throw new Error('JWT cookie not found'); | ||
} | ||
|
||
const secret = process.env.JWT_SECRET || ''; | ||
const decoded = JWT.verify(req.cookies.jwt, secret) as JWTPayloadWithUser; | ||
const secret = process.env.JWT_SECRET || ''; | ||
const token = req.cookies.jwt; | ||
const decoded = JWT.verify(token, secret) as JWTPayloadWithUser; | ||
|
||
if (decoded.exp && decoded.exp <= Date.now() / 1000) { | ||
logger.error('JWT token has expired'); | ||
return res.redirect('/auth/login'); | ||
} | ||
if (decoded.exp && decoded.exp <= Date.now() / 1000) { | ||
throw new Error('JWT token has expired'); | ||
} | ||
|
||
req.user = decoded.user; | ||
logger.info('user is authenticated'); | ||
// store the token string as we need it for the auth header in the API requests | ||
req.jwt = token; | ||
req.user = decoded.user; | ||
logger.info('user is authenticated'); | ||
} catch (err) { | ||
logger.error(err); | ||
res.status(401); | ||
return res.redirect('/auth/login?error=unauthenticated'); | ||
} | ||
|
||
return 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
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