Skip to content

Commit

Permalink
refactor: status headers
Browse files Browse the repository at this point in the history
  • Loading branch information
risv1 committed May 1, 2024
1 parent b08b7c3 commit 6861df9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions server/api/login.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default defineEventHandler(async (event) => {
try {
const body = await readBody(event);
if (!body.email || !body.password) {
setResponseStatus(event, 400);
return {
message: "Please provide all required fields.",
statusCode: 400,
};
}

Expand All @@ -23,25 +23,25 @@ export default defineEventHandler(async (event) => {
.from(users)
.where(eq(users.email, body.email));
if (!user) {
setResponseStatus(event, 404);
return {
message: "User not found.",
statusCode: 400,
};
}

const isValid = await bcrypt.compare(body.password, user.password);
if (!isValid) {
setResponseStatus(event, 400);
return {
message: "Invalid password.",
statusCode: 400,
};
}

const token = jwt.sign(user, process.env.JWT_SECRET!, { expiresIn: "1h" });
if (!token) {
setResponseStatus(event, 500);
return {
message: "Token not created.",
statusCode: 400,
};
}

Expand All @@ -50,9 +50,9 @@ export default defineEventHandler(async (event) => {
return { message: "User logged in successfully.", user: user };
} catch (e: any) {
console.error("Error: ", e);
setResponseStatus(event, 500);
return {
message: "Error: " + e,
statusCode: 500,
};
}
});
4 changes: 2 additions & 2 deletions server/api/logout.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ export default defineEventHandler(async (event) => {
const user = jwt.verify(token, "secret");
if (user) {
setCookie(event, "token", "", { expires: new Date(0) });
setResponseStatus(event, 200);
return {
statusCode: 200,
message: "Logged out",
};
}
} catch (e) {
console.error(e);
setResponseStatus(event, 500);
return {
message: "Error: " + e,
statusCode: 500,
};
}
});
8 changes: 5 additions & 3 deletions server/api/register.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bcrypt from "bcrypt";
import { users } from "~/database/schema";
import { db } from "~/database/db";
import { User } from "~/models/users";
import { set } from "zod";

async function hashPassword(password: string) {
return await bcrypt.hash(password, 10);
Expand All @@ -12,9 +13,9 @@ export default defineEventHandler(async (event) => {
try {
const body = await readBody(event);
if (!body.name || !body.email || !body.password) {
setResponseStatus(event, 400);
return {
message: "Please provide all required fields.",
statusCode: 400,
};
}

Expand Down Expand Up @@ -44,15 +45,16 @@ export default defineEventHandler(async (event) => {

if (!user) {
console.log("User not created");
return { message: "User not created.", statusCode: 400 };
setResponseStatus(event, 500);
return { message: "User not created."};
}

return { message: "User created successfully.", user: user };
} catch (e: any) {
console.error("Error: ", e);
setResponseStatus(event, 500);
return {
message: "Error: " + e,
statusCode: 500,
};
}
});
12 changes: 8 additions & 4 deletions server/api/session.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ export default defineEventHandler(async (event) => {
try {
const token = getCookie(event, "token") as string;
if (!token) {
return { status: 401, body: "Unauthorized" };
setResponseStatus(event, 401);
return { body: "Unauthorized" };
}

const data = jwt.verify(token, process.env.JWT_SECRET!) as UserPayload;
if (!data) {
return { status: 401, body: "Unauthorized" };
setResponseStatus(event, 401);
return { body: "Unauthorized" };
}

setResponseStatus(event, 200);
return {
status: 200,
user: {
name: data.name,
email: data.email,
},
};
} catch (e) {
return { status: 500, message: e };
console.error("Error: ", e);
setResponseStatus(event, 500);
return { message: e };
}
});

0 comments on commit 6861df9

Please sign in to comment.