fixed api routes
This commit is contained in:
+70
-41
@@ -3,50 +3,79 @@ 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 { Sequelize } from "sequelize";
|
||||
import { Elsie_Swash_Caps } from "next/font/google";
|
||||
// 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";
|
||||
|
||||
export default async function handler(req:NextApiRequest, res:NextApiResponse) {
|
||||
await MUser.sync();
|
||||
await MAuth.sync();
|
||||
switch (req.method) {
|
||||
case 'POST':
|
||||
case 'GET':
|
||||
const users = await MUser.findAll();
|
||||
// res.status(200).json(posts);
|
||||
let username = req.body.username;
|
||||
let password = req.body.password;
|
||||
console.log(req.body );
|
||||
if(users.length == 0){
|
||||
MUser.create({
|
||||
username: "admin",
|
||||
password: "changeme"
|
||||
})
|
||||
}
|
||||
users.forEach(user => async {
|
||||
if(user.username == username && user.password == password){
|
||||
try{
|
||||
const authtoken = await MAuth.findOne({where : {user_id: user.id}});
|
||||
if(authtoken != null){
|
||||
res.status(200).json({"status":"correct"});
|
||||
console.log(authtoken);
|
||||
}
|
||||
else{
|
||||
res.status(200).json({"status":"no such auth token"});
|
||||
}
|
||||
} catch(e){
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
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 === 'GET') {
|
||||
let auth;
|
||||
try {
|
||||
const authString = Buffer.from(req.headers.authorization.split(" ")[1], "base64").toString("utf8");
|
||||
auth = authString.split(":");
|
||||
} catch (error) {
|
||||
res.status(500).json("Basic Auth is required");
|
||||
return;
|
||||
}
|
||||
console.log(auth);
|
||||
const username = auth[0];
|
||||
const password = auth[1];
|
||||
// console.log(req.body);
|
||||
await MUser.sync()
|
||||
.then(async user => {
|
||||
// console.log(user);
|
||||
return await MAuth.sync();
|
||||
})
|
||||
.then(async auth => {
|
||||
// console.log(auth);
|
||||
return await MUser.findOne({ where: { username: username } });
|
||||
})
|
||||
.then(async user => {
|
||||
// console.log(user);
|
||||
if (user == undefined) {
|
||||
throw "no such user exists";
|
||||
}
|
||||
else{
|
||||
console.log(user.password);
|
||||
res.status(200).json({"status":"incorrect"});
|
||||
|
||||
return user;
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
})
|
||||
.then(async user => {
|
||||
const passIsValid = await validatePassword(password, user.password);
|
||||
return {passIsValid, user};
|
||||
})
|
||||
.then(async ({passIsValid, user})=>{
|
||||
if(passIsValid){
|
||||
const authtoken = await MAuth.findOne({ where: { user_id: user.id } });
|
||||
return {authtoken, user}
|
||||
}
|
||||
else{
|
||||
throw("invalid password");
|
||||
}
|
||||
})
|
||||
.then(async ({authtoken, user}) => {
|
||||
if (authtoken == null) {
|
||||
if (typeof user.id === "number") {
|
||||
// console.log("creating new auth token")
|
||||
return await MAuth.create({ user_id: user.id });
|
||||
}
|
||||
}
|
||||
else {
|
||||
return authtoken
|
||||
}
|
||||
}).then(authtoken => {
|
||||
if (authtoken != null) {
|
||||
// console.log(authtoken);
|
||||
res.status(200).json(authtoken);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).json(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user