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 {
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));
}*/
}
//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();
}
}

View File

@ -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,

View File

@ -62,11 +62,19 @@ export class AppComponent implements OnInit {
}
ngOnInit() {
this._authenticationService.OnAuthenticated.on((user:User) => {
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"]);
}
}
});

View File

@ -15,7 +15,17 @@
<li class="active"><a [routerLink]="['Gallery',{directory: '/'}]">Gallery</a></li>
<li><a [routerLink]="['Admin']">Admin</a></li>
</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>
</div><!--/.nav-collapse -->
</div>
</nav>

View File

@ -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();
}
}

View File

@ -20,12 +20,15 @@ export class GalleryService {
}
public getDirectory(directoryName:string):Promise<Message<ContentWrapper>> {
this.content = new ContentWrapper();
return this._networkService.getJson("/gallery/content/" + directoryName).then(
(message:Message<ContentWrapper>) => {
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;
}

View File

@ -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);
});

View File

@ -17,10 +17,10 @@ declare module ServerInject {
export class AuthenticationService {
private _user:User = null;
public OnAuthenticated:Event<User>;
public OnUserChanged:Event<User>;
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);
}
}

View File

@ -13,6 +13,10 @@ export class UserService {
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>> {
return this._networkService.postJson("/user/login", {"loginCredential": credential});