Skip to content

feat: Add context to Cloud Code Triggers beforeLogin and afterLogin #8724

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

Merged
merged 3 commits into from
Sep 20, 2023
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
4 changes: 2 additions & 2 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@ describe('beforeLogin hook', () => {
expect(req.headers).toBeDefined();
expect(req.ip).toBeDefined();
expect(req.installationId).toBeDefined();
expect(req.context).toBeUndefined();
expect(req.context).toBeDefined();
});

await Parse.User.signUp('tupac', 'shakur');
Expand Down Expand Up @@ -3444,7 +3444,7 @@ describe('afterLogin hook', () => {
expect(req.headers).toBeDefined();
expect(req.ip).toBeDefined();
expect(req.installationId).toBeDefined();
expect(req.context).toBeUndefined();
expect(req.context).toBeDefined();
});

await Parse.User.signUp('testuser', 'p@ssword');
Expand Down
30 changes: 30 additions & 0 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ describe('Parse.User testing', () => {
}
});

it('user login with context', async () => {
let hit = 0;
const context = { foo: 'bar' };
Parse.Cloud.beforeLogin(req => {
expect(req.context).toEqual(context);
hit++;
});
Parse.Cloud.afterLogin(req => {
expect(req.context).toEqual(context);
hit++;
});
await Parse.User.signUp('asdf', 'zxcv');
await request({
method: 'POST',
url: 'http://localhost:8378/1/login',
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-REST-API-Key': 'rest',
'X-Parse-Cloud-Context': JSON.stringify(context),
'Content-Type': 'application/json',
},
body: {
_method: 'GET',
username: 'asdf',
password: 'zxcv',
},
});
expect(hit).toBe(2);
});

it('user login with non-string username with REST API', async done => {
await Parse.User.signUp('asdf', 'zxcv');
request({
Expand Down
6 changes: 4 additions & 2 deletions src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ export class UsersRouter extends ClassesRouter {
req.auth,
Parse.User.fromJSON(Object.assign({ className: '_User' }, user)),
null,
req.config
req.config,
req.info.context
);

// If we have some new validated authData update directly
Expand Down Expand Up @@ -291,7 +292,8 @@ export class UsersRouter extends ClassesRouter {
{ ...req.auth, user: afterLoginUser },
afterLoginUser,
null,
req.config
req.config,
req.info.context
);

if (authDataResponse) {
Expand Down
2 changes: 2 additions & 0 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export function getRequestObject(
triggerType === Types.afterSave ||
triggerType === Types.beforeDelete ||
triggerType === Types.afterDelete ||
triggerType === Types.beforeLogin ||
triggerType === Types.afterLogin ||
triggerType === Types.afterFind
) {
// Set a copy of the context on the request object.
Expand Down