Fix login stuff?

This commit is contained in:
Owen Schwartz 2024-10-06 18:43:20 -04:00
parent 06eb1544f4
commit d144704066
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
11 changed files with 76 additions and 37 deletions

11
bruno/Auth/signup.bru Normal file
View file

@ -0,0 +1,11 @@
meta {
name: signup
type: http
seq: 2
}
get {
url:
body: none
auth: none
}

View file

@ -0,0 +1,18 @@
meta {
name: verify-email
type: http
seq: 3
}
put {
url: http://localhost:3000/api/v1/auth/signup
body: json
auth: none
}
body:json {
{
"email": "owen@fossorial.io",
"password": "Password123!"
}
}

11
bruno/Users/getUser.bru Normal file
View file

@ -0,0 +1,11 @@
meta {
name: getUser
type: http
seq: 1
}
get {
url:
body: none
auth: none
}

View file

@ -93,7 +93,8 @@ authenticated.delete(
authenticated.get("/users", user.listUsers);
// authenticated.get("/org/:orgId/users", user.???); // TODO: Implement this
authenticated.get("/user/:userId", user.getUser);
authenticated.get("/user", user.getUser);
// authenticated.get("/user/:userId", user.getUser);
authenticated.delete("/user/:userId", user.deleteUser);
// Auth routes

View file

@ -9,29 +9,20 @@ import createHttpError from 'http-errors';
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
import logger from '@server/logger';
const getUserSchema = z.object({
userId: z.string().uuid()
});
export async function getUser(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const parsedParams = getUserSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
parsedParams.error.errors.map(e => e.message).join(', ')
)
);
const userId = req.user?.id;
if (!userId) {
return next(createHttpError(HttpCode.UNAUTHORIZED, "User not found"));
}
const { userId } = parsedParams.data;
// Check if the user has permission to list sites
const hasPermission = await checkUserActionPermission(ActionsEnum.getUser, req);
if (!hasPermission) {
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to list sites'));
}
// // Check if the user has permission to list sites
// const hasPermission = await checkUserActionPermission(ActionsEnum.getUser, req);
// if (!hasPermission) {
// return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to list sites'));
// }
const user = await db.select()
.from(users)
@ -47,11 +38,12 @@ export async function getUser(req: Request, res: Response, next: NextFunction):
);
}
// Remove passwordHash from the response
const { passwordHash: _, ...userWithoutPassword } = user[0];
return response(res, {
data: userWithoutPassword,
data: {
email: user[0].email,
twoFactorEnabled: user[0].twoFactorEnabled,
emailVerified: user[0].emailVerified
},
success: true,
error: false,
message: "User retrieved successfully",

View file

@ -2,8 +2,8 @@ import LoginForm from "@app/components/LoginForm";
import { verifySession } from "@app/lib/verifySession";
import { redirect } from "next/navigation";
export async function Page() {
const { user } = await verifySession();
export default async function Page() {
const user = await verifySession();
if (user) {
redirect("/");
@ -15,6 +15,3 @@ export async function Page() {
</>
);
}
export default Page;

View file

@ -3,7 +3,7 @@ import { LandingProvider } from "@app/providers/LandingProvider";
import { redirect } from "next/navigation";
export default async function Page() {
const { user } = await verifySession();
const user = await verifySession();
if (!user) {
redirect("/auth/login");
@ -12,7 +12,7 @@ export default async function Page() {
return (
<>
<LandingProvider user={user}>
<p>You're logged in!</p>
<p>You are logged in!</p>
</LandingProvider>
</>
);

View file

@ -25,7 +25,7 @@ import { Alert, AlertDescription } from "@/components/ui/alert";
import { ExclamationTriangleIcon } from "@radix-ui/react-icons";
import { LoginResponse } from "@server/routers/auth";
import { api } from "@app/api";
import { useParams, useRouter } from "next/navigation";
import { useRouter } from "next/navigation";
type LoginFormProps = {
redirect?: string;

View file

@ -1,3 +1,3 @@
import { createContext } from "react";
export const UserContext = createContext<{ id: string } | null>(null);
export const UserContext = createContext<boolean | null>(null);

View file

@ -1,8 +1,17 @@
import api from "@app/api";
import { cookies } from "next/headers";
import lucia from "@server/auth";
export async function verifySession() {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
const session = await lucia.validateSession(sessionId || "");
return session;
const sessionId = cookies().get("session")?.value ?? null;
try {
const res = await api.get("/user", {
headers: {
Cookie: `session=${sessionId}`
}
});
return true;
} catch {
return false
}
}

View file

@ -4,7 +4,7 @@ import { UserContext } from "@app/contexts/userContext";
import { ReactNode } from "react";
type LandingProviderProps = {
user: any;
user: boolean ;
children: ReactNode;
};