Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added apps/X/app/favicon.ico
Binary file not shown.
7 changes: 6 additions & 1 deletion apps/X/app/home/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { CenterComp, HomeLeft, HomeRight } from "@/components/ui";
"use client";

import { HomeRight, Button, CenterComp, HomeLeft } from "@/components/ui";
import { signOut } from "next-auth/react";
const homepage = () => {
const handleLogout = async () => {
await signOut({ callbackUrl: "/" });
};
return (
<div>
<HomeRight />
Expand Down
6 changes: 0 additions & 6 deletions apps/X/app/icon.svg

This file was deleted.

9 changes: 5 additions & 4 deletions apps/X/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Metadata } from "next";
import localFont from "next/font/local";
import "./globals.css";
import { Provieder } from "./provider";
import { Provider } from "./provider";
import { RouteGard } from "@/components/RouteGuard";

const geistSans = localFont({
src: "./fonts/GeistVF.woff",
Expand All @@ -24,11 +25,11 @@ export default function RootLayout({
}>) {
return (
<html lang="en" className="dark">
<Provieder>
<Provider>
<body className={`${geistSans.variable} ${geistMono.variable} m-0`}>
{children}
<RouteGard>{children}</RouteGard>
</body>
</Provieder>
</Provider>
</html>
);
}
26 changes: 18 additions & 8 deletions apps/X/app/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { JWT } from "next-auth/jwt";
import CredentialsProvider from "next-auth/providers/credentials";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import { signOut } from "next-auth/react";
import z from "zod";

interface credentialsTypes {
Expand All @@ -15,8 +16,8 @@ interface credentialsTypes {

const userInput = z.object({
username: z.string(),
name: z.string(),
email: z.string().email(),
name: z.string().optional(),
email: z.string().email().optional(),
password: z.string(),
});

Expand Down Expand Up @@ -52,7 +53,11 @@ export const authOptions = {
name: creds.name,
});
};

const validationResult = validatedUserInput(credentials);
if (!validationResult.success) {
console.log("Validation failed", validationResult.error);
return null;
}
if (!validatedUserInput) return null;

const hashedPassword = await bcrypt.hash(credentials.password, 10);
Expand All @@ -70,7 +75,6 @@ export const authOptions = {
credentials.password,
existingUser.password
);
console.log("This is the password", passwordValidation);
if (passwordValidation) {
console.log("This is userEmail", existingUser.email);
console.log("This is username", existingUser.username);
Expand All @@ -80,8 +84,9 @@ export const authOptions = {
email: existingUser.email,
name: existingUser.name,
};
} else {
console.log("Invalid password for existing user");
}

console.log("This is name", existingUser.name);
} catch (error) {
console.log("Error while LogIn", error);
Expand Down Expand Up @@ -119,6 +124,8 @@ export const authOptions = {
async jwt({ token, user }: any) {
if (user) {
token.id = user.id;
token.username = user.username;
token.email = user.email;
}
return token;
},
Expand All @@ -127,14 +134,17 @@ export const authOptions = {
// where: { id: token.sub },
// });

if (token) {
session.user.id = token.sub;
if (token && session.user) {
session.user.id = token.id || null;
session.user.username = token.username || null;
session.user.email = token.email || null;
}

return session;
},
},
pages: {
signIn: "/signin",
error: "/error",
signOut: "/signin",
},
};
11 changes: 5 additions & 6 deletions apps/X/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { redirect } from "next/navigation";

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

if (!session) {
console.log("No session,redirecting to SIGNIN");
if (!session?.user?.id) {
console.log("No valid session, redirecting to signin");
redirect("/signin");
}
if (session.user && session.user.id) {
redirect("/home");
}
return <div>Loading......</div>;
console.log("Valid session detected, redirecting to /home");
redirect("/home");
};

export default Page;
2 changes: 1 addition & 1 deletion apps/X/app/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { SessionProvider } from "next-auth/react";

export const Provieder = ({ children }: { children: React.ReactNode }) => {
export const Provider = ({ children }: { children: React.ReactNode }) => {
return <SessionProvider>{children}</SessionProvider>;
};
38 changes: 38 additions & 0 deletions apps/X/src/components/RouteGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use client";
import { useSession } from "next-auth/react";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";

const publicPaths = ["/signin", "/login", "/error"];

export function RouteGard({ children }: { children: React.ReactNode }) {
const { data: session, status } = useSession();
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
if (status == "loading") {
return;
}
if (!session && !publicPaths.includes(pathname)) {
router.push("/signin");
}
}, [session, status, router, pathname]);

if (status == "loading") {
return (
<div className="flex justify-center items-center h-screen ">
<svg
viewBox="0 0 24 24"
aria-hidden="true"
className=" w-24 h-24 fill-current text-black dark:text-white"
>
<g>
<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>
</g>
</svg>
</div>
);
}

return <>{children}</>;
}
1 change: 1 addition & 0 deletions apps/X/src/components/home/HomeLeft.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client"
export const HomeLeft = () => {
return <div>Home Left</div>;
};
1 change: 1 addition & 0 deletions apps/X/src/components/home/HomeRight.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client"
export const HomeRight = () => {
return <div>Home Right Side</div>;
};
2 changes: 1 addition & 1 deletion apps/X/src/components/ui/X_logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const X_logo = () => {
<svg
viewBox="0 0 24 24"
aria-hidden="true"
className=" w-6 h-6 fill-current text-black dark:text-white"
className=" w-min-6 h-min-6 fill-current text-black dark:text-white"
>
<g>
<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>
Expand Down
4 changes: 2 additions & 2 deletions apps/X/src/components/ui/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SigninRightCom } from "./SigninRightCom";
import { SigninComp } from "./SigninComp";
import { Button } from "@repo/ui/button";
import { X_logoBIG } from "./X_logoBIG";
import { Dialog } from "./dialog";
import { X_logo } from "./X_logo";
Expand All @@ -12,12 +11,13 @@ import { HomeLeft } from "../home/HomeLeft";
import { CenterComp } from "../home/HomeCenterComp";
import { TweetComp } from "./TweetComp";
import { UserAvatar } from "./usrAvatar";
import { Button } from "./button";

export {
SigninComp,
Button,
UserAvatar,
SigninRightCom,
Button,
X_logoBIG,
Dialog,
TweetComp,
Expand Down
Loading