aboutsummaryrefslogtreecommitdiff
path: root/public/js/auth.js
blob: 901329823456b217a71cfb9f474ca7dc7b563e42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var page = {};

page.do = function(dest){

	var user = document.getElementById('user').value;
	var pass = document.getElementById('pass').value;

	if(user === undefined || user === null || user === '')
		return swal('Error', 'You need to specify a username', 'error');
	if(pass === undefined || pass === null || pass === '')
		return swal('Error', 'You need to specify a username', 'error');

	axios.post('/api/' + dest, {
		username: user,
		password: pass
	})
  	.then(function (response) {

    	if(response.data.success === false)
    		return swal('Error', response.data.description, 'error');
    	
    	localStorage.token = response.data.token;
    	window.location('/panel');

  	})
  	.catch(function (error) {
  		return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error');
    	console.log(error);
  	});
}

page.verify = function(){
	page.token = localStorage.token;
	if(page.token === undefined) return;

	axios.post('/api/tokens/verify', {
		token: page.token
	})
  	.then(function (response) {

    	if(response.data.success === false)
    		return swal('Error', response.data.description, 'error');
    	
    	window.location('/panel');

  	})
  	.catch(function (error) {
  		return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error');
    	console.log(error);
  	});

}

window.onload = function () {
	page.verify();
}