scratch-www/src/init.js
Ray Schamp ad7be2dad8 Redirect to ban appeal if the user is banned
The default behavior is for the `/session/` request to be redirected to the ban appeal page. Unfortunately we can't detect this 302 response, as the browser transparently handles it and javascript just sees a 200 response with a weird body.  So I've updated scratchr2 to return a special response for banned `/session/` requests.
2015-10-20 16:17:53 -04:00

74 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var api = require('./mixins/api.jsx').api;
var jar = require('./lib/jar');
var translations = require('../locales/translations.json');
require('custom-event-polyfill');
// Session
(function () {
window._session = {};
/**
* Binds the object to private session variable and dispatches a global
* "session" event.
*
* @param {object} Session object
*
* @return {void}
*/
window.updateSession = function (session) {
window._session = session;
var sessionEvent = new CustomEvent('session', session);
window.dispatchEvent(sessionEvent);
};
/**
* Gets a session object from the local proxy method. Calls "updateSession"
* once session has been returned from the proxy.
*
* @return {void}
*/
window.refreshSession = function () {
api({
host: '',
uri: '/session/'
}, function (err, body) {
if (body.banned) {
return window.location = body.redirectUrl;
} else {
window.updateSession(body);
}
});
};
// Fetch session
window.refreshSession();
})();
// L10N
(function () {
/**
* Bind locale code from cookie if available. Uses navigator language API as a fallback.
*
* @return {string}
*/
function updateLocale () {
var obj = jar.get('scratchlanguage');
if (typeof obj === 'undefined') {
obj = window.navigator.userLanguage || window.navigator.language;
}
if (typeof translations[obj] === 'undefined') {
// Fall back on the split
obj = obj.split('-')[0];
}
if (typeof translations[obj] === 'undefined') {
// Language appears to not be supported return `null`
obj = null;
}
return obj;
}
window._locale = updateLocale() || 'en';
window._translations = translations;
})();