mirror of
https://github.com/bpatrik/pigallery2.git
synced 2024-11-28 08:58:49 +02:00
middleware architecture created
This commit is contained in:
parent
51a3681708
commit
87fb346c48
10
backend/middlewares/ExtendedRequest.ts
Normal file
10
backend/middlewares/ExtendedRequest.ts
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
declare namespace Express {
|
||||
|
||||
export interface Request {
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
user?;
|
||||
}
|
||||
}
|
61
backend/middlewares/UserMWs.ts
Normal file
61
backend/middlewares/UserMWs.ts
Normal file
@ -0,0 +1,61 @@
|
||||
|
||||
import {UserManager} from "../model/UserManager";
|
||||
import {NextFunction, Request, Response} from "express";
|
||||
|
||||
export class UserMWs {
|
||||
|
||||
|
||||
public static authenticate(req:Request, res:Response, next:NextFunction){
|
||||
if (typeof req.session.user === 'undefined') {
|
||||
return res.redirect('/');
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
||||
public static inverseAuthenticate(req:Request, res:Response, next:NextFunction){
|
||||
if (typeof req.session.user !== 'undefined') {
|
||||
return res.redirect('/');
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
||||
public static login(req:Request, res:Response, next:NextFunction){
|
||||
//not enough parameter
|
||||
/* if ((typeof req.body === 'undefined') || (typeof req.body.email === 'undefined') ||
|
||||
(typeof req.body.password === 'undefined')) {
|
||||
return next();
|
||||
}*/
|
||||
|
||||
//lets find the user
|
||||
UserManager.findOne({
|
||||
// email: req.body.email
|
||||
}, function (err, result) {
|
||||
if ((err) || (!result)) {
|
||||
// res.tpl.error.push('Your email address is not registered!');
|
||||
console.log(err);
|
||||
return next();
|
||||
}
|
||||
|
||||
/* //check password
|
||||
if (result.password !== req.body.password) {
|
||||
// res.tpl.error.push('Wrong password!');
|
||||
return next();
|
||||
}
|
||||
*/
|
||||
//login is ok, save id to session
|
||||
req.session.user = result;
|
||||
|
||||
//redirect to / so the app can decide where to go next
|
||||
// return res.redirect('/');
|
||||
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static renderUser(req:Request, res:Response, next:NextFunction){
|
||||
res.json(req.session.user);
|
||||
}
|
||||
|
||||
|
||||
}
|
10
backend/model/UserManager.ts
Normal file
10
backend/model/UserManager.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import {User} from "../../common/entities/User";
|
||||
export class UserManager {
|
||||
|
||||
private static DummyUser = new User("TestUser","test@test.hu","122345");
|
||||
|
||||
public static findOne(filter,cb:(error: any,result:User) => void){
|
||||
return cb(null, UserManager.DummyUser);
|
||||
}
|
||||
|
||||
}
|
14
backend/routes/PublicRouter.ts
Normal file
14
backend/routes/PublicRouter.ts
Normal file
@ -0,0 +1,14 @@
|
||||
///<reference path="../../typings/main.d.ts"/>
|
||||
|
||||
|
||||
import * as _express from 'express';
|
||||
import * as path from 'path';
|
||||
|
||||
export class PublicRouter{
|
||||
constructor(private app){
|
||||
this.app.use(_express.static(path.resolve(__dirname, './../../frontend')));
|
||||
this.app.use('/node_modules',_express.static(path.resolve(__dirname, './../../node_modules')));
|
||||
|
||||
}
|
||||
|
||||
}
|
19
backend/routes/UserRouter.ts
Normal file
19
backend/routes/UserRouter.ts
Normal file
@ -0,0 +1,19 @@
|
||||
///<reference path="../../typings/main.d.ts"/>
|
||||
|
||||
|
||||
|
||||
import {UserMWs} from "../middlewares/UserMWs";
|
||||
export class UserRouter{
|
||||
constructor(private app){
|
||||
this.initLogin();
|
||||
}
|
||||
|
||||
private initLogin() {
|
||||
this.app.get("/api/login",
|
||||
UserMWs.inverseAuthenticate,
|
||||
UserMWs.login,
|
||||
UserMWs.renderUser
|
||||
);
|
||||
};
|
||||
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
///<reference path="../typings/main.d.ts"/>
|
||||
|
||||
import * as _express from 'express';
|
||||
import * as _session from 'express-session';
|
||||
import * as _debug from 'debug';
|
||||
import * as _http from 'http';
|
||||
import * as path from 'path';
|
||||
import {NetworkManager} from "./NetworkManager";
|
||||
import {PublicRouter} from "./routes/PublicRouter";
|
||||
import {UserRouter} from "./routes/UserRouter";
|
||||
|
||||
|
||||
export class Server {
|
||||
@ -24,14 +25,23 @@ export class Server {
|
||||
this.app.use(_morgan('dev'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Session above all
|
||||
*/
|
||||
this.app.use(_session({
|
||||
|
||||
this.app.use(_express.static(path.resolve(__dirname, './../frontend')));
|
||||
this.app.use('/node_modules',_express.static(path.resolve(__dirname, './../node_modules')));
|
||||
secret: 'keyboard cat',
|
||||
cookie: {
|
||||
maxAge: 60000
|
||||
},
|
||||
resave: true,
|
||||
saveUninitialized: false
|
||||
}));
|
||||
|
||||
|
||||
new PublicRouter(this.app);
|
||||
new UserRouter(this.app);
|
||||
|
||||
var renderIndex = (req: _express.Request, res: _express.Response) => {
|
||||
res.sendFile(path.resolve(__dirname, './../frontend/index.html'));
|
||||
};
|
||||
this.app.get('/*', renderIndex);
|
||||
|
||||
|
||||
|
||||
@ -47,7 +57,6 @@ export class Server {
|
||||
this.server.on('error', this.onError);
|
||||
this.server.on('listening', this.onListening);
|
||||
|
||||
new NetworkManager(this.server);
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "PiGallery2",
|
||||
"version": "0.0.0",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"description": "This is a photo gallery optimised for running low resource servers (especially on raspberry pi)",
|
||||
"author": "Braun Patrik",
|
||||
@ -27,6 +27,7 @@
|
||||
"es6-shim": "^0.33.13",
|
||||
"es7-reflect-metadata": "^1.6.0",
|
||||
"express": "^4.13.4",
|
||||
"express-session": "^1.13.0",
|
||||
"karma-jasmine": "^0.3.8",
|
||||
"morgan": "^1.7.0",
|
||||
"protractor": "^3.2.0",
|
||||
|
@ -2,13 +2,14 @@
|
||||
"name": "PiGallery2",
|
||||
"version": false,
|
||||
"ambientDependencies": {
|
||||
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"debug": "github:DefinitelyTyped/DefinitelyTyped/debug/debug.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"express-session": "registry:dt/express-session#0.0.0+20160317120654",
|
||||
"jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#d22516f9f089de107d7e7d5938566377370631f6",
|
||||
"mime": "github:DefinitelyTyped/DefinitelyTyped/mime/mime.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"serve-static": "github:DefinitelyTyped/DefinitelyTyped/serve-static/serve-static.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"socket.io": "github:DefinitelyTyped/DefinitelyTyped/socket.io/socket.io.d.ts#d22516f9f089de107d7e7d5938566377370631f6",
|
||||
"socket.io-client": "github:DefinitelyTyped/DefinitelyTyped/socket.io-client/socket.io-client.d.ts#d22516f9f089de107d7e7d5938566377370631f6",
|
||||
"jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#d22516f9f089de107d7e7d5938566377370631f6"
|
||||
"socket.io-client": "github:DefinitelyTyped/DefinitelyTyped/socket.io-client/socket.io-client.d.ts#d22516f9f089de107d7e7d5938566377370631f6"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user