mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
feat: add validators for username, email, password
* not passed yet
This commit is contained in:
1 parent
6a64f75950
commit
040b2a71e5
4 files changed
+82
-76
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { isValidEmail, isValidUsername } from '@linux-odyssey/utils'
|
import { matchedData } from 'express-validator'
|
||||||
import { User } from '@linux-odyssey/models'
|
import { User } from '@linux-odyssey/models'
|
||||||
import { genJWT, hashPassword } from '../../utils/auth.js'
|
import { genJWT, hashPassword } from '../../utils/auth.js'
|
||||||
|
|
||||||
@@ -12,67 +12,8 @@ export async function issueToken(req, res) {
|
|||||||
res.json({ token })
|
res.json({ token })
|
||||||
}
|
}
|
||||||
|
|
||||||
async function invalidUsername(username) {
|
|
||||||
return !isValidUsername(username) || User.exists({ username })
|
|
||||||
}
|
|
||||||
|
|
||||||
async function invalidEmail(email) {
|
|
||||||
return !isValidEmail(email) || User.exists({ email })
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function checkUsername(req, res) {
|
|
||||||
const { username, email } = req.query
|
|
||||||
const cred = username || email
|
|
||||||
if (!cred) {
|
|
||||||
res.status(400).json({
|
|
||||||
message: 'username or email is required',
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isValidEmail(cred)) {
|
|
||||||
res.json({
|
|
||||||
type: 'email',
|
|
||||||
available: !(await User.exists({ email: cred })),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (isValidUsername(cred)) {
|
|
||||||
res.json({
|
|
||||||
type: 'username',
|
|
||||||
available: !(await User.exists({ username: cred })),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
res.status(400).json({
|
|
||||||
message: 'invalid username or email',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function register(req, res, next) {
|
export async function register(req, res, next) {
|
||||||
const { username, password, email } = req.body
|
const { username, password, email } = matchedData(req)
|
||||||
if (!(username && password && email)) {
|
|
||||||
res.status(400).json({
|
|
||||||
message: 'username, password, and email are required',
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (await invalidUsername(username)) {
|
|
||||||
res.status(409).json({
|
|
||||||
type: 'username',
|
|
||||||
message: 'Invalid username',
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (await invalidEmail(email)) {
|
|
||||||
res.status(409).json({
|
|
||||||
type: 'email',
|
|
||||||
message: 'Invalid email',
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = new User({
|
const user = new User({
|
||||||
username,
|
username,
|
||||||
@@ -81,7 +22,6 @@ export async function register(req, res, next) {
|
|||||||
try {
|
try {
|
||||||
user.hashedPassword = await hashPassword(password)
|
user.hashedPassword = await hashPassword(password)
|
||||||
await user.save()
|
await user.save()
|
||||||
req.user = user
|
|
||||||
req.login(user, (err) => {
|
req.login(user, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
next(err)
|
next(err)
|
||||||
@@ -131,7 +71,7 @@ export async function socialLogin(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function registerFromSession(req, res) {
|
export async function registerFromSession(req, res) {
|
||||||
const { username } = req.body
|
const { username } = matchedData(req)
|
||||||
const { newUser } = req.session
|
const { newUser } = req.session
|
||||||
|
|
||||||
if (!newUser) {
|
if (!newUser) {
|
||||||
@@ -141,14 +81,6 @@ export async function registerFromSession(req, res) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await invalidUsername(username)) {
|
|
||||||
res.status(409).json({
|
|
||||||
type: 'username',
|
|
||||||
message: 'Invalid username',
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
newUser.username = username
|
newUser.username = username
|
||||||
const user = new User(newUser)
|
const user = new User(newUser)
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,17 @@ import enabledMethods from '../../auth/passport.js'
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
register,
|
register,
|
||||||
checkUsername,
|
|
||||||
checkSession,
|
checkSession,
|
||||||
logout,
|
logout,
|
||||||
socialLogin,
|
socialLogin,
|
||||||
registerFromSession,
|
registerFromSession,
|
||||||
} from '../controllers/authController.js'
|
} from '../controllers/authController.js'
|
||||||
|
import {
|
||||||
|
checkNewEmail,
|
||||||
|
checkNewUsername,
|
||||||
|
checkPassword,
|
||||||
|
} from '../validators/authValidator.js'
|
||||||
|
import { noError } from '../../middleware/validator.js'
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
@@ -22,12 +27,20 @@ router.post(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
router.post('/register', register)
|
router.post(
|
||||||
|
'/register',
|
||||||
|
checkNewUsername(),
|
||||||
|
checkNewEmail(),
|
||||||
|
checkPassword(),
|
||||||
|
noError,
|
||||||
|
register
|
||||||
|
)
|
||||||
|
|
||||||
router.post('/logout', logout)
|
router.post('/logout', logout)
|
||||||
|
|
||||||
router.get('/check-username', checkUsername)
|
router.get('/check-username', checkNewUsername(), noError, (req, res) =>
|
||||||
|
res.json({ available: true })
|
||||||
|
)
|
||||||
router.get('/check-session', checkSession)
|
router.get('/check-session', checkSession)
|
||||||
|
|
||||||
router.get('/available-methods', (req, res) => {
|
router.get('/available-methods', (req, res) => {
|
||||||
@@ -44,6 +57,11 @@ if (enabledMethods.github) {
|
|||||||
router.get('/github/callback', passport.authenticate('github'), socialLogin)
|
router.get('/github/callback', passport.authenticate('github'), socialLogin)
|
||||||
}
|
}
|
||||||
|
|
||||||
router.post('/register-from-session', registerFromSession)
|
router.post(
|
||||||
|
'/register-from-session',
|
||||||
|
checkNewUsername(),
|
||||||
|
noError,
|
||||||
|
registerFromSession
|
||||||
|
)
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { query } from 'express-validator'
|
||||||
|
|
||||||
|
import { isValidUsername } from '@linux-odyssey/utils'
|
||||||
|
import { User } from '@linux-odyssey/models'
|
||||||
|
|
||||||
|
export const checkUsername = () =>
|
||||||
|
query('username')
|
||||||
|
.notEmpty()
|
||||||
|
.isString()
|
||||||
|
.custom((username) => {
|
||||||
|
if (!isValidUsername(username)) {
|
||||||
|
throw new Error('Invalid username')
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
.trim()
|
||||||
|
|
||||||
|
export const checkNewUsername = () =>
|
||||||
|
checkUsername().custom(async (username) => {
|
||||||
|
if (await User.exists({ username })) {
|
||||||
|
throw new Error('Username taken')
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
export const checkEmail = () => query('email').notEmpty().isEmail().trim()
|
||||||
|
|
||||||
|
export const checkNewEmail = () =>
|
||||||
|
checkEmail().custom(async (email) => {
|
||||||
|
if (await User.exists({ email })) {
|
||||||
|
throw new Error('Email taken')
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
export const checkPassword = () =>
|
||||||
|
query('password').notEmpty().isStrongPassword({
|
||||||
|
minLength: 8,
|
||||||
|
minNumbers: 1,
|
||||||
|
minLowercase: 1,
|
||||||
|
minUppercase: 1,
|
||||||
|
minSymbols: 0,
|
||||||
|
})
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { validationResult } from 'express-validator'
|
||||||
|
|
||||||
|
export function noError(req, res, next) {
|
||||||
|
const result = validationResult(req)
|
||||||
|
if (result.isEmpty()) {
|
||||||
|
next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
console.log(result.array())
|
||||||
|
res.status(400).json({
|
||||||
|
errors: result.array(),
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in new issue
Block a user