diff --git a/src/schema/auth/mutation.ts b/src/schema/auth/mutation.ts index f481913..f3d9823 100644 --- a/src/schema/auth/mutation.ts +++ b/src/schema/auth/mutation.ts @@ -2,6 +2,7 @@ import { mutationField } from 'nexus'; import { ManagementClient } from 'auth0'; import { VotationStatus } from '@prisma/client'; import { pubsub } from '../../lib/pubsub'; +import { print } from 'graphql'; export const DeleteUserMutation = mutationField('deleteMe', { type: 'String', @@ -12,11 +13,26 @@ export const DeleteUserMutation = mutationField('deleteMe', { domain: process.env.AUTH0_DOMAIN!, clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, - scope: 'delete:users', + scope: 'read:users delete:users', }); + const auth0Users = await auth0.getUsers({ + q: `email:"${ctx.email}" AND identities.connection:"Username-Password-Authentication"`, + search_engine: 'v3', + }); + + if (!auth0Users || auth0Users.length === 0) { + console.log('Auth0 user not found'); + } + + const auth0User = auth0Users[0]; + + if (!auth0User || !auth0User.user_id) { + throw new Error('User not found or user_id is missing'); + } + return auth0 - .deleteUser({ id: `auth0|${ctx.userId}` }) + .deleteUser({ id: auth0User.user_id }) .then(async () => { // invalidate all open votations where this user is participant const votationsToInvalidate = await ctx.prisma.votation.findMany({ @@ -55,7 +71,9 @@ export const DeleteUserMutation = mutationField('deleteMe', { await Promise.all(publishPromises); return 'Bruker slettet.'; }) - .catch(() => { + .catch((e) => { + console.log('Kunne ikke slette bruker.'); + console.log(e); return 'Kunne ikke slette bruker.'; }); }, diff --git a/src/schema/auth/query.ts b/src/schema/auth/query.ts index d49af6f..ec2989b 100644 --- a/src/schema/auth/query.ts +++ b/src/schema/auth/query.ts @@ -24,16 +24,20 @@ export const UpdatePasswordLinkQuery = queryField('updateMyPassword', { domain: process.env.AUTH0_DOMAIN!, clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, - // scope: 'delete:users' }); - const res = auth0.createPasswordChangeTicket({ - user_id: `auth0|${ctx.userId}`, - ttl_sec: 1200, - includeEmailInRedirect: false, - result_url: process.env.AUTH0_CALLBACK_URL, - }); + try { + const res = await auth0.createPasswordChangeTicket({ + user_id: `auth0|${ctx.userId}`, + ttl_sec: 1200, + includeEmailInRedirect: false, + result_url: process.env.AUTH0_CALLBACK_URL, + }); - return (await res).ticket; + return res.ticket; + } catch (error) { + console.error(error); + return null; // Return null instead of undefined + } }, }); diff --git a/src/server.ts b/src/server.ts index 73a082c..2ae6c0a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -26,7 +26,7 @@ export const createApollo = (prisma: PrismaClient) => { if (req.user) { const decodedToken = req.user as DecodedToken; const userId = decodedToken.sub.split('|')[1]; - console.log('\n\n\nasdflasdfpasdf'); + const request = await axios.get(`https://${process.env.AUTH0_DOMAIN}/userinfo`, { headers: { Authorization: req.headers['authorization'] as string, @@ -34,7 +34,6 @@ export const createApollo = (prisma: PrismaClient) => { }, }); const email = request.data.email; - const user = await saveAuth0UserIfNotExist(prisma, email, userId); return { userId: user.id, email: user.email, prisma };