Skip to content

Commit

Permalink
Merge pull request #49 from codelitdev/issue-48
Browse files Browse the repository at this point in the history
Added dashboard route and keep protected when session is not active
  • Loading branch information
rajat1saxena authored Nov 8, 2023
2 parents 5a5e907 + f0d14a2 commit 41622eb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/web/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const authOptions: NextAuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async redirect({ url, baseUrl }) {
return url;
return baseUrl;
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/components/NavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function AuthButton() {
}
return (
<div className={styles.navbar}>
Guest <br />
Guest
<button onClick={() => signIn()}>Sign in</button>
</div>
);
Expand Down
6 changes: 6 additions & 0 deletions apps/web/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
"use client";

import { useSession } from "next-auth/react";
import Link from "next/link";

export default function Dashboard() {
const { data: session } = useSession();

return (
<>
<div>
<Link href="/">Home</Link>
<p>
{session?.user && <>Welcome {session?.user?.email}</>}
</p>
</div>
</>

);
}
14 changes: 11 additions & 3 deletions apps/web/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import Image from 'next/image'
"use client"

import styles from './page.module.css'
import Link from 'next/link'
import { useSession } from 'next-auth/react'

export default function Home() {
const {data: session} = useSession();

const Dashboard = session ? <Link href="/dashboard"> Dashboard </Link>: ""

return (
<>
<div>
<Link href="/dashboard">Dashboard</Link>
Hello
{Dashboard}
</div>
</>

)
}
3 changes: 3 additions & 0 deletions apps/web/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default } from "next-auth/middleware";

export const config = { matcher: ["/dashboard"] };

0 comments on commit 41622eb

Please sign in to comment.