This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
adlin/token-validator/htdocs/js/adlin-main.js

206 lines
4.9 KiB
JavaScript

angular.module("AdLinApp", ["ngRoute", "ngResource", "ngSanitize"])
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/auth", {
controller: "AuthController",
templateUrl: "views/auth.html"
})
.when("/domains", {
controller: "DomainsController",
templateUrl: "views/domains.html"
})
.when("/tunnels", {
controller: "TunnelsController",
templateUrl: "views/tunnels.html"
})
.when("/", {
templateUrl: "views/home.html"
});
$locationProvider.html5Mode(true);
});
angular.module("AdLinApp")
.factory("Student", function($resource) {
return $resource("/api/students/:studentId", { studentId: '@id' }, {
'update': {method: 'PUT'},
})
})
.factory("Progression", function($resource) {
return $resource("/api/progress")
})
.factory("Challenge", function($resource) {
return $resource("/challenge/:challengeId", { challengeId: '@id' })
});
angular.module("AdLinApp")
.run(function($rootScope, $interval, $http) {
$rootScope.checkLoginState = function() {
if (sessionStorage.token === undefined) {
$rootScope.isLogged = false;
return;
}
var token = sessionStorage.token;
$http({
method: 'GET',
url: "/api/auth",
headers: {
'Authorization': "Bearer " + token
}
}).then(function(response) {
$rootScope.isLogged = response.data;
}, function(response) {
$rootScope.isLogged = false;
});
};
$rootScope.checkLoginState();
$interval($rootScope.checkLoginState, 20000);
$rootScope.disconnectCurrentUser = function() {
sessionStorage.token = undefined;
delete sessionStorage.token;
$rootScope.isLogged = false;
}
})
.controller("AuthController", function($scope, $rootScope, $http, $location) {
$scope.auth = {
"username": "",
"password": "",
};
$scope.logmein = function() {
$scope.pleaseWait = true;
$http({
method: 'POST',
url: "/api/auth",
data: $scope.auth
}).then(function(response) {
sessionStorage.token = response.data.id_session
$scope.pleaseWait = false;
$rootScope.checkLoginState();
$location.url("/");
}, function(response) {
alert(response.data.errmsg);
$scope.pleaseWait = false;
});
}
})
.controller("TunnelsController", function($scope, $http, $interval) {
$scope.updateTunnelInfo = function() {
$http({
method: 'GET',
url: "/api/wginfo",
headers: {
'Authorization': "Bearer " + sessionStorage.token
}
}).then(function(response) {
$scope.wginfo = response.data;
});
};
$scope.updateTunnelInfo();
$scope.updateTunnelsList = function() {
$http({
method: 'GET',
url: "/api/wg/",
headers: {
'Authorization': "Bearer " + sessionStorage.token
}
}).then(function(response) {
$scope.tunnels = response.data;
}, function(response) {
alert(response.data.errmsg);
});
};
$scope.updateTunnelsList();
$interval($scope.updateTunnelsList, 12000);
$scope.newTunnel = function() {
$scope.pleaseWaitNew = true;
$http({
method: 'POST',
url: "/api/wg/",
headers: {
'Authorization': "Bearer " + sessionStorage.token
},
data: {}
}).then(function(response) {
$scope.updateTunnelsList();
$scope.pleaseWaitNew = false;
}, function(response) {
alert(response.data.errmsg);
$scope.pleaseWaitNew = false;
});
}
$scope.dropTunnel = function(tunnel) {
tunnel.pleaseWaitDrop = true;
$http({
method: 'DELETE',
url: "/api/wg/" + tunnel.TokenText,
headers: {
'Authorization': "Bearer " + sessionStorage.token
},
data: {}
}).then(function(response) {
$scope.updateTunnelsList();
tunnel.pleaseWaitDrop = false;
}, function(response) {
alert(response.data.errmsg);
tunnel.pleaseWaitDrop = false;
});
}
})
.controller("DomainsController", function($scope, $http, $interval) {
$scope.updateAssociationD = function() {
$http({
method: 'GET',
url: "/api/adomains/",
headers: {
'Authorization': "Bearer " + sessionStorage.token
},
}).then(function(response) {
$scope.adomains = [];
response.data.forEach(function(domain) {
$http({
method: 'GET',
url: "/api/adomains/" + domain,
headers: {
'Authorization': "Bearer " + sessionStorage.token
},
}).then(function(response) {
response.data.forEach(function(rr) {
$scope.adomains.push(rr);
});
});
}, function(response) {
alert(response.data.errmsg);
});
});
};
$scope.updateAssociationD();
$scope.newAssociationD = function() {
$scope.pleaseWaitNewAssociation = true;
$http({
method: 'POST',
url: "/api/adomains/",
headers: {
'Authorization': "Bearer " + sessionStorage.token
},
}).then(function(response) {
$scope.updateAssociationD();
$scope.pleaseWaitNewAssociation = false;
}, function(response) {
alert(response.data.errmsg);
$scope.pleaseWaitNewAssociation = false;
});
}
})