mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: add validation for login
This commit is contained in:
1 parent
fc6ac11c60
commit
203114cd5e
6 files changed
+44
-24
No files matched your search
@@ -11,6 +11,7 @@ const handleLogin = async ({ username, password, success, error }) => {
|
|||||||
else error('Wrong username or password.')
|
else error('Wrong username or password.')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
error('Something went wrong.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -11,34 +11,28 @@ import {
|
|||||||
registerFromSession,
|
registerFromSession,
|
||||||
} from '../controllers/authController.js'
|
} from '../controllers/authController.js'
|
||||||
import {
|
import {
|
||||||
checkNewEmail,
|
checkUsernameValidators,
|
||||||
checkNewUsername,
|
loginValidators,
|
||||||
checkPassword,
|
registerFromSessionValidators,
|
||||||
|
registerValidators,
|
||||||
} from '../validators/authValidator.js'
|
} from '../validators/authValidator.js'
|
||||||
import { noError } from '../../middleware/validator.js'
|
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/login',
|
'/login',
|
||||||
|
loginValidators,
|
||||||
passport.authenticate('local', { failureMessage: true }),
|
passport.authenticate('local', { failureMessage: true }),
|
||||||
(req, res) => {
|
(req, res) => {
|
||||||
res.json({ message: 'success' })
|
res.json({ message: 'success' })
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
router.post(
|
router.post('/register', registerValidators, register)
|
||||||
'/register',
|
|
||||||
checkNewUsername(),
|
|
||||||
checkNewEmail(),
|
|
||||||
checkPassword(),
|
|
||||||
noError,
|
|
||||||
register
|
|
||||||
)
|
|
||||||
|
|
||||||
router.post('/logout', logout)
|
router.post('/logout', logout)
|
||||||
|
|
||||||
router.get('/check-username', checkNewUsername(), noError, (req, res) =>
|
router.get('/check-username', checkUsernameValidators, (req, res) =>
|
||||||
res.json({ available: true })
|
res.json({ available: true })
|
||||||
)
|
)
|
||||||
router.get('/check-session', checkSession)
|
router.get('/check-session', checkSession)
|
||||||
@@ -59,8 +53,7 @@ if (enabledMethods.github) {
|
|||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/register-from-session',
|
'/register-from-session',
|
||||||
checkNewUsername(),
|
registerFromSessionValidators,
|
||||||
noError,
|
|
||||||
registerFromSession
|
registerFromSession
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { check } from 'express-validator'
|
import { check } from 'express-validator'
|
||||||
|
|
||||||
import { isValidUsername } from '@linux-odyssey/utils'
|
import { isValidUsername, isValidEmail } from '@linux-odyssey/utils'
|
||||||
import { User } from '@linux-odyssey/models'
|
import { User } from '@linux-odyssey/models'
|
||||||
import { passwordPolicy } from '@linux-odyssey/constants'
|
import { passwordPolicy } from '@linux-odyssey/constants'
|
||||||
|
import { noError } from '../../middleware/validator.js'
|
||||||
|
|
||||||
export const checkUsername = () =>
|
const checkUsername = () =>
|
||||||
check('username')
|
check('username')
|
||||||
.notEmpty()
|
.notEmpty()
|
||||||
.isString()
|
.isString()
|
||||||
@@ -16,7 +17,7 @@ export const checkUsername = () =>
|
|||||||
})
|
})
|
||||||
.trim()
|
.trim()
|
||||||
|
|
||||||
export const checkNewUsername = () =>
|
const checkNewUsername = () =>
|
||||||
checkUsername().custom(async (username) => {
|
checkUsername().custom(async (username) => {
|
||||||
if (await User.exists({ username })) {
|
if (await User.exists({ username })) {
|
||||||
throw new Error('Username taken')
|
throw new Error('Username taken')
|
||||||
@@ -24,9 +25,9 @@ export const checkNewUsername = () =>
|
|||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
export const checkEmail = () => check('email').notEmpty().isEmail().trim()
|
const checkEmail = () => check('email').notEmpty().isString().isEmail().trim()
|
||||||
|
|
||||||
export const checkNewEmail = () =>
|
const checkNewEmail = () =>
|
||||||
checkEmail().custom(async (email) => {
|
checkEmail().custom(async (email) => {
|
||||||
if (await User.exists({ email })) {
|
if (await User.exists({ email })) {
|
||||||
throw new Error('Email taken')
|
throw new Error('Email taken')
|
||||||
@@ -34,5 +35,30 @@ export const checkNewEmail = () =>
|
|||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
export const checkPassword = () =>
|
const checkPassword = () =>
|
||||||
check('password').notEmpty().isStrongPassword(passwordPolicy)
|
check('password').notEmpty().isStrongPassword(passwordPolicy)
|
||||||
|
|
||||||
|
const checkLogin = () =>
|
||||||
|
check('username')
|
||||||
|
.isString()
|
||||||
|
.notEmpty()
|
||||||
|
.trim()
|
||||||
|
.custom((login) => {
|
||||||
|
if (!isValidUsername(login) && !isValidEmail(login)) {
|
||||||
|
throw new Error('Invalid login')
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
export const registerValidators = [
|
||||||
|
checkNewUsername(),
|
||||||
|
checkNewEmail(),
|
||||||
|
checkPassword(),
|
||||||
|
noError,
|
||||||
|
]
|
||||||
|
|
||||||
|
export const registerFromSessionValidators = [checkNewUsername(), noError]
|
||||||
|
|
||||||
|
export const loginValidators = [checkLogin(), noError]
|
||||||
|
|
||||||
|
export const checkUsernameValidators = [checkUsername(), noError]
|
||||||
@@ -8,7 +8,7 @@ async function verifyPassword(username, password, done) {
|
|||||||
$or: [{ username }, { email: username }],
|
$or: [{ username }, { email: username }],
|
||||||
})
|
})
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return done(null, false, { message: 'Incorrect username.' })
|
return done(null, false, { message: 'Incorrect username or password.' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const isPasswordValid = await bcrypt.compare(password, user.hashedPassword)
|
const isPasswordValid = await bcrypt.compare(password, user.hashedPassword)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
export default function errorHandler(err, req, res, next) {
|
export default function errorHandler(err, req, res, next) {
|
||||||
console.error(err.stack) // Log error stack trace to the console
|
console.error(err.stack) // Log error stack trace to the console
|
||||||
res.status(500).send({ message: err.message }) // Send error response
|
res.status(500).send({ message: 'Something broke!' }) // Send error response
|
||||||
}
|
}
|
||||||
@@ -6,8 +6,8 @@ export function noError(req, res, next) {
|
|||||||
next()
|
next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
console.log(result.array())
|
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
|
message: 'Validation failed',
|
||||||
errors: result.array(),
|
errors: result.array(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user