no
This commit is contained in:
parent
fe04aaba31
commit
73685614ab
@ -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>(
|
||||||
|
|||||||
@ -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();
|
||||||
@ -41,26 +46,26 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
if (user == undefined) {
|
if (user == undefined) {
|
||||||
throw "no such user exists";
|
throw "no such user exists";
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(async user => {
|
.then(async user => {
|
||||||
const passIsValid = await validatePassword(password, user.password);
|
const passIsValid = await validatePassword(password, user.password);
|
||||||
return {passIsValid, user};
|
return { passIsValid, user };
|
||||||
})
|
})
|
||||||
.then(async ({passIsValid, user})=>{
|
.then(async ({ passIsValid, user }) => {
|
||||||
if(passIsValid){
|
if (passIsValid) {
|
||||||
const authtoken = await MAuth.findOne({ where: { user_id: user.id } });
|
const authtoken = await MAuth.findOne({ where: { user_id: user.id } });
|
||||||
return {authtoken, user}
|
return { authtoken, user }
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
throw("invalid password");
|
throw ("invalid password");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.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 });
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user