Files
edx-platform/lms/static/js/student_account/utils.js
Régis Behmo 9ab904fd19 fix: "Uncaught TypeError: n is null" during login to LMS (#29192)
When signing in from the LMS, the authentication request succeeds but
the user was never redirected to the dashboard. This is because of a
js error:

    Uncaught TypeError: userCookie is null
        userFromEdxUserCookie
        http://maple.openedx.overhang.io:8000/static/js/student_account/utils.js:32

The error comes from the fact that the util.js code ignores the
EDXAPP_EDXMKTG_USER_INFO_COOKIE_NAME setting name. Instead the cookie
name is abritrarily prefixed by "stage-" or "prod-" depending on the
hostname. This seems to be primarily motivated by the definition of
edX.org staging/prod environment hostnames :-(

To resolve this issue, we decided that the actual cookie name was not so
important. Instead, the js code needs to not crash even when the cookie
is absent.

This issue was first reported here:
https://github.com/edx/edx-platform/pull/28170#issuecomment-890449885

Close https://github.com/openedx/build-test-release-wg/issues/104
2021-11-09 11:12:24 -05:00

44 lines
1.7 KiB
JavaScript

(function(define) {
'use strict';
define(['jquery'], function($) {
var edxUserCookieUtils = {
getHostname: function() {
return window.location.hostname;
},
userFromEdxUserCookie: function() {
var hostname = this.getHostname();
var isLocalhost = hostname.indexOf('localhost') >= 0;
var isSandbox = hostname.indexOf('sandbox') >=0;
var isStage = hostname.indexOf('stage') >= 0;
var isEdge = hostname.indexOf('edge') >= 0;
var cookie, edxUserCookie, prefix, user, userCookie;
if (isLocalhost || isSandbox) {
// localhost doesn't have prefixes
edxUserCookie = 'edx-user-info';
} else {
// does not take sandboxes into account
prefix = isStage ? 'stage' : 'prod';
prefix = isEdge ? 'edge' : prefix;
edxUserCookie = prefix + '-edx-user-info';
}
cookie = document.cookie.match('(^|;)\\s*' + edxUserCookie + '\\s*=\\s*([^;]+)');
userCookie = cookie ? cookie.pop() : $.cookie(edxUserCookie);
if (!userCookie) {
return {};
}
// returns the user object from cookie. Replaces '054' with ',' and removes '\'
user = userCookie.replace(/\\/g, '').replace(/054/g, ',');
user = user.substring(1, user.length - 1);
return JSON.parse(user);
}
};
return edxUserCookieUtils;
});
}).call(this, define || RequireJS.define);