Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / web / middlewares / track-login-for-provider.js
blob4d4befb3a5ce5353ef80e564d0e4413b20313223
1 'use strict';
3 var env = require('gitter-web-env');
4 var stats = env.stats;
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',
40 button: query.source
41 });
43 next();