Skip to content

Commit

Permalink
chore(session): refactor getting user session
Browse files Browse the repository at this point in the history
  • Loading branch information
OgaDavid committed Jan 2, 2024
1 parent 4ba4cd4 commit 4387486
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 30 deletions.
File renamed without changes.
14 changes: 8 additions & 6 deletions app/(routes)/(dashboard)/_components/dashboard-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { Button } from "@/components/ui/button";
import Link from "next/link";
import UserAvatar from "./dashboard-avatar";
import { usePathname } from "next/navigation";
import { useSession } from "next-auth/react";

const DashboardHeader = () => {

const { data: userSession } = useSession();

const user = userSession?.user

const DashboardHeader = ({
session,
}: {
session: UserSession| undefined;
}) => {
const pathname = usePathname();

const createContent = pathname.match("/dashboard/create");
Expand All @@ -30,7 +32,7 @@ const DashboardHeader = ({
Create a content
</Button>
</Link>
<UserAvatar imageUrl={session?.image} />
<UserAvatar imageUrl={user?.image} />
</span>
</div>
</div>
Expand Down
20 changes: 8 additions & 12 deletions app/(routes)/(dashboard)/_components/dashboard-navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Icons } from "../../../../components/icons";
import UserAvatar from "./dashboard-avatar";
import DashboardNavItem from "./dashboard-nav-item";
import { usePathname } from "next/navigation";
import { useSession } from "next-auth/react";

export const DashboardNavigationItems = [
{
Expand Down Expand Up @@ -49,17 +50,12 @@ export const DashboardNavigationItems = [
},
];

const DashboardNavigation = ({
session,
}: {
session:
| {
name?: string | null | undefined;
email?: string | null | undefined;
image?: string | null | undefined;
}
| undefined;
}) => {
const DashboardNavigation = () => {

const { data: userSession } = useSession();

const user = userSession?.user


const pathname = usePathname();

Expand All @@ -81,7 +77,7 @@ const DashboardNavigation = ({
</div>
</div>
<span className="md:hidden">
<UserAvatar imageUrl={session?.image} />
<UserAvatar imageUrl={user?.image} />
</span>
</div>
<div className="flex items-center max-w-[820px] w-full justify-between mx-auto">
Expand Down
8 changes: 2 additions & 6 deletions app/(routes)/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import { Metadata } from "next";
import DashboardHeader from "@/app/(routes)/(dashboard)/_components/dashboard-header";
import getSession from "@/actions/get-user-session";
import DashboardNavigation from "@/app/(routes)/(dashboard)/_components/dashboard-navigation";

export const metadata: Metadata = {
Expand All @@ -16,14 +15,11 @@ export default async function DashboardLayout({
}: {
children: React.ReactNode;
}) {
const session = await getSession();

return (
<div className="pb-10">
<DashboardHeader session={session} />
<div>
<DashboardNavigation session={session} />
</div>
<DashboardHeader />
<DashboardNavigation />
{children}
</div>
);
Expand Down
6 changes: 1 addition & 5 deletions app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PrismaAdapter } from "@auth/prisma-adapter";
import prismadb from "@/lib/prismadb";
import bcrypt from 'bcryptjs';
import { LoginFormSchema } from "@/schemas";
import { getUserByEmail } from "@/data/user";
import { getUserByEmail } from "@/actions/get-user";

interface User extends NextAuthUser {
phoneNumber: string;
Expand Down Expand Up @@ -44,10 +44,6 @@ export const authOptions: NextAuthOptions = {

const { email, password } = verifiedCredentials.data

if (!email || !password) {
return null;
}

const user = await getUserByEmail(email);

if (!user) {
Expand Down
2 changes: 1 addition & 1 deletion app/api/auth/register-user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import bcrypt from "bcryptjs";
import prismadb from "@/lib/prismadb";
import { NextResponse } from "next/server";
import { RegisterFormSchema } from "@/schemas";
import { getUserByEmail } from "@/data/user";
import { getUserByEmail } from "@/actions/get-user";

export async function POST(req: Request) {
try {
Expand Down

0 comments on commit 4387486

Please sign in to comment.