Skip to content

Commit 6a436d0

Browse files
committed
Procted routes
1 parent 2986c21 commit 6a436d0

File tree

12 files changed

+78
-29
lines changed

12 files changed

+78
-29
lines changed

apps/X/app/favicon.ico

172 KB
Binary file not shown.

apps/X/app/home/page.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { CenterComp, HomeLeft, HomeRight } from "@/components/ui";
1+
"use client";
22

3+
import { HomeRight, Button, CenterComp, HomeLeft } from "@/components/ui";
4+
import { signOut } from "next-auth/react";
35
const homepage = () => {
6+
const handleLogout = async () => {
7+
await signOut({ callbackUrl: "/" });
8+
};
49
return (
510
<div>
611
<HomeRight />

apps/X/app/icon.svg

Lines changed: 0 additions & 6 deletions
This file was deleted.

apps/X/app/layout.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Metadata } from "next";
22
import localFont from "next/font/local";
33
import "./globals.css";
4-
import { Provieder } from "./provider";
4+
import { Provider } from "./provider";
5+
import { RouteGard } from "@/components/RouteGuard";
56

67
const geistSans = localFont({
78
src: "./fonts/GeistVF.woff",
@@ -24,11 +25,11 @@ export default function RootLayout({
2425
}>) {
2526
return (
2627
<html lang="en" className="dark">
27-
<Provieder>
28+
<Provider>
2829
<body className={`${geistSans.variable} ${geistMono.variable} m-0`}>
29-
{children}
30+
<RouteGard>{children}</RouteGard>
3031
</body>
31-
</Provieder>
32+
</Provider>
3233
</html>
3334
);
3435
}

apps/X/app/lib/auth.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { JWT } from "next-auth/jwt";
44
import CredentialsProvider from "next-auth/providers/credentials";
55
import GithubProvider from "next-auth/providers/github";
66
import GoogleProvider from "next-auth/providers/google";
7+
import { signOut } from "next-auth/react";
78
import z from "zod";
89

910
interface credentialsTypes {
@@ -15,8 +16,8 @@ interface credentialsTypes {
1516

1617
const userInput = z.object({
1718
username: z.string(),
18-
name: z.string(),
19-
email: z.string().email(),
19+
name: z.string().optional(),
20+
email: z.string().email().optional(),
2021
password: z.string(),
2122
});
2223

@@ -52,7 +53,11 @@ export const authOptions = {
5253
name: creds.name,
5354
});
5455
};
55-
56+
const validationResult = validatedUserInput(credentials);
57+
if (!validationResult.success) {
58+
console.log("Validation failed", validationResult.error);
59+
return null;
60+
}
5661
if (!validatedUserInput) return null;
5762

5863
const hashedPassword = await bcrypt.hash(credentials.password, 10);
@@ -70,7 +75,6 @@ export const authOptions = {
7075
credentials.password,
7176
existingUser.password
7277
);
73-
console.log("This is the password", passwordValidation);
7478
if (passwordValidation) {
7579
console.log("This is userEmail", existingUser.email);
7680
console.log("This is username", existingUser.username);
@@ -80,8 +84,9 @@ export const authOptions = {
8084
email: existingUser.email,
8185
name: existingUser.name,
8286
};
87+
} else {
88+
console.log("Invalid password for existing user");
8389
}
84-
8590
console.log("This is name", existingUser.name);
8691
} catch (error) {
8792
console.log("Error while LogIn", error);
@@ -119,6 +124,8 @@ export const authOptions = {
119124
async jwt({ token, user }: any) {
120125
if (user) {
121126
token.id = user.id;
127+
token.username = user.username;
128+
token.email = user.email;
122129
}
123130
return token;
124131
},
@@ -127,14 +134,17 @@ export const authOptions = {
127134
// where: { id: token.sub },
128135
// });
129136

130-
if (token) {
131-
session.user.id = token.sub;
137+
if (token && session.user) {
138+
session.user.id = token.id || null;
139+
session.user.username = token.username || null;
140+
session.user.email = token.email || null;
132141
}
133-
134142
return session;
135143
},
136144
},
137145
pages: {
138146
signIn: "/signin",
147+
error: "/error",
148+
signOut: "/signin",
139149
},
140150
};

apps/X/app/page.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { redirect } from "next/navigation";
44

55
const Page = async () => {
66
const session = await getServerSession(authOptions);
7+
console.log(session, "This is the session log");
78

8-
if (!session) {
9-
console.log("No session,redirecting to SIGNIN");
9+
if (!session?.user?.id) {
10+
console.log("No valid session, redirecting to signin");
1011
redirect("/signin");
1112
}
12-
if (session.user && session.user.id) {
13-
redirect("/home");
14-
}
15-
return <div>Loading......</div>;
13+
console.log("Valid session detected, redirecting to /home");
14+
redirect("/home");
1615
};
1716

1817
export default Page;

apps/X/app/provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22
import { SessionProvider } from "next-auth/react";
33

4-
export const Provieder = ({ children }: { children: React.ReactNode }) => {
4+
export const Provider = ({ children }: { children: React.ReactNode }) => {
55
return <SessionProvider>{children}</SessionProvider>;
66
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use client";
2+
import { useSession } from "next-auth/react";
3+
import { usePathname, useRouter } from "next/navigation";
4+
import { useEffect } from "react";
5+
6+
const publicPaths = ["/signin", "/login", "/error"];
7+
8+
export function RouteGard({ children }: { children: React.ReactNode }) {
9+
const { data: session, status } = useSession();
10+
const router = useRouter();
11+
const pathname = usePathname();
12+
useEffect(() => {
13+
if (status == "loading") {
14+
return;
15+
}
16+
if (!session && !publicPaths.includes(pathname)) {
17+
router.push("/signin");
18+
}
19+
}, [session, status, router, pathname]);
20+
21+
if (status == "loading") {
22+
return (
23+
<div className="flex justify-center items-center h-screen ">
24+
<svg
25+
viewBox="0 0 24 24"
26+
aria-hidden="true"
27+
className=" w-24 h-24 fill-current text-black dark:text-white"
28+
>
29+
<g>
30+
<path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"></path>
31+
</g>
32+
</svg>
33+
</div>
34+
);
35+
}
36+
37+
return <>{children}</>;
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use client"
12
export const HomeLeft = () => {
23
return <div>Home Left</div>;
34
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use client"
12
export const HomeRight = () => {
23
return <div>Home Right Side</div>;
34
};

0 commit comments

Comments
 (0)