From b49b6dee08295554e2f7c61b24a3a7360d11cca2 Mon Sep 17 00:00:00 2001 From: Arash Farzaneh Taleghani Date: Wed, 23 Oct 2024 22:29:55 +0200 Subject: [PATCH] Add compatibility for old azure database --- src/context.ts | 1 + src/lib/auth/verifyToken.ts | 1 + src/lib/tests/testContext.ts | 1 + src/server.ts | 29 +++++++++++++++++++++++------ src/utils/save_user_locally.ts | 26 +++++++++++--------------- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/context.ts b/src/context.ts index 0bc1740..4a17a34 100644 --- a/src/context.ts +++ b/src/context.ts @@ -3,4 +3,5 @@ import { PrismaClient } from '@prisma/client'; export interface Context { prisma: PrismaClient; userId: string; + email: string; } diff --git a/src/lib/auth/verifyToken.ts b/src/lib/auth/verifyToken.ts index b7a8487..c2a8321 100644 --- a/src/lib/auth/verifyToken.ts +++ b/src/lib/auth/verifyToken.ts @@ -2,6 +2,7 @@ import jwt from 'express-jwt'; import jwksRsa from 'jwks-rsa'; export interface DecodedToken { + email: string; iss: string; sub: string; aud: string[]; diff --git a/src/lib/tests/testContext.ts b/src/lib/tests/testContext.ts index 1a4f8ef..3c7cf5f 100644 --- a/src/lib/tests/testContext.ts +++ b/src/lib/tests/testContext.ts @@ -16,6 +16,7 @@ export type TestContext = { client: GraphQLClient; prisma: PrismaClient; userId: string; + email: string; }; export const createTestContext = (): TestContext => { diff --git a/src/server.ts b/src/server.ts index 4323eaa..73a082c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,8 +8,17 @@ import { protectedSchema } from './schema'; import 'dotenv/config'; import { Context } from './context'; import { checkJwt, DecodedToken } from './lib/auth/verifyToken'; -// import { saveAuth0UserIfNotExist } from './utils/save_user_locally'; +import { saveAuth0UserIfNotExist } from './utils/save_user_locally'; import cors from 'cors'; +import axios from 'axios'; +interface Auth0Profile { + sub: string; + nickname: string; + name: string; + picture: string; + updated_at: Date; + email: string; +} export const createApollo = (prisma: PrismaClient) => { const server = new ApolloServer({ @@ -17,12 +26,20 @@ export const createApollo = (prisma: PrismaClient) => { if (req.user) { const decodedToken = req.user as DecodedToken; const userId = decodedToken.sub.split('|')[1]; - // if (process.env.NODE_ENV != 'production') { - // await saveAuth0UserIfNotExist(prisma, userId, req.headers['authorization']); - // } - return { userId: userId, prisma }; + console.log('\n\n\nasdflasdfpasdf'); + const request = await axios.get(`https://${process.env.AUTH0_DOMAIN}/userinfo`, { + headers: { + Authorization: req.headers['authorization'] as string, + 'Content-Type': 'application/json', + }, + }); + const email = request.data.email; + + const user = await saveAuth0UserIfNotExist(prisma, email, userId); + + return { userId: user.id, email: user.email, prisma }; } - return { userId: '', prisma }; + return { userId: '', email: '', prisma }; }, schema: protectedSchema, mocks: process.env.MOCKING == 'true' && simpleMock, diff --git a/src/utils/save_user_locally.ts b/src/utils/save_user_locally.ts index 3886a90..6e87555 100644 --- a/src/utils/save_user_locally.ts +++ b/src/utils/save_user_locally.ts @@ -12,18 +12,14 @@ interface Auth0Profile { // Saves the auth0 user profile to the database if it does not exist locally // This is mainly to make local development easier -// export const saveAuth0UserIfNotExist = async (prisma: PrismaClient, userId: string, authHeader?: string) => { -// const userCount = await prisma.user.count({ where: { id: userId } }); -// if (userCount == 0 && authHeader) { -// const request = await axios.get(`https://${process.env.AUTH0_DOMAIN}/userinfo`, { -// headers: { -// Authorization: authHeader, -// 'Content-Type': 'application/json', -// }, -// }); -// const profile = request.data; -// await prisma.user.create({ -// data: { email: profile.email, password: '', id: userId, emailVerified: false }, -// }); -// } -// }; +export const saveAuth0UserIfNotExist = async (prisma: PrismaClient, email: string, id: string) => { + const userCount = await prisma.user.findUnique({ where: { email: email } }); + if (!userCount) { + const user = await prisma.user.create({ + data: { email: email, password: '', id: id, emailVerified: false }, + }); + return user; + } else { + return userCount; + } +};