fixed api routes

This commit is contained in:
2023-06-12 05:02:59 +02:00
parent c036418a87
commit 23aa9e4e2f
6 changed files with 197 additions and 41 deletions
+44
View File
@@ -0,0 +1,44 @@
import mysql2, { Connection, RowDataPacket, OkPacket, QueryError } from "mysql2";
import { getConnection } from "@/db";
import { Post, postPlaceholder } from "@/model/Models";
import { getPosts, IPost } from "@/controller/Post";
import { NextApiRequest, NextApiResponse } from "next";
// import { MPost, MUser, MAuth } from "@/model/Models"
import { MPost } from "@/model/sequelize/Post";
import { MUser } from "@/model/sequelize/User";
import { MAuth } from "@/model/sequelize/Auth";
import { DataType, Model, Sequelize, UUID } from "sequelize";
import { validatePassword, hashPassword } from "@/util/Auth";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
const username = req.body.username;
const password = req.body.password;
console.log(req.body);
const user = await MUser.sync()
.then(async f => {
return await MUser.findOne({ where: { username: username } });
})
.then(async user => {
if (user == undefined) {
const hash = await hashPassword(password)
return await MUser.create({
username: username,
password: hash
})
}
else{
throw "User with that username already exists";
}
})
.then(user =>{
res.status(200).json(user)
})
.catch(error => {
res.status(500).json(error);
});
}
}