From cd8f2711a14f6250fcf253a2193ee58b80b4fd96 Mon Sep 17 00:00:00 2001 From: DexterStorey Date: Fri, 18 Oct 2024 17:26:32 -0400 Subject: [PATCH] differentiate getSession / getUser --- CHANGELOG.md | 1 + index.ts | 3 ++- lib/types.ts | 10 ++++++---- lib/utils.ts | 45 +++++++++++++++++++++++++++------------------ package.json | 2 +- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80320dd..f99baec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/index.ts b/index.ts index 3ba5abc..2eeb414 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,8 @@ export { type AuthProvider, type AuthProviderConfig, - zodSession + zodSession, + zodUser } from './lib/types' export { ClientAuthProvider, useSession } from './lib/provider' diff --git a/lib/types.ts b/lib/types.ts index 9e2cd1a..e6313ec 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -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 diff --git a/lib/utils.ts b/lib/utils.ts index 8207e56..82afa6c 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -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 } } diff --git a/package.json b/package.json index 3e46729..6e7cf6e 100644 --- a/package.json +++ b/package.json @@ -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": "*",