Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:auth logic #4

Merged
merged 1 commit into from
Feb 8, 2025
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/.next/
/out/

.env

# production
/build

Expand Down
5 changes: 0 additions & 5 deletions .temp.env

This file was deleted.

6 changes: 6 additions & 0 deletions app/(protected)/dashboard/me/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Me = async () => {
await new Promise((resolve) => setTimeout(resolve, 10000));
return <div>Me</div>;
};

export default Me;
14 changes: 11 additions & 3 deletions app/(protected)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import { useEffect } from "react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import Loader from "@/components/ui/Loader";
import Header from "@/components/Header";

export default function MePage() {
export default function MePage({ children }: { children: React.ReactNode }) {
const { data: session, status } = useSession();
const router = useRouter();

Expand All @@ -13,8 +15,14 @@ export default function MePage() {
}
}, [status, router]);

if (status === "loading") return <p>Loading...</p>;
if (status === "loading") return <Loader />;
if (!session) return null;

return <h1>Welcome, {session.user?.name}!</h1>;

return (
<main className="flex flex-col h-screen">
<Header />
<div className="flex-grow container mx-auto">{children}</div>
</main>
);
}
5 changes: 0 additions & 5 deletions app/(protected)/me/page.tsx

This file was deleted.

10 changes: 6 additions & 4 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export const authConfig = {
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const res = await axios.post(`${process.env.BACKEND_URL}/login`, {
const res = await axios.post(`${process.env.NEXT_PUBLIC_API_URL}/login`, {
// const res = await axios.post(`http://localhost8080/login`, {
username: credentials?.username,
password: credentials?.password,
});

console.log("res", res.data);
const user = res.data.data;

if (res.status && user) {
Expand Down Expand Up @@ -69,7 +71,7 @@ export const authConfig = {
if (account?.provider === "google") {
// Call your backend API to find or register the user
try {
const res = await axios.post(`${process.env.BACKEND_URL}/api/v1/oauth`, {
const res = await axios.post(`${process.env.API_URL}/api/v1/oauth`, {
email: profile?.email,
name: profile?.name,
googleId: profile?.sub,
Expand All @@ -91,7 +93,7 @@ export const authConfig = {
if (account?.provider === "github") {
// Call your backend API to find or register the user
try {
const res = await axios.post(`${process.env.BACKEND_URL}/api/v1/oauth`, {
const res = await axios.post(`${process.env.API_URL}/api/v1/oauth`, {
email: profile?.email,
name: profile?.name,
githubId: profile?.sub,
Expand All @@ -117,7 +119,7 @@ export const authConfig = {

// Custom pages (optional)
pages: {
signIn: "/auth/signin", // Custom sign-in page
signIn: "/signin", // Custom sign-in page
},
} satisfies NextAuthOptions;

Expand Down
13 changes: 0 additions & 13 deletions app/api/auth/googleAuth.ts

This file was deleted.

49 changes: 0 additions & 49 deletions app/api/auth/login/route.ts

This file was deleted.

52 changes: 0 additions & 52 deletions app/api/auth/signup/route.ts

This file was deleted.

24 changes: 11 additions & 13 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import Header from "@/components/Header";
import AuthProvider from "@/components/auth/AuthProvider";
import { Toaster } from "react-hot-toast";

const geistSans = Geist({
variable: "--font-geist-sans",
Expand All @@ -25,17 +25,15 @@ export default function RootLayout({
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<main className="flex flex-col h-screen">
<AuthProvider>
<Header />
<div className="flex-grow container mx-auto">{children}</div>
</AuthProvider>
</main>
</body>
</html>
<html lang="en">
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
<main className="flex flex-col h-screen">
<AuthProvider>
<div className="flex-grow container mx-auto">{children}</div>
</AuthProvider>
<Toaster position="top-center" reverseOrder={false} />
</main>
</body>
</html>
);
}
9 changes: 9 additions & 0 deletions app/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Loader from "@/components/ui/Loader";

export default function Loading() {
return (
<div className="flex items-center justify-center h-screen">
<Loader />
</div>
);
}
10 changes: 0 additions & 10 deletions app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { GalleryVerticalEnd } from "lucide-react";

import { LoginForm } from "@/components/auth/login-form";

export default function LoginPage() {
return (
<div className="flex min-h-svh flex-col items-center justify-center gap-6 bg-muted p-6 md:p-10">
<div className="flex w-full max-w-sm flex-col gap-6">
<a href="#" className="flex items-center gap-2 self-center font-medium">
<div className="flex h-6 w-6 items-center justify-center rounded-md bg-primary text-primary-foreground">
<GalleryVerticalEnd className="size-4" />
</div>
Acme Inc.
</a>
<LoginForm />
</div>
</div>
);
}


7 changes: 5 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Header from "@/components/Header";

export default function Home() {
return (

<h1 className="text-4xl">Welcome to your Next.js app!</h1>
<main className="flex flex-col h-screen">
<Header />
<h1 className="text-4xl">Welcome to your Next.js app!</h1>
</main>
);
}
9 changes: 2 additions & 7 deletions app/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { GalleryVerticalEnd } from "lucide-react";

import { SignupForm } from "@/components/auth/signup-form";


export default function SignPage() {
return (
<div className="flex min-h-svh flex-col items-center justify-center gap-6 bg-muted p-6 md:p-10">
<div className="flex w-full max-w-sm flex-col gap-6">
<a href="#" className="flex items-center gap-2 self-center font-medium">
<div className="flex h-6 w-6 items-center justify-center rounded-md bg-primary text-primary-foreground">
<GalleryVerticalEnd className="size-4" />
</div>
Acme Inc.
</a>
<div className="flex w-full max-w-sm flex-col gap-6">
<SignupForm />
</div>
</div>
Expand Down
Empty file removed auth.config.ts
Empty file.
71 changes: 32 additions & 39 deletions components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,40 @@
"use client";
import { useEffect } from "react";
import useUserStore from "../zustand/userStore";

import { Button } from "./ui/button";
import { useRouter } from "next/navigation";
import useAuthenticate from "../hooks/useAuthenticate";
import { Skeleton } from "@/components/ui/skeleton";

export default function Header() {
const { session, status, isSessionExpired, signOut } = useAuthenticate();
const { user, clearUser } = useUserStore();
const router = useRouter();

useEffect(() => {
if (isSessionExpired) {
clearUser();
}
}, [isSessionExpired, clearUser]);

if (status === "loading") {
return null;
}

return (
<header className="bg-white py-4">
<nav className="flex justify-between items-center container mx-auto">
<div>Welcome, {session?.user?.name || "Guest"}</div>
const { session, status, signOut } = useAuthenticate();
const router = useRouter();

{session?.user ? (
<Button onClick={() => signOut()}>Logout</Button>
) : (
<div className="flex gap-1">
<Button
variant="ghost"
size="lg"
onClick={() => router.push("/signup")}
>
SignUp
</Button>
<Button size="lg" onClick={() => router.push("/login")}>
Login
</Button>
</div>
)}
</nav>
</header>
);
return (
<header className="bg-white py-4">
<nav className="flex justify-between items-center container mx-auto">
{status === "loading" ? (
<Skeleton className="w-32 h-6 rounded-md" />
) : (
<div>Welcome, {session?.user?.name || "Guest"}</div>
)}
{status === "loading" ? (
<div className="flex gap-1">
<Skeleton className="w-20 h-10 rounded-md" />
<Skeleton className="w-20 h-10 rounded-md" />
</div>
) : session?.user ? (
<Button onClick={() => signOut()}>Logout</Button>
) : (
<div className="flex gap-1">
<Button variant="ghost" size="lg" onClick={() => router.push("/signup")}>
SignUp
</Button>
<Button size="lg" onClick={() => router.push("/login")}>
Login
</Button>
</div>
)}
</nav>
</header>
);
}
Loading