Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility for old azure database #176

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { PrismaClient } from '@prisma/client';
export interface Context {
prisma: PrismaClient;
userId: string;
email: string;
}
1 change: 1 addition & 0 deletions src/lib/auth/verifyToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
1 change: 1 addition & 0 deletions src/lib/tests/testContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type TestContext = {
client: GraphQLClient;
prisma: PrismaClient;
userId: string;
email: string;
};

export const createTestContext = (): TestContext => {
Expand Down
29 changes: 23 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,38 @@ 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({
context: async ({ req }): Promise<Context> => {
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<Auth0Profile>(`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,
Expand Down
26 changes: 11 additions & 15 deletions src/utils/save_user_locally.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Auth0Profile>(`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;
}
};
Loading