generated from aritroCoder/template-typescript-backend-yarn
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from HelloSniperMonkey/main
added authorisation using jwt and also added api endpoint for login
- Loading branch information
Showing
14 changed files
with
775 additions
and
481 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 |
---|---|---|
|
@@ -90,6 +90,7 @@ out | |
# Nuxt.js build / generate output | ||
.nuxt | ||
dist | ||
/public | ||
|
||
# Gatsby files | ||
.cache/ | ||
|
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
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,43 @@ | ||
import express, { Request, Response } from 'express'; | ||
const Student = require('../../models/Student') | ||
const Mentor = require('../../models/Mentor') | ||
const { generateToken } = require('../utils/jwt'); | ||
const router = express.Router(); | ||
|
||
router.post('/students', (req: Request, res: Response) => { | ||
const { email, password } = req.body; | ||
Student.findOne({ email: email }) | ||
.then((user: typeof Student) => { | ||
if (!user) { | ||
return res.status(404).json({ message: 'Student not found' }); | ||
} | ||
if (user.password !== password) { | ||
return res.status(401).json({ message: 'Invalid password' }); | ||
} | ||
const token = generateToken({ email: user.email }); | ||
res.cookie("token", token); | ||
res.status(200).json({ message: 'Login successful' }); | ||
}).catch((err: any) => { | ||
res.status(500).json({ error: err.message }); | ||
}); | ||
}); | ||
|
||
router.post('/mentors', (req, res) => { | ||
const { email, password } = req.body; | ||
Mentor.findOne({ email: email }) | ||
.then((user: typeof Student) => { | ||
if (!user) { | ||
return res.status(404).json({ message: 'Mentor not found' }); | ||
} | ||
if (user.password !== password) { | ||
return res.status(401).json({ message: 'Invalid password' }); | ||
} | ||
const token = generateToken({ email: user.email }); | ||
res.cookie("token", token); | ||
res.status(200).json({ message: 'Login successful' }); | ||
}).catch((err: any) => { | ||
res.status(500).json({ error: err.message }); | ||
}); | ||
}); | ||
|
||
module.exports = router |
Empty file.
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
Empty file.
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,21 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { verifyToken } from '../utils/jwt'; | ||
|
||
export const authorization = (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const token = req.headers.authorization?.split(' ')[1]; | ||
if (!token) { | ||
return res.status(401).json({ message: 'Authentication required' }); | ||
} | ||
|
||
const decoded = verifyToken(token); | ||
if(!decoded) { | ||
return res.status(401).json({ message: 'Invalid token' }); | ||
} else { | ||
console.log('Authenticated by auth service'); | ||
} | ||
next(); | ||
} catch (error) { | ||
res.status(401).json({ message: 'Invalid token' }); | ||
} | ||
}; |
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,20 @@ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key'; | ||
|
||
export interface JWTPayload { | ||
email: string; | ||
} | ||
|
||
export const generateToken = (payload: JWTPayload): string => { | ||
return jwt.sign(payload, JWT_SECRET, { expiresIn: '24h' }); | ||
}; | ||
|
||
export const verifyToken = (token: string): boolean => { | ||
try { | ||
jwt.verify(token, JWT_SECRET); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; |
Oops, something went wrong.