From cbcc68dd86a0f0881dc2180bb0326deaa48058ae Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Mon, 16 May 2016 23:15:03 +0200 Subject: [PATCH] implementing logout --- backend/middlewares/AuthenticationMWs.ts | 11 ++++++++--- backend/routes/UserRouter.ts | 10 ++++++++++ frontend/app/app.component.ts | 16 ++++++++++++---- frontend/app/frame/frame.component.html | 10 ++++++++++ frontend/app/frame/frame.component.ts | 13 +++++++++++-- frontend/app/gallery/gallery.service.ts | 3 +++ .../network/autehentication.service.spec.ts | 6 +++--- .../app/model/network/authentication.service.ts | 13 +++++++++---- frontend/app/model/network/user.service.ts | 4 ++++ 9 files changed, 70 insertions(+), 16 deletions(-) diff --git a/backend/middlewares/AuthenticationMWs.ts b/backend/middlewares/AuthenticationMWs.ts index 24fffbb4..d180bc28 100644 --- a/backend/middlewares/AuthenticationMWs.ts +++ b/backend/middlewares/AuthenticationMWs.ts @@ -9,9 +9,9 @@ import {ObjectManagerRepository} from "../model/ObjectManagerRepository"; export class AuthenticationMWs { public static authenticate(req:Request, res:Response, next:NextFunction) { - /* if (typeof req.session.user === 'undefined') { - return next(new Error(ErrorCodes.NOT_AUTHENTICATED)); - }*/ + if (typeof req.session.user === 'undefined') { + return next(new Error(ErrorCodes.NOT_AUTHENTICATED)); + } //TODO: uncomment return next(); } @@ -38,6 +38,7 @@ export class AuthenticationMWs { (typeof req.body.loginCredential.password === 'undefined')) { return next(); } + //lets find the user ObjectManagerRepository.getInstance().getUserManager().findOne({ name: req.body.loginCredential.username, @@ -54,5 +55,9 @@ export class AuthenticationMWs { }); } + public static logout(req:Request, res:Response, next:NextFunction) { + delete req.session.user; + return next(); + } } \ No newline at end of file diff --git a/backend/routes/UserRouter.ts b/backend/routes/UserRouter.ts index 6dd765ec..72a252d6 100644 --- a/backend/routes/UserRouter.ts +++ b/backend/routes/UserRouter.ts @@ -9,6 +9,7 @@ import {RenderingMWs} from "../middlewares/RenderingMWs"; export class UserRouter { constructor(private app) { this.addLogin(); + this.addLogout(); this.addGetSessionUser(); this.addChangePassword(); @@ -27,6 +28,15 @@ export class UserRouter { ); }; + private addLogout() { + this.app.post("/api/user/logout", + AuthenticationMWs.authenticate, + AuthenticationMWs.logout, + RenderingMWs.renderOK + ); + }; + + private addGetSessionUser() { this.app.get("/api/user/login", AuthenticationMWs.authenticate, diff --git a/frontend/app/app.component.ts b/frontend/app/app.component.ts index 4f464b33..4c444588 100644 --- a/frontend/app/app.component.ts +++ b/frontend/app/app.component.ts @@ -62,11 +62,19 @@ export class AppComponent implements OnInit { } ngOnInit() { - this._authenticationService.OnAuthenticated.on((user:User) => { - if (this._router.isRouteActive(this._router.generate(['Login']))) { - console.log("routing"); - this._router.navigate(["Gallery", {directory: ""}]); + this._authenticationService.OnUserChanged.on((user:User) => { + if (user != null) { + if (this._router.isRouteActive(this._router.generate(['Login']))) { + console.log("routing"); + this._router.navigate(["Gallery", {directory: ""}]); + } + } else { + if (!this._router.isRouteActive(this._router.generate(['Login']))) { + console.log("routing"); + this._router.navigate(["Login"]); + } } + }); diff --git a/frontend/app/frame/frame.component.html b/frontend/app/frame/frame.component.html index cdd8da99..531d434d 100644 --- a/frontend/app/frame/frame.component.html +++ b/frontend/app/frame/frame.component.html @@ -15,7 +15,17 @@
  • Gallery
  • Admin
  • + + + diff --git a/frontend/app/frame/frame.component.ts b/frontend/app/frame/frame.component.ts index be5c0472..26cb625d 100644 --- a/frontend/app/frame/frame.component.ts +++ b/frontend/app/frame/frame.component.ts @@ -2,6 +2,8 @@ import {Component, ViewEncapsulation} from "@angular/core"; import {RouterLink} from "@angular/router-deprecated"; +import {AuthenticationService} from "../model/network/authentication.service"; +import {User} from "../../../common/entities/User"; @Component({ selector: 'app-frame', @@ -10,9 +12,16 @@ import {RouterLink} from "@angular/router-deprecated"; encapsulation: ViewEncapsulation.Emulated }) export class FrameComponent { - constructor() { + + user:User; + + constructor(private _authService:AuthenticationService) { + this.user = this._authService.getUser(); } - + logout() { + this._authService.logout(); + } + } diff --git a/frontend/app/gallery/gallery.service.ts b/frontend/app/gallery/gallery.service.ts index c6bf4747..599460e1 100644 --- a/frontend/app/gallery/gallery.service.ts +++ b/frontend/app/gallery/gallery.service.ts @@ -20,12 +20,15 @@ export class GalleryService { } public getDirectory(directoryName:string):Promise> { + this.content = new ContentWrapper(); return this._networkService.getJson("/gallery/content/" + directoryName).then( (message:Message) => { if (!message.error && message.result) { + message.result.directory.photos.forEach((photo:Photo) => { photo.directory = message.result.directory; }); + this.lastDirectory = message.result.directory; this.content = message.result; } diff --git a/frontend/app/model/network/autehentication.service.spec.ts b/frontend/app/model/network/autehentication.service.spec.ts index 1df6dd3a..1a7b4d85 100644 --- a/frontend/app/model/network/autehentication.service.spec.ts +++ b/frontend/app/model/network/autehentication.service.spec.ts @@ -37,10 +37,10 @@ describe('AuthenticationService', () => { it('should have Authenticated use', inject([AuthenticationService], (authService) => { - spyOn(authService.OnAuthenticated, "trigger").and.callThrough(); + spyOn(authService.OnUserChanged, "trigger").and.callThrough(); authService.login(); - authService.OnAuthenticated.on(() => { - expect(authService.OnAuthenticated.trigger).toHaveBeenCalled(); + authService.OnUserChanged.on(() => { + expect(authService.OnUserChanged.trigger).toHaveBeenCalled(); expect(authService.getUser()).not.toBe(null); expect(authService.isAuthenticated()).toBe(true); }); diff --git a/frontend/app/model/network/authentication.service.ts b/frontend/app/model/network/authentication.service.ts index 440034da..cf2581ea 100644 --- a/frontend/app/model/network/authentication.service.ts +++ b/frontend/app/model/network/authentication.service.ts @@ -17,10 +17,10 @@ declare module ServerInject { export class AuthenticationService { private _user:User = null; - public OnAuthenticated:Event; + public OnUserChanged:Event; constructor(private _userService:UserService) { - this.OnAuthenticated = new Event(); + this.OnUserChanged = new Event(); //picking up session.. if (this.isAuthenticated() == false && Cookie.getCookie('pigallery2-session') != null) { @@ -39,14 +39,14 @@ export class AuthenticationService { console.log(message.error); } else { this._user = message.result; - this.OnAuthenticated.trigger(this._user); + this.OnUserChanged.trigger(this._user); } }); } private setUser(user:User) { this._user = user; - this.OnAuthenticated.trigger(this._user); + this.OnUserChanged.trigger(this._user); } public login(credential:LoginCredential) { @@ -68,5 +68,10 @@ export class AuthenticationService { return this._user; } + public logout() { + this._userService.logout(); + this.setUser(null); + } + } diff --git a/frontend/app/model/network/user.service.ts b/frontend/app/model/network/user.service.ts index afdf05bd..69c12c65 100644 --- a/frontend/app/model/network/user.service.ts +++ b/frontend/app/model/network/user.service.ts @@ -13,6 +13,10 @@ export class UserService { constructor(private _networkService:NetworkService) { } + public logout():Promise> { + console.log("call logout"); + return this._networkService.postJson("/user/logout"); + } public login(credential:LoginCredential):Promise> { return this._networkService.postJson("/user/login", {"loginCredential": credential});