Commit 4a3348fa authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

finish state1: register api

parent d633fd8e
import { Request, Response } from "express";
import { SHA1 } from "crypto-js";
import db from "../models/prismaClient";
const UserController = {
/**
*
* @param {Request} req
* @param {Response} res
*/
async create(req, res) {
try {
let { name, email, phone, photo, username, password, google_token } =
req.body;
if (google_token) {
// login with google
if (!(email && photo && name)) throw "error : emty data";
username = String(email).split("@")[0];
phone = "";
password = "";
} else {
if (!(name && email && phone && username && password))
throw "error : emty data";
google_token = "";
photo = photo ?? "";
password = SHA1(password).toString();
}
let checkUsername = await db.user.findFirst({
where: { username: username },
});
if (checkUsername) throw 202;
await db.user.create({
data: {
name,
email,
phone,
photo,
username,
password,
rank: false,
google_token: google_token ?? "",
},
});
db.$disconnect();
res.json({
status: 201,
message: "success",
});
} catch (err) {
if (err === 202)
return res.json({
status: 202,
message: "username has already used",
});
console.log(err);
res.status(200).json({
status: 200,
msg: "error something",
error: err,
});
}
},
};
export default UserController;
import express from "express"; import express from "express";
import WelcomeController from "../controllers/WelcomeController"; import WelcomeController from "../controllers/WelcomeController";
import UserController from "../controllers/UserController";
const route = express.Router(); const route = express.Router();
route.get("/", WelcomeController.index); route.get("/", WelcomeController.index);
route.get("/users", (req, res) => { route.post("/user", UserController.create);
route.get("/user", (req, res) => {
res.json({ res.json({
name: "Nawasan", name: "Nawasan",
age: 21, age: 21,
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
"@mui/icons-material": "^5.14.11", "@mui/icons-material": "^5.14.11",
"@mui/material": "^5.14.11", "@mui/material": "^5.14.11",
"@prisma/client": "5.3.1", "@prisma/client": "5.3.1",
"crypto-js": "^4.1.1",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"next": "latest", "next": "latest",
......
...@@ -23,6 +23,9 @@ dependencies: ...@@ -23,6 +23,9 @@ dependencies:
'@prisma/client': '@prisma/client':
specifier: 5.3.1 specifier: 5.3.1
version: 5.3.1(prisma@5.3.1) version: 5.3.1(prisma@5.3.1)
crypto-js:
specifier: ^4.1.1
version: 4.1.1
express: express:
specifier: ^4.18.2 specifier: ^4.18.2
version: 4.18.2 version: 4.18.2
...@@ -1226,6 +1229,10 @@ packages: ...@@ -1226,6 +1229,10 @@ packages:
which: 2.0.2 which: 2.0.2
dev: true dev: true
/crypto-js@4.1.1:
resolution: {integrity: sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==}
dev: false
/css-unit-converter@1.1.2: /css-unit-converter@1.1.2:
resolution: {integrity: sha512-IiJwMC8rdZE0+xiEZHeru6YoONC4rfPMqGm2W85jMIbkFvv5nFTwJVFHam2eFrN6txmoUYFAFXiv8ICVeTO0MA==} resolution: {integrity: sha512-IiJwMC8rdZE0+xiEZHeru6YoONC4rfPMqGm2W85jMIbkFvv5nFTwJVFHam2eFrN6txmoUYFAFXiv8ICVeTO0MA==}
dev: false dev: false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment