diff --git a/backend/middlewares/AuthenticationMWs.ts b/backend/middlewares/AuthenticationMWs.ts index 18002990..26d67b4f 100644 --- a/backend/middlewares/AuthenticationMWs.ts +++ b/backend/middlewares/AuthenticationMWs.ts @@ -34,23 +34,20 @@ export class AuthenticationMWs extends BaseMWs{ public static login(req:Request, res:Response, next:NextFunction){ //not enough parameter - if ((typeof req.body === 'undefined') || (typeof req.body.logincredential === 'undefined') || (typeof req.body.logincredential.username === 'undefined') || - (typeof req.body.logincredential.password === 'undefined')) { + if ((typeof req.body === 'undefined') || (typeof req.body.loginCredential === 'undefined') || (typeof req.body.loginCredential.username === 'undefined') || + (typeof req.body.loginCredential.password === 'undefined')) { return next(); } //lets find the user UserManager.findOne({ - username: req.body.logincredential.username + username: req.body.loginCredential.username, + password: req.body.loginCredential.password }, (err, result) => { if ((err) || (!result)) { return super.renderError(res,new Error(ErrorCodes.CREDENTIAL_NOT_FOUND)); } - //check password - if (result.password !== req.body.logincredential.password) { - return super.renderError(res,new Error(ErrorCodes.CREDENTIAL_NOT_FOUND)); - } req.session.user = result; @@ -61,6 +58,10 @@ export class AuthenticationMWs extends BaseMWs{ public static renderUser(req:Request, res:Response, next:NextFunction){ + if(!(req.session.user)){ + return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR)); + } + let user = Utils.clone(req.session.user); delete user.password; super.renderMessage(res,user); diff --git a/backend/routes/GalleryRouter.ts b/backend/routes/GalleryRouter.ts index 2021f856..1ba64975 100644 --- a/backend/routes/GalleryRouter.ts +++ b/backend/routes/GalleryRouter.ts @@ -16,7 +16,7 @@ export class GalleryRouter{ private addDirectoryList() { this.app.get(["/api/gallery/:directory","/api/gallery/"], - // AuthenticationMWs.authenticate, + AuthenticationMWs.authenticate, GalleryMWs.listDirectory ); }; @@ -24,7 +24,7 @@ export class GalleryRouter{ private addGetImage() { this.app.get(["/api/gallery/:directory/:image","/api/gallery/:image"], - // AuthenticationMWs.authenticate, + AuthenticationMWs.authenticate, GalleryMWs.renderImage ); }; diff --git a/backend/routes/PublicRouter.ts b/backend/routes/PublicRouter.ts index 74b4ec17..cb53a1a4 100644 --- a/backend/routes/PublicRouter.ts +++ b/backend/routes/PublicRouter.ts @@ -10,7 +10,7 @@ export class PublicRouter{ this.app.use('/node_modules',_express.static(_path.resolve(__dirname, './../../node_modules'))); var renderIndex = (req: _express.Request, res: _express.Response) => { - res.sendFile(_path.resolve(__dirname, './../frontend/index.html')); + res.sendFile(_path.resolve(__dirname, './../../frontend/index.html')); }; this.app.get(['/login',"/gallery"], renderIndex); diff --git a/backend/server.ts b/backend/server.ts index f96ec1aa..1a55eafa 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -2,6 +2,7 @@ import * as _express from 'express'; import * as _session from 'express-session'; +import * as _bodyParser from 'body-parser'; import * as _debug from 'debug'; import * as _http from 'http'; import {PublicRouter} from "./routes/PublicRouter"; @@ -39,7 +40,12 @@ export class Server { resave: true, saveUninitialized: false })); - + + /** + * Parse parameters in POST + */ + // for parsing application/json + this.app.use(_bodyParser.json()); new PublicRouter(this.app); new UserRouter(this.app); diff --git a/common/entities/LoginCredential.ts b/common/entities/LoginCredential.ts index 2d6d587e..5866a743 100644 --- a/common/entities/LoginCredential.ts +++ b/common/entities/LoginCredential.ts @@ -1,5 +1,5 @@ export class LoginCredential{ - constructor(public username?:string, public password?:string){ + constructor(public username:string = null, public password:string = null){ } } \ No newline at end of file diff --git a/frontend/app/app.component.ts b/frontend/app/app.component.ts index 7cc2aa8c..e82ffdc6 100644 --- a/frontend/app/app.component.ts +++ b/frontend/app/app.component.ts @@ -10,6 +10,7 @@ import {User} from "../../common/entities/User"; import {Router, Location} from "angular2/router"; import {HTTP_PROVIDERS} from "angular2/http"; import {UserService} from "./model/user.service"; +import {GalleryService} from "./gallery/gallery.service"; @@ -22,6 +23,7 @@ import {UserService} from "./model/user.service"; HTTP_PROVIDERS, ROUTER_PROVIDERS, UserService, + GalleryService, AuthenticationService ] }) diff --git a/frontend/app/gallery/gallery.component.html b/frontend/app/gallery/gallery.component.html index dc2cd0a3..bdedb5e1 100644 --- a/frontend/app/gallery/gallery.component.html +++ b/frontend/app/gallery/gallery.component.html @@ -1 +1,4 @@ -

Gallery

\ No newline at end of file +

Gallery

+
+ a photo +
\ No newline at end of file diff --git a/frontend/app/gallery/gallery.component.ts b/frontend/app/gallery/gallery.component.ts index 63ef5fd1..8ca85652 100644 --- a/frontend/app/gallery/gallery.component.ts +++ b/frontend/app/gallery/gallery.component.ts @@ -2,15 +2,26 @@ import {Component, OnInit} from 'angular2/core'; import {AuthenticationService} from "../model/authentication.service"; -import {Router,Location} from "angular2/router"; +import {Router, Location, RouteParams} from "angular2/router"; +import {GalleryService} from "./gallery.service"; +import {Directory} from "../../../common/entities/Directory"; +import {Message} from "../../../common/entities/Message"; +import {GalleryPhotoComponent} from "./photo/photo.gallery.component"; @Component({ selector: 'gallery', - templateUrl: 'app/gallery/gallery.component.html' + templateUrl: 'app/gallery/gallery.component.html', + directives:[GalleryPhotoComponent] }) export class GalleryComponent implements OnInit{ - constructor(private _authService: AuthenticationService, private _router: Router, private _location:Location) { + directory:Directory = new Directory(-1,"","/",new Date(),[],[]); + + constructor(private _galleryService:GalleryService, + private _params: RouteParams, + private _authService: AuthenticationService, + private _router: Router, + private _location:Location) { } @@ -18,7 +29,18 @@ export class GalleryComponent implements OnInit{ if (!this._authService.isAuthenticated()) { this._location.replaceState('/'); // clears browser history so they can't navigate with back button this._router.navigate(['Login']); + return; } + let directoryName = this._params.get('directory'); + directoryName = directoryName ? directoryName : ""; + this._galleryService.getDirectory(directoryName).then(( message:Message) => { + if(message.errors){ + //TODO: implement + return; + } + + this.directory = message.result; + }); } } diff --git a/frontend/app/gallery/gallery.service.ts b/frontend/app/gallery/gallery.service.ts new file mode 100644 index 00000000..82f66f6b --- /dev/null +++ b/frontend/app/gallery/gallery.service.ts @@ -0,0 +1,25 @@ +/// + +import {Injectable} from 'angular2/core'; +import {NetworkService} from "../model/network.service"; +import {Http} from "angular2/http"; +import {Message} from "../../../common/entities/Message"; +import {Directory} from "../../../common/entities/Directory"; + +@Injectable() +export class GalleryService extends NetworkService{ + + + constructor(_http:Http){ + super(_http); + } + + public getDirectory(directoryName:string): Promise>{ + return this.getJson("/gallery/"+directoryName); + } + + + + + +} diff --git a/frontend/app/gallery/photo/photo.gallery.component.html b/frontend/app/gallery/photo/photo.gallery.component.html new file mode 100644 index 00000000..4e64df54 --- /dev/null +++ b/frontend/app/gallery/photo/photo.gallery.component.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/frontend/app/gallery/photo/photo.gallery.component.ts b/frontend/app/gallery/photo/photo.gallery.component.ts new file mode 100644 index 00000000..600d5a04 --- /dev/null +++ b/frontend/app/gallery/photo/photo.gallery.component.ts @@ -0,0 +1,21 @@ +/// + +import {Component, Input, OnInit} from 'angular2/core'; +import {Photo} from "../../../../common/entities/Photo"; + +@Component({ + selector: 'gallery-photo', + templateUrl: 'app/gallery/photo/photo.gallery.component.html' +}) +export class GalleryPhotoComponent{ + @Input() photo: Photo; + + constructor() { + } + + getPhotoPath(){ + return "/api/gallery/"+this.photo.name; + } + +} + diff --git a/frontend/app/login/login.component.ts b/frontend/app/login/login.component.ts index a1b54b4e..512dcf94 100644 --- a/frontend/app/login/login.component.ts +++ b/frontend/app/login/login.component.ts @@ -23,7 +23,7 @@ export class LoginComponent implements OnInit{ } } - onLogin(){ + onLogin(){ this._authService.login(this.loginCredential); } } diff --git a/frontend/app/model/authentication.service.ts b/frontend/app/model/authentication.service.ts index 891b021d..45336ec7 100644 --- a/frontend/app/model/authentication.service.ts +++ b/frontend/app/model/authentication.service.ts @@ -20,7 +20,7 @@ export class AuthenticationService{ public login(credential:LoginCredential){ this._userService.login(credential).then( (message:Message) =>{ console.log(message); - if(message.errors && message.errors.length > 0){ + if(message.errors){ console.log(message.errors); }else{ this._user = message.result; diff --git a/frontend/app/model/network.service.ts b/frontend/app/model/network.service.ts index 1599f7f8..4d346284 100644 --- a/frontend/app/model/network.service.ts +++ b/frontend/app/model/network.service.ts @@ -11,30 +11,37 @@ export class NetworkService{ constructor(protected _http:Http){ } - private callJson(method:string, url:string, data:any = {}){ - let body = JSON.stringify({ data }); + private callJson(method:string, url:string, data:any = {}): Promise{ + let body = JSON.stringify( data ); let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); - console.log(this._http.post(this._baseUrl+url, body, options)); + + if(method == "get"){ + return this._http[method](this._baseUrl+url, options) + .toPromise() + .then(res => > res.json()) + .catch(NetworkService.handleError); + } + return this._http[method](this._baseUrl+url, body, options) .toPromise() .then(res => > res.json()) .catch(NetworkService.handleError); } - protected postJson(url:string, data:any = {}){ + protected postJson(url:string, data:any = {}): Promise{ return this.callJson("post",url,data); } - protected putJson(url:string, data:any = {}){ + protected putJson(url:string, data:any = {}): Promise{ return this.callJson("put",url,data); } - protected getJson(url:string, data:any = {}){ - return this.callJson("get",url,data); + protected getJson(url:string): Promise{ + return this.callJson("get",url); } - protected deleteJson(url:string, data:any = {}){ + protected deleteJson(url:string, data:any = {}): Promise{ return this.callJson("delete",url,data); } diff --git a/frontend/app/model/user.service.ts b/frontend/app/model/user.service.ts index 164e1e2b..69cb008d 100644 --- a/frontend/app/model/user.service.ts +++ b/frontend/app/model/user.service.ts @@ -4,6 +4,8 @@ import {Injectable} from 'angular2/core'; import {LoginCredential} from "../../../common/entities/LoginCredential"; import {Http} from "angular2/http"; import {NetworkService} from "./network.service"; +import {User} from "../../../common/entities/User"; +import {Message} from "../../../common/entities/Message"; @Injectable() export class UserService extends NetworkService{ @@ -15,8 +17,8 @@ export class UserService extends NetworkService{ } - public login(credential:LoginCredential){ - return this.postJson("/user/login",credential); + public login(credential:LoginCredential): Promise>{ + return this.postJson("/user/login",{"loginCredential": credential}); } diff --git a/package.json b/package.json index 3baded01..aa9264c9 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "dependencies": { "angular2": "^2.0.0-beta.11", + "body-parser": "^1.15.0", "core-js": "^2.2.1", "debug": "^2.2.0", "es6-promise": "^3.1.2",