Skip to content
Snippets Groups Projects
Commit 664b0d27 authored by Léo Banno-Cloutier's avatar Léo Banno-Cloutier
Browse files

jams-react-client: refactor auth.tsx

Change-Id: Ia20810a6e232fe70b0feefb06643884ce030f4e5
parent f8082a35
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,16 @@ import { ...@@ -12,6 +12,16 @@ import {
} from "globalUrls"; } from "globalUrls";
class Auth { class Auth {
authenticated: boolean;
admin: boolean;
installed: boolean;
uri: string;
username: string;
localVersion: string;
remoteVersion: string;
activated: boolean;
updateAvailable: boolean;
constructor() { constructor() {
this.authenticated = false; this.authenticated = false;
this.admin = false; this.admin = false;
...@@ -24,26 +34,26 @@ class Auth { ...@@ -24,26 +34,26 @@ class Auth {
this.updateAvailable = false; this.updateAvailable = false;
} }
setJWT(access_token) { setJWT(accessToken: string) {
window.localStorage.removeItem("access_token"); window.localStorage.removeItem("access_token");
window.localStorage.setItem("access_token", access_token); window.localStorage.setItem("access_token", accessToken);
} }
setScope(scope) { setScope(scope: boolean) {
window.localStorage.removeItem("scope"); window.localStorage.removeItem("scope");
window.localStorage.setItem("scope", scope); window.localStorage.setItem("scope", scope.toString());
} }
setLocalDirectory(localDirectory) { setLocalDirectory(localDirectory: boolean) {
window.localStorage.removeItem("localDirectory"); window.localStorage.removeItem("localDirectory");
window.localStorage.setItem("localDirectory", localDirectory); window.localStorage.setItem("localDirectory", localDirectory.toString());
} }
deleteJWT() { deleteJWT() {
window.localStorage.removeItem("access_token"); window.localStorage.removeItem("access_token");
} }
setUsername(username) { setUsername(username: string) {
window.localStorage.removeItem("username"); window.localStorage.removeItem("username");
window.localStorage.setItem("username", username); window.localStorage.setItem("username", username);
} }
...@@ -52,27 +62,27 @@ class Auth { ...@@ -52,27 +62,27 @@ class Auth {
window.localStorage.removeItem("username"); window.localStorage.removeItem("username");
} }
login(jsonData, cb) { login(jsonData: { username: string; password: string }, cb: () => void) {
this.deleteJWT(); this.deleteJWT();
axios(configApiCall(api_path_post_auth_login, "POST", jsonData, null)) axios(configApiCall(api_path_post_auth_login, "POST", jsonData, null))
.then((response) => { .then((response) => {
if (response.status === 200) { if (response.status === 200) {
this.setJWT(response.data["access_token"]); this.setJWT(response.data["access_token"]);
this.setScope(response.data["scope"] === "ADMIN" ? true : false); this.setScope(response.data["scope"] === "ADMIN");
this.setUsername(jsonData.username); this.setUsername(jsonData.username);
this.authenticated = true; this.authenticated = true;
} }
cb(); cb();
}) })
.catch((error) => { .catch(() => {
this.setJWT(""); this.setJWT("");
this.setUsername(""); this.setUsername("");
this.authenticated = false; this.authenticated = false;
cb(error); cb();
}); });
} }
logout(cb) { logout(cb: () => void) {
this.deleteJWT(); this.deleteJWT();
this.authenticated = false; this.authenticated = false;
this.deleteUserhame(); this.deleteUserhame();
...@@ -84,12 +94,10 @@ class Auth { ...@@ -84,12 +94,10 @@ class Auth {
} }
isLocalDirectory() { isLocalDirectory() {
return window.localStorage.getItem("localDirectory") === "true" return window.localStorage.getItem("localDirectory") === "true";
? true
: false;
} }
checkDirectoryType(cb) { checkDirectoryType(cb: () => void) {
axios(configApiCall(api_path_get_directories, "GET", null, null)) axios(configApiCall(api_path_get_directories, "GET", null, null))
.then((response) => { .then((response) => {
if (response.data.length === 1) { if (response.data.length === 1) {
...@@ -110,7 +118,7 @@ class Auth { ...@@ -110,7 +118,7 @@ class Auth {
}); });
} }
checkAdminAccountStatus(cb) { checkAdminAccountStatus(cb: () => void) {
axios(configApiCall(api_path_post_install_admin, "GET", null, null)) axios(configApiCall(api_path_post_install_admin, "GET", null, null))
.then((response) => { .then((response) => {
if (response["headers"]["showlogin"] === "true") { if (response["headers"]["showlogin"] === "true") {
...@@ -130,7 +138,7 @@ class Auth { ...@@ -130,7 +138,7 @@ class Auth {
}); });
} }
checkServerInstalled(cb) { checkServerInstalled(cb: () => void) {
axios(configApiCall(api_path_get_server_status, "GET", null, null)) axios(configApiCall(api_path_get_server_status, "GET", null, null))
.then((response) => { .then((response) => {
if (response.data["installed"] === "true") { if (response.data["installed"] === "true") {
...@@ -149,7 +157,7 @@ class Auth { ...@@ -149,7 +157,7 @@ class Auth {
}); });
} }
checkLastKnownStep(cb) { checkLastKnownStep(cb: () => void) {
if (this.installed) { if (this.installed) {
this.authenticated = true; this.authenticated = true;
cb(); cb();
...@@ -171,7 +179,7 @@ class Auth { ...@@ -171,7 +179,7 @@ class Auth {
} }
} }
checkForUpdates(cb) { checkForUpdates(cb: () => void) {
if (this.installed && this.authenticated) { if (this.installed && this.authenticated) {
axios(configApiCall(api_path_get_subscription_status, "GET", null, null)) axios(configApiCall(api_path_get_subscription_status, "GET", null, null))
.then((response) => { .then((response) => {
...@@ -199,7 +207,7 @@ class Auth { ...@@ -199,7 +207,7 @@ class Auth {
} }
} }
getUpdates(cb) { getUpdates(cb: () => void) {
if (this.installed && this.authenticated) { if (this.installed && this.authenticated) {
axios(configApiCall(api_path_get_needs_update, "GET", null, null)).then( axios(configApiCall(api_path_get_needs_update, "GET", null, null)).then(
(response) => { (response) => {
...@@ -252,7 +260,7 @@ class Auth { ...@@ -252,7 +260,7 @@ class Auth {
} }
hasAdminScope() { hasAdminScope() {
return window.localStorage.getItem("scope") === "true" ? true : false; return window.localStorage.getItem("scope") === "true";
} }
getUsername() { getUsername() {
......
...@@ -59,10 +59,7 @@ export default function Devices({ username }) { ...@@ -59,10 +59,7 @@ export default function Devices({ username }) {
if (response.data.length === 0) { if (response.data.length === 0) {
setDevices([]); setDevices([]);
} else { } else {
var resultSet = JSON.parse( setDevices(response.data);
response.data.replace(/\s+/g, " ").trim()
);
setDevices(resultSet);
} }
}) })
.catch((error) => { .catch((error) => {
......
...@@ -62,7 +62,7 @@ const api_path_get_admin_user_groups = "/api/admin/user/groups/"; ...@@ -62,7 +62,7 @@ const api_path_get_admin_user_groups = "/api/admin/user/groups/";
const api_path_get_image = "/api/image/filehandler"; const api_path_get_image = "/api/image/filehandler";
const api_path_post_image = "/api/image/filehandler"; const api_path_post_image = "/api/image/filehandler";
module.exports = { export {
uri, uri,
current_uri, current_uri,
backend_address, backend_address,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment