Skip to content

Commit

Permalink
Merge pull request #177 from Organisasjonskollegiet/bugfix/DeleteAndU…
Browse files Browse the repository at this point in the history
…pdateuserBug

Fix bug with deleting and updating users
  • Loading branch information
Arashfa0301 authored Oct 24, 2024
2 parents 96422ea + c817d8c commit 99e171b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
24 changes: 21 additions & 3 deletions src/schema/auth/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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({
Expand Down Expand Up @@ -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.';
});
},
Expand Down
20 changes: 12 additions & 8 deletions src/schema/auth/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
});
3 changes: 1 addition & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ 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<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 };
Expand Down

0 comments on commit 99e171b

Please sign in to comment.