Skip to content

Commit

Permalink
init auth (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbao-dev authored Mar 20, 2024
1 parent 5f9f304 commit 225a1e0
Show file tree
Hide file tree
Showing 78 changed files with 2,181 additions and 1,203 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DATABASE_URL=postgres://postgres:test123@localhost:5432
REDIS_KEY=AZ8JASQgYjIyYzgyM2EtMDViZC00ZjRiLTgzOGItMDI0OGEwZTdkZDc2ZDk5OTBjM2RjM2ZlNGU3MTk2YzA5ZDQyZGI5NzU2ZmI=
DATABASE_URL=postgresql://localhost:5432/seistart
NEXTAUTH_JWT_SECRET=KyxVXUR2EIF1MuesX9ygnnsfeQyu7ExK_2suK6lLqWA4evzzDjBTR1KF__frGgWGjFoUCk1p2PPpBIeG5tVydQ
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ yarn-error.log*

# typescript
*.tsbuildinfo
next-env.d.ts
next-env.d.ts
14 changes: 0 additions & 14 deletions app/(app)/(auth)/layout.tsx

This file was deleted.

56 changes: 0 additions & 56 deletions app/(app)/(auth)/sign-in/page.tsx

This file was deleted.

50 changes: 0 additions & 50 deletions app/(app)/(auth)/sign-up/page.tsx

This file was deleted.

7 changes: 6 additions & 1 deletion app/(app)/dashboard/analytics/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// TODO: Add analytics
import { getUser } from "@/auth/auth-guard"
import { Role } from "@/server-actions/entitlements/entitlements.models"
import { redirect } from "next/navigation"

export default async function AnalyticsPage() {
const { entitlements } = await getUser()
if (!entitlements || entitlements.role !== Role.Admin) redirect("/dashboard")
return <h1 className="my-4 text-2xl font-semibold">Analytics</h1>
}
12 changes: 10 additions & 2 deletions app/(app)/dashboard/create-project/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { getUser } from "@/auth/auth-guard"
import { CreateProject } from "@/components/pages/create-project"
import { Permission } from "@/server-actions/entitlements/entitlements.models"
import { getAllTagsAction } from "@/server-actions/tags/tags.actions"
import { redirect } from "next/navigation"

export default async function CreateProjectPage() {
const { tags } = await getAllTagsAction()
return <CreateProject tags={tags}></CreateProject>
const { entitlements } = await getUser()
if (entitlements && entitlements.permissions[Permission.ProjectSelfWrite]) {
const { tags } = await getAllTagsAction()
return <CreateProject tags={tags}></CreateProject>
} else {
redirect("/dashboard")
}
}
31 changes: 18 additions & 13 deletions app/(app)/dashboard/edit-projects/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { getUser } from "@/auth/auth-guard"
import { EditProject } from "@/components/pages/edit-project"
import { Permission } from "@/server-actions/entitlements/entitlements.models"
import { getProjectBySlugAction } from "@/server-actions/projects/projects.actions"
import { getAllTagsAction } from "@/server-actions/tags/tags.actions"
import { Metadata } from "next"
import { redirect } from "next/navigation"

export async function generateMetadata({
params,
}: {
params: { slug: string }
}) {
const { project } = await getProjectBySlugAction(params.slug)
return {
title: project.name,
description: project.description,
keywords: project.tags,
}
export const metadata: Metadata = {
title: "Edit Project",
}

export default async function EditProjectPage({
params,
}: {
params: { slug: string }
}) {
const { entitlements, userId } = await getUser()
const { project } = await getProjectBySlugAction(params.slug)
const { tags } = await getAllTagsAction()
return <EditProject project={project} tags={tags}></EditProject>
if (
entitlements &&
((entitlements.permissions[Permission.ProjectSelfEdit] &&
project.userId === userId) ||
entitlements.permissions[Permission.ProjectAllEdit])
) {
const { tags } = await getAllTagsAction()
return <EditProject project={project} tags={tags}></EditProject>
} else {
redirect("/dashboard")
}
}
38 changes: 30 additions & 8 deletions app/(app)/dashboard/edit-projects/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { getUser } from "@/auth/auth-guard"
import { EditProjects } from "@/components/pages/edit-projects"
import { getAllProjectsAction } from "@/server-actions/projects/projects.actions"
import { Permission } from "@/server-actions/entitlements/entitlements.models"
import {
getAllProjectsAction,
getAllProjectsByUserAction,
} from "@/server-actions/projects/projects.actions"
import { redirect } from "next/navigation"

export default async function EditProjectsPage() {
const { projects } = await getAllProjectsAction()
return (
<>
<h1 className="my-4 text-2xl font-semibold">Edit Projects</h1>
<EditProjects projects={projects}></EditProjects>
</>
)
const { entitlements, userId } = await getUser()
if (entitlements && entitlements.permissions[Permission.ProjectAllEdit]) {
const { projects } = await getAllProjectsAction()
return (
<>
<h1 className="my-4 text-2xl font-semibold">Edit Projects</h1>
<EditProjects projects={projects}></EditProjects>
</>
)
} else if (
entitlements &&
entitlements.permissions[Permission.ProjectSelfEdit]
) {
const { projects } = await getAllProjectsByUserAction(userId)
return (
<>
<h1 className="my-4 text-2xl font-semibold">Edit Projects</h1>
<EditProjects projects={projects}></EditProjects>
</>
)
} else {
redirect("/dashboard")
}
}
6 changes: 3 additions & 3 deletions app/(app)/dashboard/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getUserAuth } from "@/auth/auth-guard"
import { getUser } from "@/auth/auth-guard"
import { DashboardSidebar } from "@/components/dashboard/dashboard-sidebar"
import { redirect } from "next/navigation"

Expand All @@ -9,8 +9,8 @@ export default async function DashboardLayout({
}: {
children: React.ReactNode
}) {
const { session } = await getUserAuth()
if (!session) redirect("/sign-in")
const { userId } = await getUser()
if (!userId) redirect("/")
return (
<>
<div className="flex h-screen">
Expand Down
5 changes: 0 additions & 5 deletions app/(app)/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { getUserAuth } from "@/auth/auth-guard"
import SignOutBtn from "@/components/auth/sign-out-button"

export default async function DashboardPage() {
const { session } = await getUserAuth()
return (
<>
<div className="flex w-full flex-row items-center justify-between">
<h1 className="my-2 text-2xl font-bold">Home</h1> <SignOutBtn />
</div>
<pre className="my-2 rounded-lg bg-secondary p-4">
{session?.user.email}
</pre>
</>
)
}
5 changes: 5 additions & 0 deletions app/(app)/dashboard/tags/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { getUser } from "@/auth/auth-guard"
import { DashboardTags } from "@/components/pages/dashboard-tags"
import { Role } from "@/server-actions/entitlements/entitlements.models"
import { getAllTagsAction } from "@/server-actions/tags/tags.actions"
import { redirect } from "next/navigation"

export default async function ProjectsPage() {
const { entitlements } = await getUser()
if (!entitlements || entitlements.role !== Role.Admin) redirect("/dashboard")
const { tags } = await getAllTagsAction()
return <DashboardTags tags={tags}></DashboardTags>
}
7 changes: 7 additions & 0 deletions app/(app)/dashboard/wallets/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Wallets } from "@/components/pages/wallets"
import { getAllWalletsAction } from "@/server-actions/wallets/wallets.actions"

export default async function ProjectsPage() {
const { wallets } = await getAllWalletsAction()
return <Wallets wallets={wallets}></Wallets>
}
2 changes: 1 addition & 1 deletion app/(app)/ecosystem/RelationsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const RelationsContainer = () => {
const nodes = generateMockData2

return [nodes] as const
}, [])
}, [generateMockData2])

