mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 08:39:43 +00:00
Add check for authorization header
This commit is contained in:
parent
6790b94b80
commit
ac50b755d1
1 changed files with 32 additions and 3 deletions
|
|
@ -1,14 +1,41 @@
|
||||||
|
// Set the auth header if necessary
|
||||||
|
function authHeaders(xhr) {
|
||||||
|
if (window.localStorage.getItem("authtoken") !== null) {
|
||||||
|
xhr.setRequestHeader("Authorization", "Basic " + authToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redirect if not authorized
|
||||||
|
function redirectIfNotAuthorized() {
|
||||||
|
$.ajax({
|
||||||
|
url: jsconfig.baseurl + "/api/version",
|
||||||
|
beforeSend: authHeaders,
|
||||||
|
statusCode: {
|
||||||
|
401: function() {
|
||||||
|
window.location.replace(baseurl + "/app/login.html");
|
||||||
|
},
|
||||||
|
403: function() {
|
||||||
|
window.location.replace(baseurl + "/app/login.html");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Call the redirect function
|
||||||
|
redirectIfNotAuthorized();
|
||||||
|
|
||||||
function celsiusToFahrenheit(degree) {
|
function celsiusToFahrenheit(degree) {
|
||||||
return degree * 1.8 + 32;
|
return degree * 1.8 + 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
function fahrenheitToCelsius(degree) {
|
function fahrenheitToCelsius(degree) {
|
||||||
return (degree - 32) * 5 / 9;
|
return (degree - 32) * 5 / 9;
|
||||||
}
|
};
|
||||||
|
|
||||||
function renderThermostats() {
|
function renderThermostats() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: jsconfig.baseurl + "/api/status/"
|
url: jsconfig.baseurl + "/api/status/",
|
||||||
|
beforeSend: authHeaders
|
||||||
}).then(function(data) {
|
}).then(function(data) {
|
||||||
$("#thermostats").empty();
|
$("#thermostats").empty();
|
||||||
for (var key in data) {
|
for (var key in data) {
|
||||||
|
|
@ -44,7 +71,8 @@ function renderThermostats() {
|
||||||
|
|
||||||
// Display sensor config
|
// Display sensor config
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: jsconfig.baseurl + "/api/config/sensors/" + data[key].alias
|
url: jsconfig.baseurl + "/api/config/sensors/" + data[key].alias,
|
||||||
|
beforeSend: authHeaders
|
||||||
}).then(function(configData){
|
}).then(function(configData){
|
||||||
if (jsconfig.fahrenheit) {
|
if (jsconfig.fahrenheit) {
|
||||||
var degUnit = "°F";
|
var degUnit = "°F";
|
||||||
|
|
@ -79,6 +107,7 @@ function renderThermostats() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: jsconfig.baseurl + "/api/config/sensors",
|
url: jsconfig.baseurl + "/api/config/sensors",
|
||||||
|
beforeSend: authHeaders,
|
||||||
data: JSON.stringify([{
|
data: JSON.stringify([{
|
||||||
"id": configData.id,
|
"id": configData.id,
|
||||||
"alias": configData.alias,
|
"alias": configData.alias,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue