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