mirror of
https://github.com/bpatrik/pigallery2.git
synced 2024-11-24 08:42:24 +02:00
creating network and login services
This commit is contained in:
parent
a288ba71c6
commit
df717f55e1
@ -26,7 +26,6 @@ export class Server {
|
||||
|
||||
this.app.use(_express.static(path.resolve(__dirname, './../frontend')));
|
||||
this.app.use('/node_modules',_express.static(path.resolve(__dirname, './../node_modules')));
|
||||
this.app.use('/common',_express.static(path.resolve(__dirname, './../common'))); //TODO:remove after adding webpack
|
||||
|
||||
var renderIndex = (req: _express.Request, res: _express.Response) => {
|
||||
res.sendFile(path.resolve(__dirname, './../frontend/index.html'));
|
||||
|
14
common/MessageTypes.ts
Normal file
14
common/MessageTypes.ts
Normal file
@ -0,0 +1,14 @@
|
||||
export var MessageTypes = {
|
||||
Client:{
|
||||
Login:{
|
||||
Authenticate:"Authenticate"
|
||||
}
|
||||
|
||||
},
|
||||
Server:{
|
||||
Login:{
|
||||
Authenticated:"Authenticated"
|
||||
}
|
||||
|
||||
}
|
||||
};
|
10
common/Utils.ts
Normal file
10
common/Utils.ts
Normal file
@ -0,0 +1,10 @@
|
||||
/// <reference path="../typings/tsd.d.ts"/>
|
||||
|
||||
export class Utils {
|
||||
|
||||
static clone<T>(object:T):T {
|
||||
return JSON.parse(JSON.stringify(object));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
export class User {
|
||||
constructor(private name?:string = null,private email?:string = null, private password?:string = null){}
|
||||
}
|
3
common/entities/Error.ts
Normal file
3
common/entities/Error.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export class Error{
|
||||
|
||||
}
|
5
common/entities/LoginCredential.ts
Normal file
5
common/entities/LoginCredential.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export class LoginCredential{
|
||||
constructor(public username?:string, public password?:string){
|
||||
|
||||
}
|
||||
}
|
5
common/entities/Message.ts
Normal file
5
common/entities/Message.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import {Error} from "./Error";
|
||||
|
||||
export class Message<T>{
|
||||
constructor(public error:Error,public result:T){}
|
||||
}
|
3
common/entities/User.ts
Normal file
3
common/entities/User.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export class User {
|
||||
constructor(public name?:string,public email?:string, public password?:string){}
|
||||
}
|
@ -3,6 +3,8 @@
|
||||
import { Component } from 'angular2/core';
|
||||
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';
|
||||
import {LoginComponent} from "./login/login.component";
|
||||
import {NetworkService} from "./model/network.service";
|
||||
import {LoginService} from "./login/login.service";
|
||||
|
||||
|
||||
|
||||
@ -12,7 +14,9 @@ import {LoginComponent} from "./login/login.component";
|
||||
template: `<router-outlet></router-outlet>`,
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
providers: [
|
||||
ROUTER_PROVIDERS
|
||||
ROUTER_PROVIDERS,
|
||||
NetworkService,
|
||||
LoginService
|
||||
]
|
||||
})
|
||||
@RouteConfig([
|
||||
|
@ -1,7 +1,7 @@
|
||||
<form>
|
||||
<div class="group">
|
||||
<input type="text" [(ngModel)]="user.name"><span class="highlight"></span><span class="bar"></span>
|
||||
<label *ngIf="!user.name">Username (or e-mail)</label>
|
||||
<input type="text" [(ngModel)]="user.username"><span class="highlight"></span><span class="bar"></span>
|
||||
<label *ngIf="!user.username">Username (or e-mail)</label>
|
||||
</div>
|
||||
<div class="group">
|
||||
<input type="password" [(ngModel)]="user.password"><span class="highlight"></span><span class="bar"></span>
|
||||
|
@ -1,7 +1,8 @@
|
||||
///<reference path="../../../typings/tsd.d.ts"/>
|
||||
|
||||
import { Component, OnInit } from 'angular2/core';
|
||||
import {User} from '../../../common/enities/User';
|
||||
import {LoginCredential} from '../../../common/entities/LoginCredential';
|
||||
import {LoginService} from "./login.service";
|
||||
|
||||
@Component({
|
||||
selector: 'login',
|
||||
@ -9,9 +10,9 @@ import {User} from '../../../common/enities/User';
|
||||
styleUrls: ['app/login/login.component.css']
|
||||
})
|
||||
export class LoginComponent{
|
||||
user:User;
|
||||
constructor() {
|
||||
this.user = new User();
|
||||
user: LoginCredential;
|
||||
constructor(private _loginService: LoginService) {
|
||||
this.user = new LoginCredential();
|
||||
}
|
||||
}
|
||||
|
||||
|
35
frontend/app/login/login.service.ts
Normal file
35
frontend/app/login/login.service.ts
Normal file
@ -0,0 +1,35 @@
|
||||
///<reference path="../../../typings/tsd.d.ts"/>
|
||||
|
||||
import {Injectable} from 'angular2/core';
|
||||
import {NetworkService} from "../model/network.service";
|
||||
import {OnInit} from "angular2/core";
|
||||
import {MessageTypes} from "../../../common/MessageTypes";
|
||||
import {Utils} from "../../../common/Utils";
|
||||
import {LoginCredential} from "../../../common/entities/LoginCredential";
|
||||
import {User} from "../../../common/entities/User";
|
||||
import {Message} from "../../../common/entities/Message";
|
||||
|
||||
@Injectable()
|
||||
export class LoginService implements OnInit{
|
||||
|
||||
private _authenticating:LoginCredential;
|
||||
constructor(private _networkService: NetworkService){
|
||||
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this._networkService.socketIO.on(MessageTypes.Server.Login.Authenticated, (message:Message<User>) => {
|
||||
if(message.result.name == this._authenticating.username || message.result.email == this._authenticating.username){
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public login(user:LoginCredential):Promise<User> {
|
||||
this._networkService.socketIO.emit(MessageTypes.Client.Login.Authenticate, user);
|
||||
this._authenticating = Utils.clone(user);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
20
frontend/app/model/network.service.ts
Normal file
20
frontend/app/model/network.service.ts
Normal file
@ -0,0 +1,20 @@
|
||||
///<reference path="../../../typings/tsd.d.ts"/>
|
||||
|
||||
import * as io from 'socket.io-client';
|
||||
import {Injectable} from 'angular2/core';
|
||||
import {OnInit} from "angular2/core";
|
||||
|
||||
@Injectable()
|
||||
export class NetworkService implements OnInit{
|
||||
|
||||
private _socketIO: SocketIOClient.Socket;
|
||||
|
||||
ngOnInit() {
|
||||
this._socketIO = io();
|
||||
}
|
||||
|
||||
|
||||
get socketIO():SocketIOClient.Socket {
|
||||
return this._socketIO;
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
"reflect-metadata": "~0.1.2",
|
||||
"rxjs": "^5.0.0-beta.2",
|
||||
"socket.io": "^1.4.5",
|
||||
"socket.io-client": "^1.4.5",
|
||||
"ts-loader": "^0.8.1",
|
||||
"webpack": "^1.12.14",
|
||||
"zone.js": "^0.5.15"
|
||||
|
6
tsd.json
6
tsd.json
@ -19,6 +19,12 @@
|
||||
},
|
||||
"serve-static/serve-static.d.ts": {
|
||||
"commit": "0d622d857f97d44ea7dcad2b3edec1f23c48fe9e"
|
||||
},
|
||||
"socket.io/socket.io.d.ts": {
|
||||
"commit": "d22516f9f089de107d7e7d5938566377370631f6"
|
||||
},
|
||||
"socket.io-client/socket.io-client.d.ts": {
|
||||
"commit": "d22516f9f089de107d7e7d5938566377370631f6"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user