Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): return out of auth api route functions #353

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions apps/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function authRoutes(fastify: FastifyInstance) {
const requester = await checkSession(bearer);

if (!requester?.isAdmin) {
reply.code(401).send({
return reply.code(401).send({
message: "Unauthorized",
});
}
Expand All @@ -54,7 +54,7 @@ export function authRoutes(fastify: FastifyInstance) {

// if exists, return 400
if (record) {
reply.code(400).send({
return reply.code(400).send({
message: "Email already exists",
});
}
Expand Down Expand Up @@ -117,7 +117,7 @@ export function authRoutes(fastify: FastifyInstance) {

// if exists, return 400
if (record) {
reply.code(400).send({
return reply.code(400).send({
message: "Email already exists",
});
}
Expand Down Expand Up @@ -158,7 +158,7 @@ export function authRoutes(fastify: FastifyInstance) {
});

if (!user) {
reply.code(401).send({
return reply.code(401).send({
message: "Invalid email",
success: false,
});
Expand Down Expand Up @@ -224,7 +224,7 @@ export function authRoutes(fastify: FastifyInstance) {
});

if (!user) {
reply.code(401).send({
return reply.code(401).send({
message: "Invalid Code",
success: false,
});
Expand Down Expand Up @@ -267,20 +267,18 @@ export function authRoutes(fastify: FastifyInstance) {
where: { email },
});

if (!user) {
reply.code(401).send({
if (!user?.password) {
return reply.code(401).send({
message: "Invalid email or password",
});
}

//@ts-expect-error
const isPasswordValid = await bcrypt.compare(password, user!.password);

if (!isPasswordValid) {
reply.code(401).send({
message: "Invalid email or password",
});

throw new Error("Password is not valid");
}

Expand Down Expand Up @@ -337,14 +335,14 @@ export function authRoutes(fastify: FastifyInstance) {
});

if (!user) {
reply.code(401).send({
return reply.code(401).send({
message: "Invalid email",
success: false,
});
}

if (user?.external_user) {
reply.send({
return reply.send({
success: true,
message: "External user",
oauth: false,
Expand All @@ -361,7 +359,7 @@ export function authRoutes(fastify: FastifyInstance) {
const oauth = provider[0];

if (authtype.length === 0) {
reply.code(200).send({
return reply.code(200).send({
success: true,
message: "SSO not enabled",
oauth: false,
Expand Down Expand Up @@ -513,7 +511,7 @@ export function authRoutes(fastify: FastifyInstance) {
});

if (!user) {
reply.code(401).send({
return reply.code(401).send({
message: "Invalid user",
});
}
Expand Down Expand Up @@ -616,7 +614,7 @@ export function authRoutes(fastify: FastifyInstance) {
});

if (check?.isAdmin === false) {
reply.code(401).send({
return reply.code(401).send({
message: "Unauthorized",
});
}
Expand Down