Skip to content

Commit

Permalink
Merge pull request #42 from spira/feature/logout-listener
Browse files Browse the repository at this point in the history
Added logout listener functionality.
  • Loading branch information
zakhenry committed Mar 8, 2016
2 parents 47bd335 + 91f948d commit 7b7bfc4
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 39 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-jwt-auth",
"version": "3.5.1",
"version": "3.6.0",
"description": "Angular JSON Web Token Authentication Module",
"main": "dist/ngJwtAuth.js",
"dependencies": {
Expand Down
12 changes: 9 additions & 3 deletions dist/ngJwtAuth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ declare module NgJwtAuth {
interface IUserFactory {
(subClaim: string, tokenData: IJwtClaims): ng.IPromise<IUser>;
}
interface ILoginListener {
(user: IUser): any;
interface IUserEventListener {
(user: IUser): void;
}
interface IBase64Service {
encode(string: string): string;
Expand All @@ -117,6 +117,7 @@ declare module NgJwtAuth {
private userFactory;
private loginPromptFactory;
private loginListeners;
private logoutListeners;
private userLoggedInPromise;
private refreshTimerPromise;
private tokenData;
Expand Down Expand Up @@ -343,7 +344,12 @@ declare module NgJwtAuth {
* Register a login listener function
* @param loginListener
*/
registerLoginListener(loginListener: ILoginListener): void;
registerLoginListener(loginListener: IUserEventListener): void;
/**
* Register a logout listener function
* @param logoutListener
*/
registerLogoutListener(logoutListener: IUserEventListener): void;
/**
* Get a user's token given their identifier
* @param userIdentifier
Expand Down
60 changes: 34 additions & 26 deletions dist/ngJwtAuth.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ngJwtAuth.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-jwt-auth",
"version": "3.5.1",
"version": "3.6.0",
"description": "Angular JSON Web Token Authentication Module",
"main": "dist/ngJwtAuth.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/ngJwtAuthInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ module NgJwtAuth {
(subClaim:string, tokenData:IJwtClaims): ng.IPromise<IUser>;
}

export interface ILoginListener {
(user:IUser):any;
export interface IUserEventListener {
(user:IUser):void;
}

export interface IBase64Service {
Expand Down
20 changes: 15 additions & 5 deletions src/ngJwtAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module NgJwtAuth {
//private properties
private userFactory:IUserFactory;
private loginPromptFactory:ILoginPromptFactory;
private loginListeners:ILoginListener[] = [];
private loginListeners:IUserEventListener[] = [];
private logoutListeners:IUserEventListener[] = [];
private userLoggedInPromise:ng.IPromise<any>;

private refreshTimerPromise:ng.IPromise<any>;
Expand Down Expand Up @@ -513,9 +514,7 @@ module NgJwtAuth {
*/
private handleLogin(user:IUser):void {

_.each(this.loginListeners, (listener:ILoginListener) => {
listener(user);
});
_.invoke(this.loginListeners, _.call, null, user);

}

Expand Down Expand Up @@ -660,6 +659,9 @@ module NgJwtAuth {
public logout():void {
this.clearJWTToken();
this.loggedIn = false;

//call all logout listeners with user that is logged out
_.invoke(this.logoutListeners, _.call, null, this.user);
this.user = null;
}

Expand All @@ -668,10 +670,18 @@ module NgJwtAuth {
* Register a login listener function
* @param loginListener
*/
public registerLoginListener(loginListener:ILoginListener):void {
public registerLoginListener(loginListener:IUserEventListener):void {
this.loginListeners.push(loginListener);
}

/**
* Register a logout listener function
* @param logoutListener
*/
public registerLogoutListener(logoutListener:IUserEventListener):void {
this.logoutListeners.push(logoutListener);
}

/**
* Get a user's token given their identifier
* @param userIdentifier
Expand Down
22 changes: 22 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,28 @@ describe('Service tests', () => {

});

describe('Logout listening', () => {

let mockListener = sinon.stub();

it('should be able to register a logout listener', () => {

ngJwtAuthService.registerLogoutListener(mockListener);

expect(mockListener).not.to.have.been.called;

});

it('should notify the logout listener', () => {

ngJwtAuthService.logout();

expect(mockListener).to.have.been.called;

});

});

describe('Failed authentication', () => {


Expand Down

0 comments on commit 7b7bfc4

Please sign in to comment.