mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add username and email check in frontend
This commit is contained in:
1 parent
30aeacaf06
commit
c5ccf03ac5
15 files changed
+95
-64
No files matched your search
@@ -4,10 +4,12 @@ WORKDIR /app
|
||||
COPY ./package.json yarn.lock ./
|
||||
COPY server/package.json ./server/package.json
|
||||
COPY packages/file-graph/package.json ./packages/file-graph/package.json
|
||||
COPY packages/utils/package.json ./packages/utils/package.json
|
||||
RUN yarn install --frozen-lockfile --production
|
||||
|
||||
COPY ./server ./server
|
||||
COPY packages/file-graph ./packages/file-graph
|
||||
COPY packages/utils ./packages/utils
|
||||
COPY quests ./quests
|
||||
|
||||
WORKDIR /app/server
|
||||
|
||||
@@ -4,6 +4,7 @@ WORKDIR /app
|
||||
COPY package.json yarn.lock ./
|
||||
COPY server/package.json ./server/package.json
|
||||
COPY packages/file-graph/package.json ./packages/file-graph/package.json
|
||||
COPY packages/utils/package.json ./packages/utils/package.json
|
||||
RUN yarn install --frozen-lockfile
|
||||
|
||||
COPY server/nodemon.json ./server/nodemon.json
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@linux-odyssey/file-graph": "*",
|
||||
"@linux-odyssey/utils": "*",
|
||||
"bcrypt": "^5.1.1",
|
||||
"commander": "^11.0.0",
|
||||
"connect-mongo": "^5.0.0",
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import { isValidEmail, isValidUsername } from '@linux-odyssey/utils'
|
||||
import User from '../../models/user.js'
|
||||
import {
|
||||
genJWT,
|
||||
hashPassword,
|
||||
isValidEmail,
|
||||
isValidUsername,
|
||||
} from '../../utils/auth.js'
|
||||
import { genJWT, hashPassword } from '../../utils/auth.js'
|
||||
|
||||
export async function issueToken(req, res) {
|
||||
const { user } = req
|
||||
|
||||
@@ -47,42 +47,3 @@ export function verifyJWT(token) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function isValidUsername(username) {
|
||||
// Define a regular expression pattern for a valid Linux username.
|
||||
const usernamePattern = /^[a-z][a-z0-9_-]*$/
|
||||
|
||||
// Test the username against the pattern.
|
||||
if (!usernamePattern.test(username)) return false
|
||||
|
||||
// check if the username is between 1 and 32 characters
|
||||
if (username.length < 4 || username.length > 32) {
|
||||
return false
|
||||
}
|
||||
|
||||
const reservedWords = [
|
||||
'root',
|
||||
'admin',
|
||||
'administrator',
|
||||
'sysadmin',
|
||||
'system',
|
||||
'daemon',
|
||||
'bin',
|
||||
'nobody',
|
||||
'nogroup',
|
||||
'guest',
|
||||
'user',
|
||||
]
|
||||
if (reservedWords.includes(username)) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
export function isValidEmail(email) {
|
||||
// Define a regular expression pattern for a valid email address.
|
||||
const emailPattern = /^[\w-.+]+@([\w][\w-]*\.)+[\w-]{2,}$/
|
||||
|
||||
// Test the email against the pattern.
|
||||
return emailPattern.test(email)
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import { isValidEmail, isValidUsername } from '../../src/utils/auth'
|
||||
|
||||
test('check valid username', () => {
|
||||
const valid = ['alex', 'alex123', 'john_doe', 'john-doe', 'rudeus']
|
||||
|
||||
const invalid = ['root', 'admin', '12michael', 'Justin', 'testUser']
|
||||
|
||||
valid.forEach((username) => {
|
||||
expect(isValidUsername(username)).toBe(true)
|
||||
})
|
||||
|
||||
invalid.forEach((username) => {
|
||||
expect(isValidUsername(username)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
test('check valid email', () => {
|
||||
const validEmails = [
|
||||
'user@example.com',
|
||||
'user.name@example.co.uk',
|
||||
'user1234@email.domain',
|
||||
'user+tag@example.net',
|
||||
'user@subdomain.example.com',
|
||||
'user@123.45.67.89',
|
||||
]
|
||||
|
||||
const invalidEmails = [
|
||||
'user@.com',
|
||||
'@example.com',
|
||||
'user@-invalid-domain.com',
|
||||
'user@.123',
|
||||
'user@.123.45.67',
|
||||
]
|
||||
|
||||
validEmails.forEach((email) => {
|
||||
expect(isValidEmail(email)).toBe(true)
|
||||
})
|
||||
|
||||
invalidEmails.forEach((email) => {
|
||||
expect(isValidEmail(email)).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user