3 var env = require('gitter-web-env');
6 /** TODO move onto its own method once we find the need for it elsewhere
7 * isRelativeURL() checks if the URL is relative
9 * url String - the url to be check
10 * @return Boolean - the result of the check
12 function isRelativeURL(url) {
13 var relativeUrl = new RegExp('^/[^/]');
14 return relativeUrl.test(url);
17 // Stop random input from being tracked
18 const ACTION_ALLOWLIST = ['signup', 'login'];
19 function validateAction(inputAction) {
20 return ACTION_ALLOWLIST.includes(inputAction);
23 module.exports = function trackLoginForProvider(provider) {
24 return function(req, res, next) {
25 var query = req.query;
27 // adds the source of the action to the session (for tracking how users
28 // 'come in' to the app)
29 req.session.source = query.source;
31 // checks if we have a relative url path and adds it to the session
32 if (query.returnTo && isRelativeURL(query.returnTo)) {
33 req.session.returnTo = query.returnTo;
36 //send data to stats service
37 if (query.action && validateAction(query.action)) {
38 stats.event(query.action + '_clicked', {
39 method: provider + '_oauth',