Skip to content

Commit 3b3972d

Browse files
Fill navbar w/ real auth data (#42)
* barebone * commit --------- Co-authored-by: Joshua Silva <72359611+joshuasilva414@users.noreply.github.com>
1 parent 97afd3a commit 3b3972d

File tree

6 files changed

+97
-45
lines changed

6 files changed

+97
-45
lines changed

drizzle.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export default defineConfig({
66
dialect: "turso",
77
out: "./drizzle",
88
dbCredentials: {
9-
url: process.env.TURSO_DATABASE_URL!,
9+
url: process.env.DATABASE_URL!,
10+
//url: process.env.TURSO_DATABASE_URL!,
1011
authToken: process.env.TURSO_AUTH_TOKEN!,
1112
},
1213
});

src/app/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Metadata } from "next";
22
import { Geist, Geist_Mono } from "next/font/google";
33
import "./globals.css";
44
import Footer from "@/components/footer";
5+
import Navigation from "@/components/navbar"
56
import { Toaster } from "sonner";
67

78
const geistSans = Geist({
@@ -29,6 +30,7 @@ export default function RootLayout({
2930
<body
3031
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
3132
>
33+
<Navigation />
3234
{children}
3335
<Toaster />
3436
<Footer />

src/app/page.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import Image from "next/image";
22
import Link from "next/link";
3-
import Navigation from "@/components/navbar"
4-
3+
import Navigation from "@/components/navbar";
4+
import AutoSignInTestComponent from "@/components/autoSignIn";
55
export default function Home() {
6-
var isSignedIn = false;
7-
var mockUser = null;
8-
96
return (
10-
<div className="min-h-screen bg-gradient-to-b from-slate-50 via-white to-white text-slate-900">
11-
<header className="border-b border-slate-200 bg-white/70 backdrop-blur">
12-
<Navigation isSignedIn={isSignedIn}/> {}
13-
</header>
14-
</div>
7+
<div className="parent">
8+
<div>I suppose tis is the main page</div>
9+
{/*
10+
<AutoSignInTestComponent />
11+
*/} </div>
1512
);
16-
}
13+
}

src/components/autoSignIn.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use client";
2+
import { useEffect } from 'react';
3+
import { authClient } from '@/lib/auth-client'; // Adjust path to your auth client instance
4+
5+
interface AutoSignInProps {
6+
account_id?: string;
7+
password?: string;
8+
callbackUrl?: string;
9+
}
10+
11+
/**
12+
* A component for testing that automatically attempts to sign in on mount.
13+
*/
14+
const AutoSignInTestComponent = ({
15+
account_id = "test", // Default test user
16+
password = "test", // Default test password
17+
callbackUrl = "/dashboard" // Redirect location on success
18+
}: AutoSignInProps) => {
19+
20+
useEffect(() => {
21+
const signIn = async () => {
22+
console.log(`Attempting to sign in test user: ${account_id}`);
23+
24+
const { error } = await authClient.signIn({
25+
account_id,
26+
password,
27+
callbackU: callbackUrl,
28+
});
29+
30+
if (error) {
31+
console.error("Auto sign-in failed:", error.message);
32+
33+
} else {
34+
console.log("Auto sign-in successful!");
35+
}
36+
};
37+
38+
signIn();
39+
}, [account_id, password, callbackUrl]); // Re-run if props change (though unlikely in this context)
40+
41+
return (
42+
<div>
43+
{/* Visual indicator for your tests */}
44+
<p>Running auto sign-in logic...</p>
45+
</div>
46+
);
47+
};
48+
49+
export default AutoSignInTestComponent;

src/components/navbar.tsx

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"use client";
2-
import { useEffect, useRef, useState } from "react";
32
import Link from "next/link";
43
import {
54
DropdownMenu,
@@ -11,20 +10,24 @@ import {
1110
} from "@/components/ui/dropdown-menu";
1211
import { Button } from "@/components/ui/button";
1312
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";
14-
type NavigationProps = {
15-
isSignedIn: boolean;
16-
User?: {
17-
name: string;
18-
email: string;
19-
avatar?: string;
20-
profileHref?:string;
21-
coursesHref?:string;
22-
historyHref?: string;
23-
};
24-
};
13+
import { authClient } from "@/lib/auth-client";
14+
const { useSession, signOut } = authClient;
2515

16+
export default function Navigation() {
17+
const { data: session } = useSession();
18+
const user = session?.user;
19+
const isSignedIn = Boolean(user);
20+
const avatarSrc = user?.image ?? "/user.png";
21+
const displayName = user?.name ?? "User";
22+
const email = user?.email ?? "";
2623

27-
export default function Navigation({ isSignedIn, User }: NavigationProps) {
24+
const handleSignOut = async () => {
25+
try {
26+
await signOut();
27+
} catch (error) {
28+
console.error("Failed to sign out", error);
29+
}
30+
};
2831

2932
return (
3033
<div className="p-5" >
@@ -56,51 +59,51 @@ export default function Navigation({ isSignedIn, User }: NavigationProps) {
5659
</Link>
5760
</Button>
5861
</li>
59-
{isSignedIn && User ? (
62+
{isSignedIn && user ? (
6063
<>
6164

6265
<li>
63-
<Button className="text-lg px-4 py-2 border border-border rounded-md transition-all duration-300 hover:bg-muted hover:text-foreground">
64-
<Link
65-
href="/logout"
66+
<Button
67+
className="text-lg px-4 py-2 border border-border rounded-md transition-all duration-300 hover:bg-muted hover:text-foreground"
68+
onClick={handleSignOut}
6669
>
6770
Logout
68-
</Link>
6971
</Button>
7072
</li>
7173
{/* Profile Pic Placeholder */}
7274
<li>
7375
<DropdownMenu>
7476
<DropdownMenuTrigger asChild>
7577
<Avatar className="w-12 h-12 border-2 border-white rounded-full overflow-hidden transition-all duration-300 hover:border-blue-300 hover:drop-shadow-[1px_1px_40px_rgba(255,215,100,1)] cursor-pointer">
76-
<AvatarImage
77-
src={`/${User.avatar ?? "user.png"}`}
78-
alt={User.name}
78+
<AvatarImage
79+
src={avatarSrc}
80+
alt={displayName}
81+
7982
className="object-cover w-full h-full"
8083
/>
81-
<AvatarFallback>{User.name?.charAt(0).toUpperCase() ?? "?"}</AvatarFallback>
84+
<AvatarFallback>{displayName.charAt(0).toUpperCase()}</AvatarFallback>
8285
</Avatar>
8386
</DropdownMenuTrigger>
8487
<DropdownMenuContent align="end" className="w-56">
8588
<DropdownMenuLabel>
86-
<p className="font-semibold">{User.name}</p>
87-
<p className="text-sm text-gray-500">{User.email}</p>
89+
<p className="font-semibold">{displayName}</p>
90+
{email && <p className="text-sm text-gray-500">{email}</p>}
8891
</DropdownMenuLabel>
8992
<DropdownMenuSeparator />
9093
<DropdownMenuItem asChild>
91-
<Link href={User.profileHref ?? "/profile"}>Profile</Link>
94+
<Link href="/profile">Profile</Link>
9295
</DropdownMenuItem>
9396
<DropdownMenuItem asChild>
94-
<Link href={User.coursesHref ?? "/courses"}>My Courses</Link>
97+
<Link href="/courses">My Courses</Link>
9598
</DropdownMenuItem>
9699
<DropdownMenuItem asChild>
97-
<Link href={User.historyHref ?? "/history"}>History</Link>
100+
<Link href="/history">History</Link>
98101
</DropdownMenuItem>
99102
<DropdownMenuSeparator />
100103
<DropdownMenuItem asChild>
101-
<Link href="/logout" className="text-red-600">
104+
<button className="text-red-600" onClick={handleSignOut}>
102105
Logout
103-
</Link>
106+
</button>
104107
</DropdownMenuItem>
105108
</DropdownMenuContent>
106109
</DropdownMenu>
@@ -113,7 +116,7 @@ export default function Navigation({ isSignedIn, User }: NavigationProps) {
113116
<Link
114117
href="/sign-in"
115118
>
116-
Login
119+
Sign in
117120
</Link>
118121
</Button>
119122

@@ -135,9 +138,9 @@ export default function Navigation({ isSignedIn, User }: NavigationProps) {
135138
<DropdownMenuItem asChild className="">
136139
<Button className="px-4 py-2 border border-border rounded-md transition-all duration-300 hover:bg-muted hover:text-foreground">
137140
<Link
138-
href="/login"
141+
href="/sign-in"
139142
>
140-
Login
143+
Sign in
141144
</Link>
142145
</Button>
143146
</DropdownMenuItem>

src/lib/auth-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createAuthClient } from "better-auth/client";
1+
import { createAuthClient } from "better-auth/react";
22
import { inferAdditionalFields } from "better-auth/client/plugins";
33
import { auth } from "./auth";
44

0 commit comments

Comments
 (0)