Skip to content

Commit

Permalink
feat(api): add GET /auth/me (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Jun 6, 2024
1 parent 09edb4b commit 292dd52
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 32 deletions.
1 change: 0 additions & 1 deletion apps/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hono } from 'hono'

import { logger } from 'hono/logger'
import { prettyJSON } from 'hono/pretty-json'
import { trimTrailingSlash } from 'hono/trailing-slash'
Expand Down
26 changes: 14 additions & 12 deletions apps/api/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { PrismaClient } from '@prisma/client'

// eslint-disable-next-line import/no-mutable-exports
// biome-ignore lint/style/useConst: <explanation>
let prisma: PrismaClient

if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
const globalWithPrisma = global as typeof globalThis & {
prisma: PrismaClient
}
if (!globalWithPrisma.prisma) {
globalWithPrisma.prisma = new PrismaClient()
}
prisma = globalWithPrisma.prisma
}
prisma = new PrismaClient()

// if (process.env.NODE_ENV === 'production') {
// prisma = new PrismaClient()
// } else {
// const globalWithPrisma = global as typeof globalThis & {
// prisma: PrismaClient
// }
// if (!globalWithPrisma.prisma) {
// globalWithPrisma.prisma = new PrismaClient()
// }
// prisma = globalWithPrisma.prisma
// }

export default prisma
83 changes: 83 additions & 0 deletions apps/api/prisma/generated/zod/index.ts

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
previewFeatures = ["relationJoins"]
}

generator zod {
Expand Down
4 changes: 0 additions & 4 deletions apps/api/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ hono.get('/', (c) => {
return c.json({ message: 'Hello Hono!' })
})

hono.get('version', (c) => {
return c.json({ version: '1.0.0' })
})

hono.route('/auth', authApp)
3 changes: 1 addition & 2 deletions apps/api/v1/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { clerkMiddleware, getAuth } from '@hono/clerk-auth'
import { clerkMiddleware } from '@hono/clerk-auth'

export const authMiddleware = clerkMiddleware
export { getAuth }
31 changes: 19 additions & 12 deletions apps/api/v1/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { zValidator } from '@hono/zod-validator'
import { getAuth } from '@hono/clerk-auth'
import { Hono } from 'hono'
import { zLogin } from '../validation'
import { HTTPException } from 'hono/http-exception'
import { findUserById } from '../services/user.service'

const authApp = new Hono()
const router = new Hono()

authApp.get('/login', zValidator('query', zLogin), (c) => {
const { email, password } = c.req.valid('query')
return c.json({
message: 'Login',
email,
password,
env: process.env.NODE_ENV,
})
router.get('/me', async (c) => {
const auth = getAuth(c)

if (!auth?.userId) {
throw new HTTPException(401, { message: 'unauthorized' })
}

const user = await findUserById(auth.userId)

if (!user) {
throw new HTTPException(401, { message: 'unauthorized' })
}

return c.json(user)
})

export default authApp
export default router
9 changes: 9 additions & 0 deletions apps/api/v1/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import prisma from '@/lib/prisma'

export async function findUserById(id: string) {
return await prisma.user.findUnique({
where: {
id,
},
})
}

0 comments on commit 292dd52

Please sign in to comment.