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

implementing auto relogin if session is present

This commit is contained in:
Braun Patrik 2016-03-20 20:06:14 +01:00
parent 23885f2699
commit 7d246dc909
5 changed files with 38 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import {UserRequestConstrainsMWs} from "../middlewares/UserRequestConstrainsMWs"
export class UserRouter{
constructor(private app){
this.addLogin();
this.addGetSessionUser();
this.addChangePassword();
@ -25,6 +26,13 @@ export class UserRouter{
);
};
private addGetSessionUser() {
this.app.get("/api/user/login",
AuthenticationMWs.authenticate,
AuthenticationMWs.renderUser
);
};
private addChangePassword() {
this.app.post("/api/user/:id/password",

View File

@ -32,10 +32,11 @@ export class Server {
* Session above all
*/
this.app.use(_session({
secret: 'keyboard cat',
name:"pigallery2-session",
secret: 'PiGallery2 secret',
cookie: {
maxAge: 60000
maxAge: 60000,
httpOnly: false
},
resave: true,
saveUninitialized: false

View File

@ -6,6 +6,7 @@ import {Event} from "../../../common/event/Event";
import {UserService} from "./user.service";
import {LoginCredential} from "../../../common/entities/LoginCredential";
import {Message} from "../../../common/entities/Message";
import { Cookie } from 'ng2-cookies/ng2-cookies';
@Injectable()
export class AuthenticationService{
@ -15,6 +16,24 @@ export class AuthenticationService{
constructor(private _userService: UserService){
this.OnAuthenticated = new Event();
//picking up session..
if(this.isAuthenticated() == false && Cookie.getCookie('pigallery2-session') != null){
this.getSessionUser();
}
}
private getSessionUser(){
this._userService.getSessionUser().then( (message:Message<User>) =>{
console.log(message);
if(message.errors){
console.log(message.errors);
}else{
this._user = message.result;
this.OnAuthenticated.trigger(this._user);
}
});
}
public login(credential:LoginCredential){
@ -30,7 +49,7 @@ export class AuthenticationService{
}
public isAuthenticated():boolean{
return this._user && this._user != null;
return (this._user && this._user != null) ? true : false;
}

View File

@ -21,5 +21,8 @@ export class UserService extends NetworkService{
return this.postJson("/user/login",{"loginCredential": credential});
}
public getSessionUser(): Promise<Message<User>>{
return this.getJson("/user/login");
}
}

View File

@ -22,6 +22,7 @@
},
"dependencies": {
"angular2": "^2.0.0-beta.11",
"angular2-cookie": "^1.0.8",
"body-parser": "^1.15.0",
"core-js": "^2.2.1",
"debug": "^2.2.0",
@ -29,11 +30,12 @@
"express-session": "^1.13.0",
"mime": "^1.3.4",
"morgan": "^1.7.0",
"ng2-cookies": "^0.1.4",
"rxjs": "5.0.0-beta.2",
"ts-loader": "^0.8.1",
"typescript": "^1.8.9",
"typings": "^0.7.9",
"webpack": "^1.12.14",
"rxjs": "5.0.0-beta.2",
"zone.js": "0.6.4"
},
"devDependencies": {