Account creation

This commit is contained in:
2024-06-22 23:59:44 +08:00
parent ce51b78b14
commit 4a39c5ad31
9 changed files with 380 additions and 58 deletions

View File

@@ -3,7 +3,7 @@ module.exports = (sequelize, DataTypes) => {
"User",
{
id: {
type: DataTypes.INTEGER(),
type: DataTypes.STRING(36),
allowNull: false,
primaryKey: true,
},
@@ -23,7 +23,7 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING(8),
allowNull: false,
},
passwordHash: {
password: {
type: DataTypes.STRING(255),
allowNull: false,
},

View File

@@ -10,12 +10,14 @@
"keywords": [],
"author": "Wind_Explorer",
"dependencies": {
"argon2": "^0.40.3",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"mysql2": "^3.10.1",
"nodemon": "^3.1.3",
"sequelize": "^6.37.3",
"uuid": "^10.0.0",
"yup": "^1.4.0"
}
}

36
server/pnpm-lock.yaml generated
View File

@@ -5,6 +5,9 @@ settings:
excludeLinksFromLockfile: false
dependencies:
argon2:
specifier: ^0.40.3
version: 0.40.3
cors:
specifier: ^2.8.5
version: 2.8.5
@@ -23,12 +26,20 @@ dependencies:
sequelize:
specifier: ^6.37.3
version: 6.37.3(mysql2@3.10.1)
uuid:
specifier: ^10.0.0
version: 10.0.0
yup:
specifier: ^1.4.0
version: 1.4.0
packages:
/@phc/format@1.0.0:
resolution: {integrity: sha512-m7X9U6BG2+J+R1lSOdCiITLLrxm+cWlNI3HUFA92oLO77ObGNzaKdh8pMLqdZcshtkKuV84olNNXDfMc4FezBQ==}
engines: {node: '>=10'}
dev: false
/@types/debug@4.1.12:
resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==}
dependencies:
@@ -65,6 +76,16 @@ packages:
picomatch: 2.3.1
dev: false
/argon2@0.40.3:
resolution: {integrity: sha512-FrSmz4VeM91jwFvvjsQv9GYp6o/kARWoYKjbjDB2U5io1H3e5X67PYGclFDeQff6UXIhUd4aHR3mxCdBbMMuQw==}
engines: {node: '>=16.17.0'}
requiresBuild: true
dependencies:
'@phc/format': 1.0.0
node-addon-api: 8.0.0
node-gyp-build: 4.8.1
dev: false
/array-flatten@1.1.1:
resolution: {integrity: sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==}
dev: false
@@ -579,6 +600,16 @@ packages:
engines: {node: '>= 0.6'}
dev: false
/node-addon-api@8.0.0:
resolution: {integrity: sha512-ipO7rsHEBqa9STO5C5T10fj732ml+5kLN1cAG8/jdHd56ldQeGj3Q7+scUS+VHK/qy1zLEwC4wMK5+yM0btPvw==}
engines: {node: ^18 || ^20 || >= 21}
dev: false
/node-gyp-build@4.8.1:
resolution: {integrity: sha512-OSs33Z9yWr148JZcbZd5WiAXhh/n9z8TxQcdMhIOlpN9AhWpLfvVFO73+m77bBABQMaY9XSvIa+qk0jlI7Gcaw==}
hasBin: true
dev: false
/nodemon@3.1.3:
resolution: {integrity: sha512-m4Vqs+APdKzDFpuaL9F9EVOF85+h070FnkHVEoU4+rmT6Vw0bmNl7s61VEkY/cJkL7RCv1p4urnUDUMrS5rk2w==}
engines: {node: '>=10'}
@@ -904,6 +935,11 @@ packages:
engines: {node: '>= 0.4.0'}
dev: false
/uuid@10.0.0:
resolution: {integrity: sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==}
hasBin: true
dev: false
/uuid@8.3.2:
resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==}
hasBin: true

View File

@@ -2,25 +2,39 @@ const express = require("express");
const yup = require("yup");
const { Op } = require("sequelize");
const { User } = require("../models");
const argon2 = require("argon2");
const router = express.Router();
const { v4: uuidV4 } = require("uuid");
let validationSchema = yup.object({
id: yup.number().min(0).required(),
id: yup.string().trim().min(36).max(36).required(),
firstName: yup.string().trim().min(1).max(100).required(),
lastName: yup.string().trim().min(1).max(100).required(),
email: yup.string().trim().min(5).max(69).email().required(),
phoneNumber: yup.string().trim().length(8).required(),
passwordHash: yup.string().trim().min(128).max(255).required(),
phoneNumber: yup
.string()
.trim()
.matches(/^[0-9]+$/)
.length(8)
.required(),
password: yup.string().trim().max(255).required(),
});
router.post("/", async (req, res) => {
let data = req.body;
try {
data.id = uuidV4();
data.password = await argon2.hash(data.password);
console.log("Validating schema...");
data = await validationSchema.validate(data, { abortEarly: false });
// Process valid data
console.log("Creating user...");
let result = await User.create(data);
res.json(result);
console.log("Success!");
} catch (err) {
console.log("Error caught! Info: " + err);
res.status(400).json({ errors: err.errors });
}
});