Skip to content

Commit

Permalink
Add login page and redirect when not logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
RobVermeer committed Dec 29, 2023
1 parent cd60ebf commit 4bcb62c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
9 changes: 2 additions & 7 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type { Metadata } from "next"
import { Ubuntu } from "next/font/google"
import "./globals.css"
import { getServerSession } from "next-auth"
import { authOptions } from "@/lib/nextAuth"
import { Header } from "@/components/Header"
import { Login } from "@/components/Login"
import { Toaster } from "@/components/ui/toaster"

const ubuntu = Ubuntu({ weight: ["400"], subsets: ["latin"] })
Expand All @@ -20,13 +17,11 @@ interface Props {
}

export default async function RootLayout({ children }: Props) {
const session = await getServerSession(authOptions)

return (
<html lang="en">
<body className={ubuntu.className}>
<Header session={session} />
{session ? children : <Login />}
<Header />
{children}
<Toaster />
</body>
</html>
Expand Down
14 changes: 14 additions & 0 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { redirect } from "next/navigation"
import { getServerSession } from "next-auth"
import { authOptions } from "@/lib/nextAuth"
import { LoginForm } from "@/components/LoginForm"

export default async function Login() {
const session = await getServerSession(authOptions)

if (session) {
redirect("/")
}

return <LoginForm />
}
7 changes: 7 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { EmptyState } from "@/components/EmptyState"
import { Issues } from "@/components/Issues"
import { ProjectSelect } from "@/components/ProjectSelect"
import { authOptions } from "@/lib/nextAuth"
import { getProjectsForUser } from "@/lib/project"
import { ServerCog } from "lucide-react"
import { getServerSession } from "next-auth"
import Link from "next/link"
import { redirect } from "next/navigation"

export default async function Home() {
const session = await getServerSession(authOptions)

if (!session) redirect("/login")

const projects = await getProjectsForUser()

return (
Expand Down
12 changes: 8 additions & 4 deletions src/app/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { EditProject } from "@/components/EditProject"
import { EmptyState } from "@/components/EmptyState"
import { Issues } from "@/components/Issues"
import { NewProject } from "@/components/NewProject"
import { ProjectSelect } from "@/components/ProjectSelect"
import { authOptions } from "@/lib/nextAuth"
import { getProjectsForUser } from "@/lib/project"
import { ServerCog } from "lucide-react"
import Link from "next/link"
import { getServerSession } from "next-auth"
import { redirect } from "next/navigation"

export default async function Profile() {
const session = await getServerSession(authOptions)

if (!session) redirect("/login")

export default async function Home() {
const projects = await getProjectsForUser()

return (
Expand Down
7 changes: 7 additions & 0 deletions src/app/project/[projectId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { EmptyState } from "@/components/EmptyState"
import { Issues } from "@/components/Issues"
import { ProjectSelect } from "@/components/ProjectSelect"
import { authOptions } from "@/lib/nextAuth"
import { getProjectsForUser } from "@/lib/project"
import { ServerCog } from "lucide-react"
import { getServerSession } from "next-auth"
import Link from "next/link"
import { redirect } from "next/navigation"

interface Props {
params: { projectId: string }
}

export default async function Project({ params }: Props) {
const session = await getServerSession(authOptions)

if (!session) redirect("/login")

const projects = await getProjectsForUser()

return (
Expand Down
9 changes: 4 additions & 5 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Session } from "next-auth"
import { getServerSession } from "next-auth"
import { Logo } from "../Logo"
import { Menu } from "./Menu"
import { authOptions } from "@/lib/nextAuth"

interface Props {
session: Session | null
}
export async function Header() {
const session = await getServerSession(authOptions)

export async function Header({ session }: Props) {
return (
<header
className="p-4 flex h-64 bg-cover bg-center items-start justify-between"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { signIn } from "next-auth/react"
import { Button } from "@/components/ui/button"
import { Chrome, Github } from "lucide-react"

export function Login() {
export function LoginForm() {
return (
<main className="p-4 grid gap-3 fixed left-0 bottom-0 right-0 md:bottom-auto md:top-1/2 md:max-w-lg md:w-full md:mx-auto">
<main className="grid gap-3 p-4 mb-8 mx-auto md:max-w-[592px]">
<Button variant="secondary" onClick={() => signIn("github")}>
<Github size="18" className="mr-2" /> Login with Github
</Button>
Expand Down

0 comments on commit 4bcb62c

Please sign in to comment.