This commit is contained in:
Andreas 2023-06-12 05:43:37 +02:00
parent fe04aaba31
commit 73685614ab
2 changed files with 26 additions and 21 deletions

View File

@ -14,7 +14,7 @@ class UserModel extends Model<UserAttributes, UserCreationAttributes>{
updatedAt?: Date;
username: string = "";
password: string = "";
id: undefined;
id?:number;
// declare title
}
export const MUser = sequelize.define<UserModel>(

View File

@ -15,19 +15,24 @@ import { validatePassword, hashPassword } from "@/util/Auth";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'GET') {
let auth;
let getAuth = () => {
try {
if (req.headers.authorization === undefined) {
throw "Basic Auth is required";
}
const authString = Buffer.from(req.headers.authorization.split(" ")[1], "base64").toString("utf8");
auth = authString.split(":");
return authString.split(":");
} catch (error) {
res.status(500).json("Basic Auth is required");
res.status(500).json(error);
return;
}
};
const auth = getAuth() || ["",""];
console.log(auth);
const username = auth[0];
const password = auth[1];
// console.log(req.body);
await MUser.sync()
MUser.sync()
.then(async user => {
// console.log(user);
return await MAuth.sync();
@ -59,8 +64,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
})
.then(async ({ authtoken, user }) => {
if (authtoken == null) {
if (typeof user.id === "number") {
if (authtoken == undefined) {
if (user.id != undefined) {
// console.log("creating new auth token")
return await MAuth.create({ user_id: user.id });
}