return (
<Suspense fallback={<ChartSkeleton />}>
Expand Down
13 changes: 13 additions & 0 deletions app/(app)/ecosystem/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getUser } from "@/auth/auth-guard"
import { Role } from "@/server-actions/entitlements/entitlements.models"
import { redirect } from "next/navigation"

export default async function EcosystemLayout({
children,
}: {
children: React.ReactNode
}) {
const { entitlements } = await getUser()
if (!entitlements || entitlements.role !== Role.Admin) redirect("/")
return <>{children}</>
}
17 changes: 7 additions & 10 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
import { Footer } from "@/components/footer"
import { Header } from "@/components/header"
import { ThemeProvider } from "@/components/theme/theme-provider"
import { appMetadata } from "@/config/meteada.config"
import AppProvider from "@/providers/app-provider"
import { getCompleteUserProfileAction } from "@/server-actions/user-profile/user-profile.actions"
import { cn } from "@/utils/tailwind.utils"
import type { Metadata } from "next"
import { Space_Grotesk } from "next/font/google"
import "./globals.css"

const spaceGrotesk = Space_Grotesk({ subsets: ["latin"] })

export const metadata: Metadata = {
...appMetadata.home,
}

export default function RootLayout({
const PRE_LAUNCH = true
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
const { userProfile } = await getCompleteUserProfileAction()
return (
<html lang="en" suppressHydrationWarning>
<body className={cn(spaceGrotesk.className)}>
<ThemeProvider
attribute="class"
enableSystem={true}
disableTransitionOnChange
>
<AppProvider initialUserProfile={userProfile}>
<div className="min-h-100vh container mx-auto flex flex-col supports-[height:100cqh]:min-h-[100cqh] supports-[height:100dvh]:min-h-[100dvh] supports-[height:100svh]:min-h-[100svh]">
<Header />
<main className="flex-1">{children}</main>
<Footer />
</div>
</ThemeProvider>
</AppProvider>
</body>
</html>
)
Expand Down
14 changes: 3 additions & 11 deletions auth/auth-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ export const authGuard = async () => {
if (!session) redirect("/sign-in")
}

export const getUserAuth = async () => {
export const getUser = async () => {
const { session, user } = await validateRequest()
if (!session) return { session: null }
return {
session: {
user: {
id: user.id,
email: user.email,
name: user.name,
},
},
}
if (!session) return { userId: undefined }
return { userId: user.id, entitlements: session.entitlements }
}

export const validateRequest = cache(
Expand Down
Loading

0 comments on commit 225a1e0

Please sign in to comment.