Skip to content

Commit d7bc0e4

Browse files
Moumoulsdavimacedo
authored andcommitted
Reset and Send verification email (parse-community#6301)
1 parent 759a933 commit d7bc0e4

File tree

2 files changed

+196
-11
lines changed

2 files changed

+196
-11
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ describe('ParseGraphQLServer', () => {
285285
user1 = new Parse.User();
286286
user1.setUsername('user1');
287287
user1.setPassword('user1');
288+
user1.setEmail('[email protected]');
288289
await user1.signUp();
289290

290291
user2 = new Parse.User();
@@ -7149,6 +7150,89 @@ describe('ParseGraphQLServer', () => {
71497150
});
71507151
}
71517152
});
7153+
7154+
it('should send reset password', async () => {
7155+
const clientMutationId = uuidv4();
7156+
const emailAdapter = {
7157+
sendVerificationEmail: () => {},
7158+
sendPasswordResetEmail: () => Promise.resolve(),
7159+
sendMail: () => {},
7160+
};
7161+
parseServer = await global.reconfigureServer({
7162+
appName: 'test',
7163+
emailAdapter: emailAdapter,
7164+
publicServerURL: 'http://test.test',
7165+
});
7166+
const user = new Parse.User();
7167+
user.setUsername('user1');
7168+
user.setPassword('user1');
7169+
user.setEmail('[email protected]');
7170+
await user.signUp();
7171+
await Parse.User.logOut();
7172+
const result = await apolloClient.mutate({
7173+
mutation: gql`
7174+
mutation ResetPassword($input: ResetPasswordInput!) {
7175+
resetPassword(input: $input) {
7176+
clientMutationId
7177+
ok
7178+
}
7179+
}
7180+
`,
7181+
variables: {
7182+
input: {
7183+
clientMutationId,
7184+
7185+
},
7186+
},
7187+
});
7188+
7189+
expect(result.data.resetPassword.clientMutationId).toEqual(
7190+
clientMutationId
7191+
);
7192+
expect(result.data.resetPassword.ok).toBeTruthy();
7193+
});
7194+
it('should send verification email again', async () => {
7195+
const clientMutationId = uuidv4();
7196+
const emailAdapter = {
7197+
sendVerificationEmail: () => {},
7198+
sendPasswordResetEmail: () => Promise.resolve(),
7199+
sendMail: () => {},
7200+
};
7201+
parseServer = await global.reconfigureServer({
7202+
appName: 'test',
7203+
emailAdapter: emailAdapter,
7204+
publicServerURL: 'http://test.test',
7205+
});
7206+
const user = new Parse.User();
7207+
user.setUsername('user1');
7208+
user.setPassword('user1');
7209+
user.setEmail('[email protected]');
7210+
await user.signUp();
7211+
await Parse.User.logOut();
7212+
const result = await apolloClient.mutate({
7213+
mutation: gql`
7214+
mutation SendVerificationEmail(
7215+
$input: SendVerificationEmailInput!
7216+
) {
7217+
sendVerificationEmail(input: $input) {
7218+
clientMutationId
7219+
ok
7220+
}
7221+
}
7222+
`,
7223+
variables: {
7224+
input: {
7225+
clientMutationId,
7226+
7227+
},
7228+
},
7229+
});
7230+
7231+
expect(result.data.sendVerificationEmail.clientMutationId).toEqual(
7232+
clientMutationId
7233+
);
7234+
expect(result.data.sendVerificationEmail.ok).toBeTruthy();
7235+
});
71527236
});
71537237

