Skip to content

Commit

Permalink
🏷️ fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
miicolas committed Jul 29, 2024
1 parent 020674a commit 82a2fae
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
12 changes: 5 additions & 7 deletions src/components/cards/cardBlog.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { Button } from "@/components/ui/button";
import Link from "next/link";
import getSession from "@/lib/session";
import { Session, CardBlogProps } from "@/lib/types";




type CardBlogProps = {
title: string;
description: string;
date: string;
slug: string;
};

export default async function CardBlog({
title,
description,
date,
slug,
}: CardBlogProps) {
const session = await getSession();
const session: Session = await getSession();

return (
<>
Expand Down
51 changes: 27 additions & 24 deletions src/components/layout/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,40 @@ import { Avatar, AvatarImage } from "@/components/ui/avatar";
import { Button } from "@/components/ui/button";
import Link from "next/link";
import getSession from "@/lib/session";
import { Session } from "@/lib/types";

export default async function Header() {
const session: Session = await getSession();

// Default values for potential null or undefined
const user = session?.user;
const image = user?.image || ""; // Provide a default empty string if image is null or undefined
const isAdmin = user?.isAdmin ?? false; // Default to false if isAdmin is undefined

export default async function Header() {
const session = await getSession();
return (
<header className="flex flex-row justify-between items-center">
<h1 className="text-2xl font-bold">Weekly Journal Nicolas'</h1>
<div className="flex flex-row gap-4">
{!session && (
<LoginGithubButton />
)}

{session && (
<>
<div className="flex flex-row gap-4">
<Avatar className="border-2 border-gray-200">
<AvatarImage src={session.user.image} />
</Avatar>
<LogoutButton />
{session.user.isAdmin && (
<Link href="/blog/create">
<Button>
Add Post
</Button>
</Link>
)}
</div>
</>
)}
{!user ? (
<LoginGithubButton />
) : (
<>
<div className="flex flex-row gap-4">
<Avatar className="border-2 border-gray-200">
<AvatarImage src={image} />
</Avatar>
<LogoutButton />
{isAdmin && (
<Link href="/blog/create">
<Button>
Add Post
</Button>
</Link>
)}
</div>
</>
)}
</div>
</header>
);
}
}
20 changes: 15 additions & 5 deletions src/lib/session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { auth } from "@/lib/auth"
import { auth } from "@/lib/auth";
import { Session } from "@/lib/types";

export default async function getSession() {
const session = await auth()
export default async function getSession(): Promise<Session> {
const session = await auth();

if (!session) {
return {
user: {
isAdmin: false,
image: "",
},
} as Session;
}

return session
}
return session;
}
16 changes: 16 additions & 0 deletions src/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type User = {
isAdmin?: boolean;
image?: string | null;
};

export type Session = {
user?: User;
} | null;


export type CardBlogProps = {
title: string;
description: string;
date: string;
slug: string;
};

0 comments on commit 82a2fae

Please sign in to comment.