Skip to content

Commit

Permalink
differentiate getSession / getUser
Browse files Browse the repository at this point in the history
DexterStorey committed Oct 18, 2024
1 parent d7d9696 commit cd8f271
Showing 5 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- [2024-10-18] [differentiate getSession / getUser](https://github.com/RubricLab/auth/commit/7362e36e1d37dd0cd348c7cef5cbe10e790e0c00)
- [2024-10-18] [add more scope such as provider and session cookies to package](https://github.com/RubricLab/auth/commit/d0a9ebfb9d7e0358dd65f394e879bef21ade2722)
- [2024-10-03] [release](https://github.com/RubricLab/auth/commit/5b29a0fc2dc85cfb5ad32be4b05c419640924c3d)
- [2024-10-03] [mod lint script](https://github.com/RubricLab/auth/commit/f06a0912e6b4e04ff260f44e263e6f396b44d9fb)
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export {
type AuthProvider,
type AuthProviderConfig,
zodSession
zodSession,
zodUser
} from './lib/types'

export { ClientAuthProvider, useSession } from './lib/provider'
10 changes: 6 additions & 4 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { PrismaClient } from '@prisma/client'
import { z } from 'zod'

export const zodUser = z.object({
id: z.string(),
authProviders: z.array(z.object({ provider: z.string(), accountId: z.string() }))
})

export const zodSession = z.object({
sessionKey: z.string(),
user: z.object({
id: z.string(),
authProviders: z.array(z.object({ provider: z.string(), accountId: z.string() }))
})
user: zodUser
})

export type Session = z.infer<typeof zodSession>
45 changes: 27 additions & 18 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Prisma } from '@prisma/client'

import { type AuthProviders, zodSession } from './types'
import { type AuthProviders, zodSession, zodUser } from './types'

import { cookies } from 'next/headers'

@@ -59,23 +59,8 @@ export function createAuthActions({
})
},

async getSession(userId?: string) {
const user = userId
? await db.user.findUnique({
where: {
id: userId
},
select: {
id: true,
authProviders: {
select: {
provider: true,
accountId: true
}
}
}
})
: JSON.parse(cookies().get('user')?.value || '{}')
async getSession() {
const user = JSON.parse(cookies().get('user')?.value || '{}')

const sessionKey = cookies().get('key')?.value

@@ -92,6 +77,30 @@ export function createAuthActions({
return null
}

return data
},
async getUser(userId: string) {
const user = await db.user.findUnique({
where: {
id: userId
},
select: {
id: true,
authProviders: {
select: {
provider: true,
accountId: true
}
}
}
})

const { data, success } = zodUser.safeParse(user)

if (!success) {
return null
}

return data
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
"lint:fix": "bun x biome check --fix --unsafe . && bun x biome lint --write --unsafe ."
},
"name": "@rubriclab/auth",
"version": "0.0.6",
"version": "0.0.7",
"main": "index.ts",
"dependencies": {
"@rubriclab/package": "*",

0 comments on commit cd8f271

Please sign in to comment.