71547238
describe('Session Token', () => {

src/GraphQL/loaders/usersMutations.js

Lines changed: 112 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLNonNull, GraphQLString } from 'graphql';
1+
import { GraphQLNonNull, GraphQLString, GraphQLBoolean } from 'graphql';
22
import { mutationWithClientMutationId } from 'graphql-relay';
33
import UsersRouter from '../../Routers/UsersRouter';
44
import * as objectsMutations from '../helpers/objectsMutations';
@@ -93,16 +93,18 @@ const load = parseGraphQLSchema => {
9393
const { username, password } = args;
9494
const { config, auth, info } = context;
9595

96-
const { sessionToken } = (await usersRouter.handleLogIn({
97-
body: {
98-
username,
99-
password,
100-
},
101-
query: {},
102-
config,
103-
auth,
104-
info,
105-
})).response;
96+
const { sessionToken } = (
97+
await usersRouter.handleLogIn({
98+
body: {
99+
username,
100+
password,
101+
},
102+
query: {},
103+
config,
104+
auth,
105+
info,
106+
})
107+
).response;
106108

107109
info.sessionToken = sessionToken;
108110

@@ -171,6 +173,105 @@ const load = parseGraphQLSchema => {
171173
);
172174
parseGraphQLSchema.addGraphQLType(logOutMutation.type, true, true);
173175
parseGraphQLSchema.addGraphQLMutation('logOut', logOutMutation, true, true);
176+
177+
const resetPasswordMutation = mutationWithClientMutationId({
178+
name: 'ResetPassword',
179+
description:
180+
'The resetPassword mutation can be used to reset the password of an existing user.',
181+
inputFields: {
182+
email: {
183+
descriptions: 'Email of the user that should receive the reset email',
184+
type: new GraphQLNonNull(GraphQLString),
185+
},
186+
},
187+
outputFields: {
188+
ok: {
189+
description: "It's always true.",
190+
type: new GraphQLNonNull(GraphQLBoolean),
191+
},
192+
},
193+
mutateAndGetPayload: async ({ email }, context) => {
194+
const { config, auth, info } = context;
195+
196+
await usersRouter.handleResetRequest({
197+
body: {
198+
email,
199+
},
200+
config,
201+
auth,
202+
info,
203+
});
204+
205+
return { ok: true };
206+
},
207+
});
208+
209+
parseGraphQLSchema.addGraphQLType(
210+
resetPasswordMutation.args.input.type.ofType,
211+
true,
212+
true
213+
);
214+
parseGraphQLSchema.addGraphQLType(resetPasswordMutation.type, true, true);
215+
parseGraphQLSchema.addGraphQLMutation(
216+
'resetPassword',
217+
resetPasswordMutation,
218+
true,
219+
true
220+
);
221+
222+
const sendVerificationEmailMutation = mutationWithClientMutationId({
223+
name: 'SendVerificationEmail',
224+
description:
225+
'The sendVerificationEmail mutation can be used to send the verification email again.',
226+
inputFields: {
227+
email: {
228+
descriptions:
229+
'Email of the user that should receive the verification email',
230+
type: new GraphQLNonNull(GraphQLString),
231+
},
232+
},
233+
outputFields: {
234+
ok: {
235+
description: "It's always true.",
236+
type: new GraphQLNonNull(GraphQLBoolean),
237+
},
238+
},
239+
mutateAndGetPayload: async ({ email }, context) => {
240+
try {
241+
const { config, auth, info } = context;
242+
243+
await usersRouter.handleVerificationEmailRequest({
244+
body: {
245+
email,
246+
},
247+
config,
248+
auth,
249+
info,
250+
});
251+
252+
return { ok: true };
253+
} catch (e) {
254+
parseGraphQLSchema.handleError(e);
255+
}
256+
},
257+
});
258+
259+
parseGraphQLSchema.addGraphQLType(
260+
sendVerificationEmailMutation.args.input.type.ofType,
261+
true,
262+
true
263+
);
264+
parseGraphQLSchema.addGraphQLType(
265+
sendVerificationEmailMutation.type,
266+
true,
267+
true
268+
);
269+
parseGraphQLSchema.addGraphQLMutation(
270+
'sendVerificationEmail',
271+
sendVerificationEmailMutation,
272+
true,
273+
true
274+
);
174275
};
175276

176277
export { load };

0 commit comments

Comments
 (0)