Save token to cookie when logging in
This commit is contained in:
parent
59dcdd1694
commit
cbb4cd07c7
|
@ -0,0 +1,29 @@
|
|||
// https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie/4825695#4825695
|
||||
|
||||
export function setCookie (name, value, days) {
|
||||
let expires;
|
||||
if (days) {
|
||||
const date = new Date();
|
||||
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
||||
expires = '; expires=' + date.toGMTString();
|
||||
}
|
||||
else {
|
||||
expires = '';
|
||||
}
|
||||
document.cookie = name + '=' + value + expires + '; path=/';
|
||||
}
|
||||
|
||||
export function getCookie(c_name) {
|
||||
if (document.cookie.length > 0) {
|
||||
let c_start = document.cookie.indexOf(c_name + '=');
|
||||
if (c_start != -1) {
|
||||
c_start = c_start + c_name.length + 1;
|
||||
let c_end = document.cookie.indexOf(';', c_start);
|
||||
if (c_end == -1) {
|
||||
c_end = document.cookie.length;
|
||||
}
|
||||
return unescape(document.cookie.substring(c_start, c_end));
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
import { setCookie } from "../StackOverflow/cookie";
|
||||
|
||||
export function request (data = {}, success = () => {}, error = () => {}/* , fail = () => {} */) {
|
||||
return fetch('./api/', {
|
||||
method: 'POST', // or 'PUT'
|
||||
|
@ -14,4 +16,8 @@ export function request (data = {}, success = () => {}, error = () => {}/* , fai
|
|||
}
|
||||
return success(response.data);
|
||||
});
|
||||
}
|
||||
|
||||
export function saveToken(token) {
|
||||
setCookie('token', token, 30);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import { request } from "./helpers";
|
||||
import { request, saveToken } from "./helpers";
|
||||
import { addMessage } from "../utilities";
|
||||
import { setupLogoutButton } from "./setupListeners";
|
||||
import { renderAccountSettings } from "./render";
|
||||
|
@ -25,6 +25,7 @@ export function logIn() {
|
|||
password,
|
||||
}, successData => {
|
||||
console.log(successData);
|
||||
saveToken(successData.token);
|
||||
}, errorData => {
|
||||
errorHTML += errorData;
|
||||
}).then(() => {
|
||||
|
@ -84,9 +85,10 @@ export function createAccount() {
|
|||
allowEmail,
|
||||
},
|
||||
}, responseData => {
|
||||
return responseData;
|
||||
saveToken(responseData.token);
|
||||
return responseData;
|
||||
}, errorData => {
|
||||
errorHTML += `<p class="bold red">${errorData}</p>`;
|
||||
errorHTML += `<p class="bold red">${errorData}</p>`;
|
||||
}).then(responseData => {
|
||||
console.log(responseData);
|
||||
createAccountErrorMessages.innerHTML = errorHTML;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { addWord } from './wordManagement';
|
||||
import { getCookie } from './StackOverflow/cookie';
|
||||
|
||||
export function getNextId() {
|
||||
const lastId = window.currentDictionary.words.reduce((highestId, word) => {
|
||||
|
@ -147,3 +148,7 @@ export function addMessage(messageText, time = 5000) {
|
|||
|
||||
setTimeout(closeMessage, time);
|
||||
}
|
||||
|
||||
export function hasToken() {
|
||||
return getCookie('token') !== '';
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue