Skip to content

Commit feec168

Browse files
committed
Merge branch 'master' of https://github.com/parse-community/parse-server into ldap-auth-support
2 parents 6c97b00 + 5ed0885 commit feec168

File tree

6 files changed

+264
-6
lines changed

6 files changed

+264
-6
lines changed

package-lock.json

+150
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/CloudCode.spec.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -2219,10 +2219,14 @@ describe('afterFind hooks', () => {
22192219
it('should validate triggers correctly', () => {
22202220
expect(() => {
22212221
Parse.Cloud.beforeSave('_Session', () => {});
2222-
}).toThrow('Triggers are not supported for _Session class.');
2222+
}).toThrow(
2223+
'Only the afterLogout trigger is allowed for the _Session class.'
2224+
);
22232225
expect(() => {
22242226
Parse.Cloud.afterSave('_Session', () => {});
2225-
}).toThrow('Triggers are not supported for _Session class.');
2227+
}).toThrow(
2228+
'Only the afterLogout trigger is allowed for the _Session class.'
2229+
);
22262230
expect(() => {
22272231
Parse.Cloud.beforeSave('_PushStatus', () => {});
22282232
}).toThrow('Only afterSave is allowed on _PushStatus');
@@ -2247,6 +2251,22 @@ describe('afterFind hooks', () => {
22472251
expect(() => {
22482252
Parse.Cloud.beforeLogin('SomeClass', () => {});
22492253
}).toThrow('Only the _User class is allowed for the beforeLogin trigger');
2254+
expect(() => {
2255+
Parse.Cloud.afterLogout(() => {});
2256+
}).not.toThrow();
2257+
expect(() => {
2258+
Parse.Cloud.afterLogout('_Session', () => {});
2259+
}).not.toThrow();
2260+
expect(() => {
2261+
Parse.Cloud.afterLogout('_User', () => {});
2262+
}).toThrow(
2263+
'Only the _Session class is allowed for the afterLogout trigger.'
2264+
);
2265+
expect(() => {
2266+
Parse.Cloud.afterLogout('SomeClass', () => {});
2267+
}).toThrow(
2268+
'Only the _Session class is allowed for the afterLogout trigger.'
2269+
);
22502270
});
22512271

22522272
it('should skip afterFind hooks for aggregate', done => {
@@ -2436,6 +2456,22 @@ describe('beforeLogin hook', () => {
24362456
done();
24372457
});
24382458

2459+
it('should trigger afterLogout hook on logout', async done => {
2460+
let userId;
2461+
Parse.Cloud.afterLogout(req => {
2462+
expect(req.object.className).toEqual('_Session');
2463+
expect(req.object.id).toBeDefined();
2464+
const user = req.object.get('user');
2465+
expect(user).toBeDefined();
2466+
userId = user.id;
2467+
});
2468+
2469+
const user = await Parse.User.signUp('user', 'pass');
2470+
await Parse.User.logOut();
2471+
expect(user.id).toBe(userId);
2472+
done();
2473+
});
2474+
24392475
it('should have expected data in request', async done => {
24402476
Parse.Cloud.beforeLogin(req => {
24412477
expect(req.object).toBeDefined();

spec/ParseUser.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,24 @@ describe('Parse.User testing', () => {
15231523
done();
15241524
});
15251525

1526+
it('logout with provider should call afterLogout trigger', async done => {
1527+
const provider = getMockFacebookProvider();
1528+
Parse.User._registerAuthenticationProvider(provider);
1529+
1530+
let userId;
1531+
Parse.Cloud.afterLogout(req => {
1532+
expect(req.object.className).toEqual('_Session');
1533+
expect(req.object.id).toBeDefined();
1534+
const user = req.object.get('user');
1535+
expect(user).toBeDefined();
1536+
userId = user.id;
1537+
});
1538+
const user = await Parse.User._logInWith('facebook');
1539+
await Parse.User.logOut();
1540+
expect(user.id).toBe(userId);
1541+
done();
1542+
});
1543+
15261544
it('link with provider', async done => {
15271545
const provider = getMockFacebookProvider();
15281546
Parse.User._registerAuthenticationProvider(provider);

src/Routers/UsersRouter.js

+12
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ export class UsersRouter extends ClassesRouter {
302302
records.results[0].objectId
303303
)
304304
.then(() => {
305+
this._runAfterLogoutTrigger(req, records.results[0]);
305306
return Promise.resolve(success);
306307
});
307308
}
@@ -311,6 +312,17 @@ export class UsersRouter extends ClassesRouter {
311312
return Promise.resolve(success);
312313
}
313314

315+
_runAfterLogoutTrigger(req, session) {
316+
// After logout trigger
317+
maybeRunTrigger(
318+
TriggerTypes.afterLogout,
319+
req.auth,
320+
Parse.Session.fromJSON(Object.assign({ className: '_Session' }, session)),
321+
null,
322+
req.config
323+
);
324+
}
325+
314326
_throwOnBadEmailConfig(req) {
315327
try {
316328
Config.validateEmailConfiguration({

src/cloud-code/Parse.Cloud.js

+35
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,41 @@ ParseCloud.beforeLogin = function(handler) {
165165
);
166166
};
167167

168+
/**
169+
*
170+
* Registers the after logout function.
171+
*
172+
* **Available in Cloud Code only.**
173+
*
174+
* This function is triggered after a user logs out.
175+
*
176+
* ```
177+
* Parse.Cloud.afterLogout((request) => {
178+
* // code here
179+
* })
180+
*
181+
* ```
182+
*
183+
* @method afterLogout
184+
* @name Parse.Cloud.afterLogout
185+
* @param {Function} func The function to run after a logout. This function can be async and should take one parameter a {@link Parse.Cloud.TriggerRequest};
186+
*/
187+
ParseCloud.afterLogout = function(handler) {
188+
let className = '_Session';
189+
if (typeof handler === 'string' || isParseObjectConstructor(handler)) {
190+
// validation will occur downstream, this is to maintain internal
191+
// code consistency with the other hook types.
192+
className = getClassName(handler);
193+
handler = arguments[1];
194+
}
195+
triggers.addTrigger(
196+
triggers.Types.afterLogout,
197+
className,
198+
handler,
199+
Parse.applicationId
200+
);
201+
};
202+
168203
/**
169204
* Registers an after save function.
170205
*

0 commit comments

Comments
 (0)