1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

implementing logout

This commit is contained in:
Braun Patrik 2016-05-16 23:15:03 +02:00
parent 540df6c6a8
commit cbcc68dd86
9 changed files with 70 additions and 16 deletions

View File

@ -9,9 +9,9 @@ import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
export class AuthenticationMWs { export class AuthenticationMWs {
public static authenticate(req:Request, res:Response, next:NextFunction) { public static authenticate(req:Request, res:Response, next:NextFunction) {
/* if (typeof req.session.user === 'undefined') { if (typeof req.session.user === 'undefined') {
return next(new Error(ErrorCodes.NOT_AUTHENTICATED)); return next(new Error(ErrorCodes.NOT_AUTHENTICATED));
}*/ }
//TODO: uncomment //TODO: uncomment
return next(); return next();
} }
@ -38,6 +38,7 @@ export class AuthenticationMWs {
(typeof req.body.loginCredential.password === 'undefined')) { (typeof req.body.loginCredential.password === 'undefined')) {
return next(); return next();
} }
//lets find the user //lets find the user
ObjectManagerRepository.getInstance().getUserManager().findOne({ ObjectManagerRepository.getInstance().getUserManager().findOne({
name: req.body.loginCredential.username, 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();
}
} }

View File

@ -9,6 +9,7 @@ import {RenderingMWs} from "../middlewares/RenderingMWs";
export class UserRouter { export class UserRouter {
constructor(private app) { constructor(private app) {
this.addLogin(); this.addLogin();
this.addLogout();
this.addGetSessionUser(); this.addGetSessionUser();
this.addChangePassword(); 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() { private addGetSessionUser() {
this.app.get("/api/user/login", this.app.get("/api/user/login",
AuthenticationMWs.authenticate, AuthenticationMWs.authenticate,

View File

@ -62,11 +62,19 @@ export class AppComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this._authenticationService.OnAuthenticated.on((user:User) => { this._authenticationService.OnUserChanged.on((user:User) => {
if (this._router.isRouteActive(this._router.generate(['Login']))) { if (user != null) {
console.log("routing"); if (this._router.isRouteActive(this._router.generate(['Login']))) {
this._router.navigate(["Gallery", {directory: ""}]); console.log("routing");
this._router.navigate(["Gallery", {directory: ""}]);
}
} else {
if (!this._router.isRouteActive(this._router.generate(['Login']))) {
console.log("routing");
this._router.navigate(["Login"]);
}
} }
}); });

View File

@ -15,7 +15,17 @@
<li class="active"><a [routerLink]="['Gallery',{directory: '/'}]">Gallery</a></li> <li class="active"><a [routerLink]="['Gallery',{directory: '/'}]">Gallery</a></li>
<li><a [routerLink]="['Admin']">Admin</a></li> <li><a [routerLink]="['Admin']">Admin</a></li>
</ul> </ul>
<ul class="nav navbar-nav navbar-right">
<li>
<p class="navbar-text">{{user.name}}</p>
</li>
<li>
<a style="cursor: pointer" (click)="logout()">Logout</a>
</li>
</ul>
<ng-content select="[navbar]"></ng-content> <ng-content select="[navbar]"></ng-content>
</div><!--/.nav-collapse --> </div><!--/.nav-collapse -->
</div> </div>
</nav> </nav>

View File

@ -2,6 +2,8 @@
import {Component, ViewEncapsulation} from "@angular/core"; import {Component, ViewEncapsulation} from "@angular/core";
import {RouterLink} from "@angular/router-deprecated"; import {RouterLink} from "@angular/router-deprecated";
import {AuthenticationService} from "../model/network/authentication.service";
import {User} from "../../../common/entities/User";
@Component({ @Component({
selector: 'app-frame', selector: 'app-frame',
@ -10,9 +12,16 @@ import {RouterLink} from "@angular/router-deprecated";
encapsulation: ViewEncapsulation.Emulated encapsulation: ViewEncapsulation.Emulated
}) })
export class FrameComponent { export class FrameComponent {
constructor() {
user:User;
constructor(private _authService:AuthenticationService) {
this.user = this._authService.getUser();
} }
logout() {
this._authService.logout();
}
} }

View File

@ -20,12 +20,15 @@ export class GalleryService {
} }
public getDirectory(directoryName:string):Promise<Message<ContentWrapper>> { public getDirectory(directoryName:string):Promise<Message<ContentWrapper>> {
this.content = new ContentWrapper();
return this._networkService.getJson("/gallery/content/" + directoryName).then( return this._networkService.getJson("/gallery/content/" + directoryName).then(
(message:Message<ContentWrapper>) => { (message:Message<ContentWrapper>) => {
if (!message.error && message.result) { if (!message.error && message.result) {
message.result.directory.photos.forEach((photo:Photo) => { message.result.directory.photos.forEach((photo:Photo) => {
photo.directory = message.result.directory; photo.directory = message.result.directory;
}); });
this.lastDirectory = message.result.directory; this.lastDirectory = message.result.directory;
this.content = message.result; this.content = message.result;
} }

View File

@ -37,10 +37,10 @@ describe('AuthenticationService', () => {
it('should have Authenticated use', inject([AuthenticationService], (authService) => { it('should have Authenticated use', inject([AuthenticationService], (authService) => {
spyOn(authService.OnAuthenticated, "trigger").and.callThrough(); spyOn(authService.OnUserChanged, "trigger").and.callThrough();
authService.login(); authService.login();
authService.OnAuthenticated.on(() => { authService.OnUserChanged.on(() => {
expect(authService.OnAuthenticated.trigger).toHaveBeenCalled(); expect(authService.OnUserChanged.trigger).toHaveBeenCalled();
expect(authService.getUser()).not.toBe(null); expect(authService.getUser()).not.toBe(null);
expect(authService.isAuthenticated()).toBe(true); expect(authService.isAuthenticated()).toBe(true);
}); });

View File

@ -17,10 +17,10 @@ declare module ServerInject {
export class AuthenticationService { export class AuthenticationService {
private _user:User = null; private _user:User = null;
public OnAuthenticated:Event<User>; public OnUserChanged:Event<User>;
constructor(private _userService:UserService) { constructor(private _userService:UserService) {
this.OnAuthenticated = new Event(); this.OnUserChanged = new Event();
//picking up session.. //picking up session..
if (this.isAuthenticated() == false && Cookie.getCookie('pigallery2-session') != null) { if (this.isAuthenticated() == false && Cookie.getCookie('pigallery2-session') != null) {
@ -39,14 +39,14 @@ export class AuthenticationService {
console.log(message.error); console.log(message.error);
} else { } else {
this._user = message.result; this._user = message.result;
this.OnAuthenticated.trigger(this._user); this.OnUserChanged.trigger(this._user);
} }
}); });
} }
private setUser(user:User) { private setUser(user:User) {
this._user = user; this._user = user;
this.OnAuthenticated.trigger(this._user); this.OnUserChanged.trigger(this._user);
} }
public login(credential:LoginCredential) { public login(credential:LoginCredential) {
@ -68,5 +68,10 @@ export class AuthenticationService {
return this._user; return this._user;
} }
public logout() {
this._userService.logout();
this.setUser(null);
}
} }

View File

@ -13,6 +13,10 @@ export class UserService {
constructor(private _networkService:NetworkService) { constructor(private _networkService:NetworkService) {
} }
public logout():Promise<Message<string>> {
console.log("call logout");
return this._networkService.postJson("/user/logout");
}
public login(credential:LoginCredential):Promise<Message<User>> { public login(credential:LoginCredential):Promise<Message<User>> {
return this._networkService.postJson("/user/login", {"loginCredential": credential}); return this._networkService.postJson("/user/login", {"loginCredential": credential});