1 let router
= require("express").Router();
2 const validator
= require('../public/javascripts/utils/validation');
3 const UserModel
= require('../models/user');
4 const UserEntity
= require('../entities/user');
5 const maild
= require('../utils/mailer');
6 const TokenGen
= require("../utils/tokenGenerator");
7 const access
= require("../utils/access");
8 const params
= require("../config/parameters");
11 function sendLoginToken(subject
, to
, res
)
13 // Set login token and send welcome(back) email with auth link
14 let token
= TokenGen
.generate(params
.token
.length
);
15 UserEntity
.setLoginToken(token
, to
._id
, to
.ip
, (err
,ret
) => {
16 access
.checkRequest(res
, err
, ret
, "Cannot set login token", () => {
18 from: params
.mail
.from,
21 body: "Hello " + to
.initials
+ "!\n" +
22 "Access your account here: " +
23 params
.siteURL
+ "/authenticate?token=" + token
+ "\\n" +
24 "Token will expire in " + params
.token
.expire
/(1000*60) + " minutes."
32 router
.get('/register', access
.ajax
, access
.unlogged
, (req
,res
) => {
33 let email
= decodeURIComponent(req
.query
.email
);
34 let forename
= decodeURIComponent(req
.query
.forename
);
35 let name
= decodeURIComponent(req
.query
.name
);
41 let error
= validator(newUser
, "User");
43 return res
.json({errmsg:error
});
44 if (!UserModel
.whitelistCheck(newUser
.email
))
45 return res
.json({errmsg: "Email not in whitelist"});
46 UserEntity
.getByEmail(newUser
.email
, (err
,user0
) => {
47 access
.checkRequest(res
, err
, !user0
?["ok"]:{}, "An account exists with this email", () => {
48 UserModel
.create(newUser
, (err
,user
) => {
49 access
.checkRequest(res
, err
, user
, "Registration failed", () => {
51 sendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
59 router
.get('/sendtoken', access
.ajax
, access
.unlogged
, (req
,res
) => {
60 let email
= decodeURIComponent(req
.query
.email
);
61 let error
= validator({email:email
}, "User");
63 return res
.json({errmsg:error
});
64 UserEntity
.getByEmail(email
, (err
,user
) => {
65 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
67 sendLoginToken("Token for " + params
.siteURL
, user
, res
);
72 // Authentication process, optionally with email changing:
73 router
.get('/authenticate', access
.unlogged
, (req
,res
) => {
74 let loginToken
= req
.query
.token
;
75 let error
= validator({token:loginToken
}, "User");
77 return res
.json({errmsg:error
});
78 UserEntity
.getByLoginToken(loginToken
, (err
,user
) => {
79 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
80 if (user
.loginToken
.ip
!= req
.ip
)
81 return res
.json({errmsg: "IP address mismatch"});
83 let tsNow
= now
.getTime();
84 // If token older than params.tokenExpire, do nothing
85 if (user
.loginToken
.timestamp
+ params
.token
.expire
< tsNow
)
86 return res
.json({errmsg: "Token expired"});
87 // Generate and update session token + destroy login token
88 let token
= TokenGen
.generate(params
.token
.length
);
89 UserEntity
.setSessionToken(token
, user
._id
, (err
,ret
) => {
90 access
.checkRequest(res
, err
, ret
, "Authentication failed", () => {
91 // Set cookies and redirect to user main control panel
92 res
.cookie("token", token
, {
94 maxAge: params
.cookieExpire
,
96 res
.cookie("initials", user
.initials
, {
98 maxAge: params
.cookieExpire
,
100 res
.redirect("/" + user
.initials
);
107 router
.get('/logout', access
.logged
, (req
,res
) => {
108 UserModel
.logout(req
.user
._id
, req
.cookies
.token
, (err
,ret
) => {
109 access
.checkRequest(res
, err
, ret
, "Logout failed", () => {
110 res
.clearCookie("initials");
111 res
.clearCookie("token");
117 module
.exports
= router
;