diff --git a/backend/routes/UserRouter.ts b/backend/routes/UserRouter.ts index 885052c4..ffd9757f 100644 --- a/backend/routes/UserRouter.ts +++ b/backend/routes/UserRouter.ts @@ -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", diff --git a/backend/server.ts b/backend/server.ts index 1a55eafa..62b136a7 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -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 diff --git a/frontend/app/model/authentication.service.ts b/frontend/app/model/authentication.service.ts index 45336ec7..19fc6508 100644 --- a/frontend/app/model/authentication.service.ts +++ b/frontend/app/model/authentication.service.ts @@ -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) =>{ + 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; } diff --git a/frontend/app/model/user.service.ts b/frontend/app/model/user.service.ts index 69cb008d..917c0191 100644 --- a/frontend/app/model/user.service.ts +++ b/frontend/app/model/user.service.ts @@ -21,5 +21,8 @@ export class UserService extends NetworkService{ return this.postJson("/user/login",{"loginCredential": credential}); } + public getSessionUser(): Promise>{ + return this.getJson("/user/login"); + } } diff --git a/package.json b/package.json index 5d439b2b..4de025f7 100644 --- a/package.json +++ b/package.json @@ -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": {