Skip to content
This repository was archived by the owner on May 14, 2022. It is now read-only.

Commit aebba44

Browse files
committed
fix: lack of passport verification (MEL-12) (#31)
1 parent 581a1f4 commit aebba44

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

lib/auth/passport.ts

+9-15
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ import { jwtConfig } from '../utils/config'
77

88
/* Fucking Passport-thing selection */
99

10-
let localstrategy = new LocalStrategy({ usernameField: 'username' }, function (username, password, done) {
11-
User.findOne({ username: username })
12-
.then(function (user) {
13-
if (!user) {
14-
return done(null, false, { message: 'No such user' })
15-
}
16-
if (!verify(password, user.password)) {
17-
done(null, false, { message: 'Wrong password' })
18-
}
19-
return done(null, user)
20-
})
21-
.catch(function (err) {
22-
return done(null, false, { message: err })
23-
})
10+
let localstrategy = new LocalStrategy({ usernameField: 'username' }, async function (username, password, done) {
11+
const aggregateUser = await User.findOne({ username: username })
12+
if (!aggregateUser) {
13+
return done(null, false)
14+
}
15+
if (!(await verify(password, aggregateUser.password))) {
16+
return done(null, false)
17+
}
18+
return done(null, aggregateUser)
2419
})
2520

2621
let jwtstrategy = new JwtStrategy(jwtConfig, function (payload, done) {
@@ -37,7 +32,6 @@ let jwtstrategy = new JwtStrategy(jwtConfig, function (payload, done) {
3732
})
3833

3934
passport.serializeUser((user: any, done) => {
40-
// ? ID or _ID
4135
done(null, user.id)
4236
})
4337

lib/auth/router.v1.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ router.post('/login', (req, res, next) => {
1414
return res.status(400).json({ errors: err })
1515
}
1616
if (!user) {
17-
return res.status(400).json({ errors: 'No user found' })
17+
return res.status(400).json({ message: 'User with specified data do not exist (wrong password, login or no account)' })
1818
}
1919

2020
const token = jwt.sign({ id: user.id }, jwtConfig.secretOrKey)
2121
req.logIn(user, function (err) {
2222
if (err) {
2323
return res.status(400).json({ errors: err })
2424
}
25-
return res.status(200).json({ success: `Hello! ${user.username}`, token: token })
25+
return res.status(200).json({ success: `Hello! ${user.username}`, token: token, data: user })
2626
})
2727
})(req, res, next)
2828
})

0 commit comments

Comments
 (0)