Skip to content

Commit 7402dae

Browse files
committed
SignIN - Pull API Token on Login
1 parent 082f504 commit 7402dae

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

apps/templates/accounts/login.html

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,50 @@ <h3 class="font-weight-bolder text-info text-gradient">
9898
{% endblock content %}
9999

100100
<!-- Specific JS goes HERE -->
101-
{% block javascripts %}{% endblock javascripts %}
101+
{% block javascripts %}
102+
103+
<script>
104+
105+
// Save aKey in localStorage
106+
const asyncLocalStorage = {
107+
setItem: function (key, value) {
108+
return Promise.resolve().then(function () {
109+
localStorage.setItem(key, value);
110+
});
111+
}
112+
}
113+
114+
// Login hook
115+
document.getElementsByTagName('form')[0].onsubmit = (e) => {
116+
117+
e.preventDefault();
118+
119+
const formData = new FormData(e.target)
120+
const username = formData.get('username');
121+
const password = formData.get('password');
122+
123+
fetch('/login/jwt/',{
124+
method: 'POST',
125+
body: JSON.stringify({
126+
username: username,
127+
password: password
128+
}),
129+
headers: {
130+
'Content-Type': 'application/json'
131+
}
132+
})
133+
.then((response) => response.json())
134+
.then((res) => {
135+
136+
//console.log('Token: ' + res.data);
137+
138+
// store token in localStorage
139+
asyncLocalStorage.setItem('token',res.data).then(result => {e.target.submit()})
140+
141+
})
142+
.catch((err) => {console.log(err)})
143+
}
144+
145+
</script>
146+
147+
{% endblock javascripts %}

0 commit comments

Comments
 (0)