Skip to content

Commit

Permalink
feat: Add context to Cloud Code Triggers beforeLogin and `afterLogi…
Browse files Browse the repository at this point in the history
…n` (#8724)
  • Loading branch information
dplewis authored Sep 20, 2023
1 parent 0593985 commit a9c34ef
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
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

0 comments on commit a9c34ef

Please sign in to comment.