Skip to content

Commit

Permalink
feat: added auth
Browse files Browse the repository at this point in the history
  • Loading branch information
warmachine028 committed Aug 29, 2024
1 parent ef9b94c commit 212998a
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 71 deletions.
8 changes: 8 additions & 0 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POSTGRES_URL=<Your POSTGRES_URL>
POSTGRES_PRISMA_URL=<Your POSTGRES_PRISMA_URL>
POSTGRES_URL_NO_SSL=<Your POSTGRES_URL_NO_SSL>
POSTGRES_URL_NON_POOLING=<Your POSTGRES_URL_NON_POOLING>
POSTGRES_USER=<Your POSTGRES_USER>
POSTGRES_HOST=<Your POSTGRES_HOST>
POSTGRES_PASSWORD=<Your POSTGRES_PASSWORD>
POSTGRES_DATABASE=<Your POSTGRES_DATABASE>
3 changes: 2 additions & 1 deletion client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ yarn-debug.log*
yarn-error.log*

# env files (can opt-in for commiting if needed)
.env*
.env
.env.local

# vercel
.vercel
Expand Down
127 changes: 64 additions & 63 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
{
"dependencies": {
"@hookform/resolvers": "^3.9.0",
"@lucia-auth/adapter-prisma": "^4.0.1",
"@prisma/client": "^5.18.0",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tabs": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@tanstack/react-query": "^5.52.1",
"@tanstack/react-query-devtools": "^5.52.1",
"@tiptap/extension-placeholder": "^2.6.6",
"@tiptap/pm": "^2.6.6",
"@tiptap/react": "^2.6.6",
"@tiptap/starter-kit": "^2.6.6",
"@uploadthing/react": "^6.7.2",
"arctic": "^1.9.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"ky": "^1.7.1",
"lucia": "^3.2.0",
"lucide-react": "^0.435.0",
"next": "15.0.0-rc.0",
"next-themes": "^0.3.0",
"prisma": "^5.18.0",
"react": "19.0.0-rc-f994737d14-20240522",
"react-cropper": "^2.3.3",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"react-hook-form": "^7.53.0",
"react-image-file-resizer": "^0.4.8",
"react-intersection-observer": "^9.13.0",
"react-linkify-it": "^1.0.8",
"stream-chat": "^8.39.0",
"stream-chat-react": "^11.23.7",
"tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7",
"uploadthing": "^6.13.2",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^22",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^9",
"eslint-config-next": "15.0.0-rc.0",
"eslint-config-prettier": "^9.1.0",
"postcss": "^8",
"prettier": "^3.3.3",
"prettier-plugin-packagejson": "^2.5.2",
"prettier-plugin-tailwindcss": "^0.6.6",
"tailwindcss": "^3.4.1",
"typescript": "^5"
},
"name": "next-book",
"private": true,
"scripts": {
"build": "next build",
"dev": "next dev --turbo",
"lint": "next lint",
"start": "next start"
},
"version": "0.1.0"
"dependencies": {
"@hookform/resolvers": "^3.9.0",
"@lucia-auth/adapter-prisma": "^4.0.1",
"@prisma/client": "^5.18.0",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tabs": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@tanstack/react-query": "^5.52.1",
"@tanstack/react-query-devtools": "^5.52.1",
"@tiptap/extension-placeholder": "^2.6.6",
"@tiptap/pm": "^2.6.6",
"@tiptap/react": "^2.6.6",
"@tiptap/starter-kit": "^2.6.6",
"@uploadthing/react": "^6.7.2",
"arctic": "^1.9.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"ky": "^1.7.1",
"lucia": "^3.2.0",
"lucide-react": "^0.435.0",
"next": "15.0.0-rc.0",
"next-themes": "^0.3.0",
"prisma": "^5.18.0",
"react": "19.0.0-rc-f994737d14-20240522",
"react-cropper": "^2.3.3",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"react-hook-form": "^7.53.0",
"react-image-file-resizer": "^0.4.8",
"react-intersection-observer": "^9.13.0",
"react-linkify-it": "^1.0.8",
"stream-chat": "^8.39.0",
"stream-chat-react": "^11.23.7",
"tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7",
"uploadthing": "^6.13.2",
"zod": "^3.23.8"
},
"license":"MIT",
"devDependencies": {
"@types/node": "^22",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^9",
"eslint-config-next": "15.0.0-rc.0",
"eslint-config-prettier": "^9.1.0",
"postcss": "^8",
"prettier": "^3.3.3",
"prettier-plugin-packagejson": "^2.5.2",
"prettier-plugin-tailwindcss": "^0.6.6",
"tailwindcss": "^3.4.1",
"typescript": "^5"
},
"name": "next-book",
"private": true,
"scripts": {
"build": "next build",
"dev": "next dev --turbo",
"lint": "next lint",
"start": "next start"
},
"version": "0.1.0"
}
32 changes: 26 additions & 6 deletions client/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,33 @@ generator client {
previewFeatures = ["fullTextSearch"]
}

// datasource db {
// provider = "postgresql"
// url = env("DATABASE_URL")
// }

datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL") // uses connection pooling
url = env("POSTGRES_PRISMA_URL") // env("DATABASE_URL") uses connection pooling
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
}

model User {
id String @id
userName String @unique
displayName String
email String? @unique
passwordHash String?
googleId String? @unique
avatarUrl String?
bio String?
sessions Session[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
@@map("users")
}

model Session {
id String @id
userId String
expiresAt DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@map("sessions")
}
69 changes: 69 additions & 0 deletions client/src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Lucia, Session, User } from 'lucia'
import { PrismaAdapter } from '@lucia-auth/adapter-prisma'
import prisma from '@/lib/prisma'
import { cache } from 'react'
import { cookies } from 'next/headers'

const adapter = new PrismaAdapter(prisma.session, prisma.user)

interface DatabaseUserAttributes {
id: string
userName: string
displayName: string
avatarUrl: String | null
googleId: string | null
}

declare module 'lucia' {
interface Register {
Lucia: typeof lucia
DatabaseUserAttributes: DatabaseUserAttributes
}
}

export const lucia = new Lucia(adapter, {
sessionCookie: {
expires: false,
attributes: {
secure: process.env.NODE_ENV === 'production'
}
},
getUserAttributes({ id, userName, displayName, avatarUrl, googleId }: DatabaseUserAttributes) {
return {
id,
userName,
displayName,
avatarUrl,
googleId
}
}
})

export const validateRequest = cache(
async (): Promise<{ user: User; session: Session } | { user: null; session: null }> => {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null
if (!sessionId) {
return {
user: null,
session: null
}
}
const result = await lucia.validateSession(sessionId)
try {
if (result.session?.fresh) {
const sessionCookie = lucia.createSessionCookie(result.session.id)
const { name, value, attributes } = sessionCookie
cookies().set(name, value, attributes)
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie()
const { name, value, attributes } = sessionCookie

cookies().set(name, value, attributes)
}
} catch (error) {
console.log(error)
}
return result
}
)
2 changes: 1 addition & 1 deletion client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2017",
"target": "ESNext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 212998a

Please sign in to comment.