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:
parent
23885f2699
commit
7d246dc909
@ -8,6 +8,7 @@ import {UserRequestConstrainsMWs} from "../middlewares/UserRequestConstrainsMWs"
|
|||||||
export class UserRouter{
|
export class UserRouter{
|
||||||
constructor(private app){
|
constructor(private app){
|
||||||
this.addLogin();
|
this.addLogin();
|
||||||
|
this.addGetSessionUser();
|
||||||
this.addChangePassword();
|
this.addChangePassword();
|
||||||
|
|
||||||
|
|
||||||
@ -25,6 +26,13 @@ export class UserRouter{
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private addGetSessionUser() {
|
||||||
|
this.app.get("/api/user/login",
|
||||||
|
AuthenticationMWs.authenticate,
|
||||||
|
AuthenticationMWs.renderUser
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
private addChangePassword() {
|
private addChangePassword() {
|
||||||
this.app.post("/api/user/:id/password",
|
this.app.post("/api/user/:id/password",
|
||||||
|
@ -32,10 +32,11 @@ export class Server {
|
|||||||
* Session above all
|
* Session above all
|
||||||
*/
|
*/
|
||||||
this.app.use(_session({
|
this.app.use(_session({
|
||||||
|
name:"pigallery2-session",
|
||||||
secret: 'keyboard cat',
|
secret: 'PiGallery2 secret',
|
||||||
cookie: {
|
cookie: {
|
||||||
maxAge: 60000
|
maxAge: 60000,
|
||||||
|
httpOnly: false
|
||||||
},
|
},
|
||||||
resave: true,
|
resave: true,
|
||||||
saveUninitialized: false
|
saveUninitialized: false
|
||||||
|
@ -6,6 +6,7 @@ import {Event} from "../../../common/event/Event";
|
|||||||
import {UserService} from "./user.service";
|
import {UserService} from "./user.service";
|
||||||
import {LoginCredential} from "../../../common/entities/LoginCredential";
|
import {LoginCredential} from "../../../common/entities/LoginCredential";
|
||||||
import {Message} from "../../../common/entities/Message";
|
import {Message} from "../../../common/entities/Message";
|
||||||
|
import { Cookie } from 'ng2-cookies/ng2-cookies';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthenticationService{
|
export class AuthenticationService{
|
||||||
@ -15,6 +16,24 @@ export class AuthenticationService{
|
|||||||
|
|
||||||
constructor(private _userService: UserService){
|
constructor(private _userService: UserService){
|
||||||
this.OnAuthenticated = new Event();
|
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){
|
public login(credential:LoginCredential){
|
||||||
@ -30,7 +49,7 @@ export class AuthenticationService{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public isAuthenticated():boolean{
|
public isAuthenticated():boolean{
|
||||||
return this._user && this._user != null;
|
return (this._user && this._user != null) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,5 +21,8 @@ export class UserService extends NetworkService{
|
|||||||
return this.postJson("/user/login",{"loginCredential": credential});
|
return this.postJson("/user/login",{"loginCredential": credential});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getSessionUser(): Promise<Message<User>>{
|
||||||
|
return this.getJson("/user/login");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"angular2": "^2.0.0-beta.11",
|
"angular2": "^2.0.0-beta.11",
|
||||||
|
"angular2-cookie": "^1.0.8",
|
||||||
"body-parser": "^1.15.0",
|
"body-parser": "^1.15.0",
|
||||||
"core-js": "^2.2.1",
|
"core-js": "^2.2.1",
|
||||||
"debug": "^2.2.0",
|
"debug": "^2.2.0",
|
||||||
@ -29,11 +30,12 @@
|
|||||||
"express-session": "^1.13.0",
|
"express-session": "^1.13.0",
|
||||||
"mime": "^1.3.4",
|
"mime": "^1.3.4",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0",
|
||||||
|
"ng2-cookies": "^0.1.4",
|
||||||
|
"rxjs": "5.0.0-beta.2",
|
||||||
"ts-loader": "^0.8.1",
|
"ts-loader": "^0.8.1",
|
||||||
"typescript": "^1.8.9",
|
"typescript": "^1.8.9",
|
||||||
"typings": "^0.7.9",
|
"typings": "^0.7.9",
|
||||||
"webpack": "^1.12.14",
|
"webpack": "^1.12.14",
|
||||||
"rxjs": "5.0.0-beta.2",
|
|
||||||
"zone.js": "0.6.4"
|
"zone.js": "0.6.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user