This repository was archived by the owner on Dec 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
81 lines (69 loc) · 1.99 KB
/
app.js
File metadata and controls
81 lines (69 loc) · 1.99 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
var apiURL = (() => {
if (window.location.hostname !== 'backoffice.upframe.co') {
return `http://${window.location.hostname}:3002`
}
return 'https://api.upframe.co'
})()
function handleErrors (response) {
if (!response.ok) {
throw Error(response.statusText)
}
return response
}
function validate () {
let token = window.localStorage.getItem('token')
fetch(`${apiURL}/tokens/validate`, {
method: 'POST',
mode: 'cors',
headers: new window.Headers({
'Authorization': `Bearer ${token}`
})
}).then(handleErrors)
.then(function (response) {
document.getElementById('login').style.display = 'none'
}).catch(function (error) {
console.log(error)
})
}
function submit (event) {
event.preventDefault()
fetch(`${apiURL}/tokens/get`, {
method: 'POST',
mode: 'cors',
body: new window.FormData(event.currentTarget)
}).then(handleErrors)
.then(function (res) { return res.json() })
.then(function (response) {
window.localStorage.setItem('token', response['Content'])
window.location.reload()
}).catch(function (error) {
console.log(error)
})
}
document.addEventListener('DOMContentLoaded', () => {
document.querySelector('#login form').addEventListener('submit', submit)
validate()
document.getElementById('logout').addEventListener('click', event => {
event.preventDefault()
window.localStorage.clear()
window.location.reload()
})
document.getElementById('email').addEventListener('submit', event => {
event.preventDefault()
let token = window.localStorage.getItem('token')
fetch(`${apiURL}/emails`, {
method: 'POST',
mode: 'cors',
body: new window.FormData(event.currentTarget),
headers: new window.Headers({
'Authorization': `Bearer ${token}`
})
}).then(handleErrors)
.then(function (response) {
window.alert('Success')
}).catch(function (error) {
window.alert('Error:' + error)
})
})
})