2
0
Files
bot/apps/builder/services/api/utils.ts

31 lines
919 B
TypeScript
Raw Normal View History

2022-07-01 10:26:04 +02:00
import { setUser } from '@sentry/nextjs'
2022-02-24 11:13:19 +01:00
import { User } from 'db'
import { NextApiRequest } from 'next'
import { getSession } from 'next-auth/react'
2022-08-08 08:21:36 +02:00
import { env } from 'utils'
const mockedUser: User = {
id: 'proUser',
name: 'Pro user',
email: 'pro-user@email.com',
company: null,
createdAt: new Date(),
emailVerified: null,
graphNavigation: 'TRACKPAD',
image: 'https://avatars.githubusercontent.com/u/16015833?v=4',
lastActivityAt: new Date(),
onboardingCategories: [],
updatedAt: new Date(),
}
2022-02-24 11:13:19 +01:00
export const getAuthenticatedUser = async (
req: NextApiRequest
): Promise<User | undefined> => {
const session = await getSession({ req })
2022-08-08 08:21:36 +02:00
if (env('E2E_TEST') === 'true' && !session?.user) return mockedUser
if (!session?.user || !('id' in session.user)) return
2022-07-01 10:26:04 +02:00
const user = session.user as User
setUser({ id: user.id, email: user.email ?? undefined })
return session?.user as User
2022-02-24 11:13:19 +01:00
}