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 name
= decodeURIComponent(req
.query
.name
);
39 let error
= validator(newUser
, "User");
41 return res
.json({errmsg:error
});
42 if (!UserModel
.whitelistCheck(newUser
.email
))
43 return res
.json({errmsg: "Email not in whitelist"});
44 UserEntity
.getByEmail(newUser
.email
, (err
,user0
) => {
45 access
.checkRequest(res
, err
, !user0
?["ok"]:{}, "An account exists with this email", () => {
46 UserModel
.create(newUser
, (err
,user
) => {
47 access
.checkRequest(res
, err
, user
, "Registration failed", () => {
49 sendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
57 router
.get('/sendtoken', access
.ajax
, access
.unlogged
, (req
,res
) => {
58 let email
= decodeURIComponent(req
.query
.email
);
59 let error
= validator({email:email
}, "User");
61 return res
.json({errmsg:error
});
62 UserEntity
.getByEmail(email
, (err
,user
) => {
63 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
65 sendLoginToken("Token for " + params
.siteURL
, user
, res
);
70 // Authentication process, optionally with email changing:
71 router
.get('/authenticate', access
.unlogged
, (req
,res
) => {
72 let loginToken
= req
.query
.token
;
73 let error
= validator({token:loginToken
}, "User");
75 return res
.json({errmsg:error
});
76 UserEntity
.getByLoginToken(loginToken
, (err
,user
) => {
77 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
78 if (user
.loginToken
.ip
!= req
.ip
)
79 return res
.json({errmsg: "IP address mismatch"});
81 let tsNow
= now
.getTime();
82 // If token older than params.tokenExpire, do nothing
83 if (user
.loginToken
.timestamp
+ params
.token
.expire
< tsNow
)
84 return res
.json({errmsg: "Token expired"});
85 // Generate and update session token + destroy login token
86 let token
= TokenGen
.generate(params
.token
.length
);
87 UserEntity
.setSessionToken(token
, user
._id
, (err
,ret
) => {
88 access
.checkRequest(res
, err
, ret
, "Authentication failed", () => {
89 // Set cookies and redirect to user main control panel
90 res
.cookie("token", token
, {
92 maxAge: params
.cookieExpire
,
94 res
.cookie("initials", user
.initials
, {
96 maxAge: params
.cookieExpire
,
98 res
.redirect("/" + user
.initials
);
105 router
.get('/logout', access
.logged
, (req
,res
) => {
106 UserModel
.logout(req
.user
._id
, req
.cookies
.token
, (err
,ret
) => {
107 access
.checkRequest(res
, err
, ret
, "Logout failed", () => {
108 res
.clearCookie("initials");
109 res
.clearCookie("token");
115 module
.exports
= router
;