feat: add username and email check in frontend

This commit is contained in:
Lin, Hung-Shin committed 2023-10-13 16:38:25 +08:00
1 parent 30aeacaf06
commit c5ccf03ac5
15 files changed
+95 -64

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
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)
}
+42
View File
@@ -0,0 +1,42 @@
import { isValidEmail, isValidUsername } from './auth.js'
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)
})
})
+1
View File
@@ -0,0 +1 @@
export { isValidEmail, isValidUsername } from './auth.js'
+8
View File
@@ -0,0 +1,8 @@
{
"name": "@linux-odyssey/utils",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"exports": "./index.js",
"license": "MIT"
}