mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-28 07:46:36 +00:00
added utils for unauth, verify, and response
This commit is contained in:
@@ -7,10 +7,10 @@ import response from "@server/utils/response";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import createHttpError from "http-errors";
|
||||
import { z } from "zod";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { decodeHex } from "oslo/encoding";
|
||||
import { TOTPController } from "oslo/otp";
|
||||
import { z } from "zod";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
export const loginBodySchema = z.object({
|
||||
email: z.string().email(),
|
||||
@@ -45,15 +45,13 @@ export async function login(
|
||||
const sessionId = req.cookies[lucia.sessionCookieName];
|
||||
const { session: existingSession } = await lucia.validateSession(sessionId);
|
||||
if (existingSession) {
|
||||
return res.status(HttpCode.OK).send(
|
||||
response<null>({
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Already logged in",
|
||||
status: HttpCode.OK,
|
||||
}),
|
||||
);
|
||||
return response<null>(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Already logged in",
|
||||
status: HttpCode.OK,
|
||||
});
|
||||
}
|
||||
|
||||
const existingUserRes = await db
|
||||
@@ -89,15 +87,13 @@ export async function login(
|
||||
|
||||
if (existingUser.twoFactorEnabled) {
|
||||
if (!code) {
|
||||
return res.status(HttpCode.ACCEPTED).send(
|
||||
response<{ codeRequested: boolean }>({
|
||||
data: { codeRequested: true },
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Two-factor authentication required",
|
||||
status: HttpCode.ACCEPTED,
|
||||
}),
|
||||
);
|
||||
return response<{ codeRequested: boolean }>(res, {
|
||||
data: { codeRequested: true },
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Two-factor authentication required",
|
||||
status: HttpCode.ACCEPTED,
|
||||
});
|
||||
}
|
||||
|
||||
if (!existingUser.twoFactorSecret) {
|
||||
@@ -131,13 +127,11 @@ export async function login(
|
||||
lucia.createSessionCookie(session.id).serialize(),
|
||||
);
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response<null>({
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged in successfully",
|
||||
status: HttpCode.OK,
|
||||
}),
|
||||
);
|
||||
return response<null>(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged in successfully",
|
||||
status: HttpCode.OK,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,13 +23,11 @@ export async function logout(
|
||||
await lucia.invalidateSession(sessionId);
|
||||
res.setHeader("Set-Cookie", lucia.createBlankSessionCookie().serialize());
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response<null>({
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged out successfully",
|
||||
status: HttpCode.OK,
|
||||
}),
|
||||
);
|
||||
return response<null>(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged out successfully",
|
||||
status: HttpCode.OK,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -28,7 +28,11 @@ export const signupBodySchema = z.object({
|
||||
|
||||
export type SignUpBody = z.infer<typeof signupBodySchema>;
|
||||
|
||||
export async function signup(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
export async function signup(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
): Promise<any> {
|
||||
const parsedBody = signupBodySchema.safeParse(req.body);
|
||||
|
||||
if (!parsedBody.success) {
|
||||
@@ -64,15 +68,13 @@ export async function signup(req: Request, res: Response, next: NextFunction): P
|
||||
lucia.createSessionCookie(session.id).serialize(),
|
||||
);
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response<null>({
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "User created successfully",
|
||||
status: HttpCode.OK,
|
||||
}),
|
||||
);
|
||||
return response<null>(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "User created successfully",
|
||||
status: HttpCode.OK,
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof SqliteError && e.code === "SQLITE_CONSTRAINT_UNIQUE") {
|
||||
return next(
|
||||
|
||||
42
server/routers/auth/verifyTotp.ts
Normal file
42
server/routers/auth/verifyTotp.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import createHttpError from "http-errors";
|
||||
import { z } from "zod";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { decodeHex } from "oslo/encoding";
|
||||
import { TOTPController } from "oslo/otp";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import { verifySession, lucia, unauthorized } from "@server/auth";
|
||||
|
||||
export const verifyTotpBody = z.object({
|
||||
code: z.string(),
|
||||
});
|
||||
|
||||
export type VerifyTotpBody = z.infer<typeof verifyTotpBody>;
|
||||
|
||||
export type VerifyTotpResponse = {
|
||||
valid: boolean;
|
||||
};
|
||||
|
||||
export async function verifyTotp(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
): Promise<any> {
|
||||
const parsedBody = verifyTotpBody.safeParse(req.body);
|
||||
|
||||
if (!parsedBody.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedBody.error).toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const { code } = parsedBody.data;
|
||||
|
||||
const { session, user } = await verifySession(req);
|
||||
if (!session) {
|
||||
return unauthorized();
|
||||
}
|
||||
}
|
||||
@@ -47,3 +47,4 @@ unauthenticated.use("/auth", authRouter);
|
||||
authRouter.put("/signup", auth.signup);
|
||||
authRouter.post("/login", auth.login);
|
||||
authRouter.post("/logout", auth.logout);
|
||||
authRouter.post("/verify-totp", auth.logout);
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
|
||||
// define zod type here
|
||||
|
||||
export async function createSite(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
return res.status(HttpCode.OK).send(
|
||||
response<null>({
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged in successfully",
|
||||
status: HttpCode.OK,
|
||||
}),
|
||||
);
|
||||
export async function createSite(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
): Promise<any> {
|
||||
return response<null>(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged in successfully",
|
||||
status: HttpCode.OK,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user