mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-28 06:34:50 +02:00
Fix login stuff?
This commit is contained in:
parent
06eb1544f4
commit
d144704066
11 changed files with 76 additions and 37 deletions
11
bruno/Auth/signup.bru
Normal file
11
bruno/Auth/signup.bru
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
meta {
|
||||||
|
name: signup
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url:
|
||||||
|
body: none
|
||||||
|
auth: none
|
||||||
|
}
|
18
bruno/Auth/verify-email.bru
Normal file
18
bruno/Auth/verify-email.bru
Normal 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
11
bruno/Users/getUser.bru
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
meta {
|
||||||
|
name: getUser
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url:
|
||||||
|
body: none
|
||||||
|
auth: none
|
||||||
|
}
|
|
@ -93,7 +93,8 @@ authenticated.delete(
|
||||||
|
|
||||||
authenticated.get("/users", user.listUsers);
|
authenticated.get("/users", user.listUsers);
|
||||||
// authenticated.get("/org/:orgId/users", user.???); // TODO: Implement this
|
// 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);
|
authenticated.delete("/user/:userId", user.deleteUser);
|
||||||
|
|
||||||
// Auth routes
|
// Auth routes
|
||||||
|
|
|
@ -9,29 +9,20 @@ import createHttpError from 'http-errors';
|
||||||
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
||||||
import logger from '@server/logger';
|
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> {
|
export async function getUser(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const parsedParams = getUserSchema.safeParse(req.params);
|
const userId = req.user?.id;
|
||||||
if (!parsedParams.success) {
|
|
||||||
return next(
|
if (!userId) {
|
||||||
createHttpError(
|
return next(createHttpError(HttpCode.UNAUTHORIZED, "User not found"));
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
parsedParams.error.errors.map(e => e.message).join(', ')
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { userId } = parsedParams.data;
|
// // Check if the user has permission to list sites
|
||||||
|
// const hasPermission = await checkUserActionPermission(ActionsEnum.getUser, req);
|
||||||
// Check if the user has permission to list sites
|
// if (!hasPermission) {
|
||||||
const hasPermission = await checkUserActionPermission(ActionsEnum.getUser, req);
|
// return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to list sites'));
|
||||||
if (!hasPermission) {
|
// }
|
||||||
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to list sites'));
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await db.select()
|
const user = await db.select()
|
||||||
.from(users)
|
.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, {
|
return response(res, {
|
||||||
data: userWithoutPassword,
|
data: {
|
||||||
|
email: user[0].email,
|
||||||
|
twoFactorEnabled: user[0].twoFactorEnabled,
|
||||||
|
emailVerified: user[0].emailVerified
|
||||||
|
},
|
||||||
success: true,
|
success: true,
|
||||||
error: false,
|
error: false,
|
||||||
message: "User retrieved successfully",
|
message: "User retrieved successfully",
|
||||||
|
|
|
@ -2,8 +2,8 @@ import LoginForm from "@app/components/LoginForm";
|
||||||
import { verifySession } from "@app/lib/verifySession";
|
import { verifySession } from "@app/lib/verifySession";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
export async function Page() {
|
export default async function Page() {
|
||||||
const { user } = await verifySession();
|
const user = await verifySession();
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
redirect("/");
|
redirect("/");
|
||||||
|
@ -15,6 +15,3 @@ export async function Page() {
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Page;
|
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { LandingProvider } from "@app/providers/LandingProvider";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
export default async function Page() {
|
export default async function Page() {
|
||||||
const { user } = await verifySession();
|
const user = await verifySession();
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
redirect("/auth/login");
|
redirect("/auth/login");
|
||||||
|
@ -12,7 +12,7 @@ export default async function Page() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<LandingProvider user={user}>
|
<LandingProvider user={user}>
|
||||||
<p>You're logged in!</p>
|
<p>You are logged in!</p>
|
||||||
</LandingProvider>
|
</LandingProvider>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
@ -25,7 +25,7 @@ import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||||
import { ExclamationTriangleIcon } from "@radix-ui/react-icons";
|
import { ExclamationTriangleIcon } from "@radix-ui/react-icons";
|
||||||
import { LoginResponse } from "@server/routers/auth";
|
import { LoginResponse } from "@server/routers/auth";
|
||||||
import { api } from "@app/api";
|
import { api } from "@app/api";
|
||||||
import { useParams, useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
type LoginFormProps = {
|
type LoginFormProps = {
|
||||||
redirect?: string;
|
redirect?: string;
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
import { createContext } from "react";
|
import { createContext } from "react";
|
||||||
|
|
||||||
export const UserContext = createContext<{ id: string } | null>(null);
|
export const UserContext = createContext<boolean | null>(null);
|
||||||
|
|
|
@ -1,8 +1,17 @@
|
||||||
|
import api from "@app/api";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import lucia from "@server/auth";
|
|
||||||
|
|
||||||
export async function verifySession() {
|
export async function verifySession() {
|
||||||
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
|
const sessionId = cookies().get("session")?.value ?? null;
|
||||||
const session = await lucia.validateSession(sessionId || "");
|
|
||||||
return session;
|
try {
|
||||||
|
const res = await api.get("/user", {
|
||||||
|
headers: {
|
||||||
|
Cookie: `session=${sessionId}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { UserContext } from "@app/contexts/userContext";
|
||||||
import { ReactNode } from "react";
|
import { ReactNode } from "react";
|
||||||
|
|
||||||
type LandingProviderProps = {
|
type LandingProviderProps = {
|
||||||
user: any;
|
user: boolean ;
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue