2016-03-18 22:36:58 +02:00
|
|
|
|
|
|
|
import {UserManager} from "../model/UserManager";
|
|
|
|
import {NextFunction, Request, Response} from "express";
|
2016-03-19 10:58:27 +02:00
|
|
|
import {BaseMWs} from "./BaseMWs";
|
|
|
|
import {Error, ErrorCodes} from "../../common/entities/Error";
|
2016-03-18 22:36:58 +02:00
|
|
|
|
2016-03-19 10:58:27 +02:00
|
|
|
export class UserMWs extends BaseMWs{
|
2016-03-18 22:36:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
public static authenticate(req:Request, res:Response, next:NextFunction){
|
|
|
|
if (typeof req.session.user === 'undefined') {
|
2016-03-19 10:58:27 +02:00
|
|
|
return super.renderError(res,new Error(ErrorCodes.NOT_AUTHENTICATED));
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static inverseAuthenticate(req:Request, res:Response, next:NextFunction){
|
|
|
|
if (typeof req.session.user !== 'undefined') {
|
2016-03-19 10:58:27 +02:00
|
|
|
return super.renderError(res,new Error(ErrorCodes.ALREADY_AUTHENTICATED));
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
|
|
|
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){
|
2016-03-19 10:58:27 +02:00
|
|
|
super.renderMessage(res,req.session.user);
|
2016-03-18 22:36:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|