mirror of
https://github.com/fosrl/pangolin.git
synced 2025-05-12 21:30:35 +01:00
added utils for unauth, verify, and response
This commit is contained in:
parent
d1e198fe55
commit
44e020784b
11 changed files with 125 additions and 69 deletions
|
@ -1,3 +1,6 @@
|
|||
export * from "./unauthorizedResponse";
|
||||
export * from "./verifySession";
|
||||
|
||||
import { Lucia, TimeSpan } from "lucia";
|
||||
import { DrizzleSQLiteAdapter } from "@lucia-auth/adapter-drizzle";
|
||||
import db from "@server/db";
|
||||
|
|
6
server/auth/unauthorizedResponse.ts
Normal file
6
server/auth/unauthorizedResponse.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
|
||||
export function unauthorized(msg?: string) {
|
||||
return createHttpError(HttpCode.UNAUTHORIZED, msg || "Unauthorized");
|
||||
}
|
9
server/auth/verifySession.ts
Normal file
9
server/auth/verifySession.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { Request } from "express";
|
||||
import { lucia } from "@server/auth";
|
||||
|
||||
export async function verifySession(req: Request) {
|
||||
const res = await lucia.validateSession(
|
||||
req.cookies[lucia.sessionCookieName],
|
||||
);
|
||||
return res;
|
||||
}
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
|
1
server/utils/index.ts
Normal file
1
server/utils/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from "./response";
|
|
@ -1,19 +1,17 @@
|
|||
import { ResponseT } from "@server/types/Response";
|
||||
import { Response } from "express";
|
||||
|
||||
export const response = <T>({
|
||||
data,
|
||||
success,
|
||||
error,
|
||||
message,
|
||||
status,
|
||||
}: ResponseT<T>) => {
|
||||
return {
|
||||
export const response = <T>(
|
||||
res: Response,
|
||||
{ data, success, error, message, status }: ResponseT<T>,
|
||||
) => {
|
||||
return res.status(status).send({
|
||||
data,
|
||||
success,
|
||||
error,
|
||||
message,
|
||||
status,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export default response;
|
||||
|
|
Loading…
Reference in a new issue