-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented JWT Authentication for User login and registration #69
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import User from '../models/userModel.js'; | ||
|
||
const authRoute = async (req, res, next) => { | ||
try { | ||
const token = req.cookies.jwt; | ||
|
||
if (!token) { | ||
return res | ||
.status(401) | ||
.json({ error: 'Unauthorized - No Token Provided' }); | ||
} | ||
|
||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
|
||
if (!decoded) { | ||
return res | ||
.status(401) | ||
.json({ error: 'Unauthorized - Invalid Token' }); | ||
} | ||
|
||
const user = await User.findById(decoded.userId).select('-password'); | ||
|
||
if (!user) { | ||
return res.status(404).json({ error: 'User not found' }); | ||
} | ||
|
||
req.user = user; | ||
|
||
next(); | ||
} catch (error) { | ||
console.log( | ||
'Error in middleware: ', | ||
//@ts-expect-error error?.message is giving error due to ts configuration | ||
error?.message || 'Invalid access token' | ||
); | ||
res.status(500).json({ error: 'Internal server error' }); | ||
} | ||
}; | ||
|
||
export default authRoute; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const userSchema = new mongoose.Schema( | ||
{ | ||
username: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minlength: 6, | ||
}, | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
const User = mongoose.model('User', userSchema); | ||
|
||
export default User; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import express from 'express'; | ||
import authRoute from '../middlewares/authMiddleware'; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/register', authRoute); | ||
|
||
router.post('/login', authRoute); | ||
|
||
export default router; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,12 @@ app.get('/', (req, res) => { | |
res.send('Hello from the backend!'); | ||
}); | ||
|
||
//routes | ||
|
||
import userRoute from '../routes/userRoute.js'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better if you wrote all the import statements in one place. You can then segregate those imports there by mentioning their use case (in this case routes) in the comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AadityanshuSingh true i just thought that keeping all the routes import together would make it look more organized |
||
app.use('/api/v1/auth', userRoute); | ||
|
||
app.listen(port, () => { | ||
console.log(`Server running on port ${port}`); | ||
}); |
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.
Why are you using the authRoute function for the login route? Authentication will be done after the login (and token generation) of the user.
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.
@AadityanshuSingh sorry about that , I just kept it that way because the login controller was not yet made