mirror of
https://github.com/Alamantus/Lexiconga.git
synced 2025-05-07 18:52:04 +02:00
Save token to cookie when logging in
This commit is contained in:
parent
59dcdd1694
commit
cbb4cd07c7
4 changed files with 45 additions and 3 deletions
29
src/js/StackOverflow/cookie.js
Normal file
29
src/js/StackOverflow/cookie.js
Normal file
|
@ -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 = () => {} */) {
|
export function request (data = {}, success = () => {}, error = () => {}/* , fail = () => {} */) {
|
||||||
return fetch('./api/', {
|
return fetch('./api/', {
|
||||||
method: 'POST', // or 'PUT'
|
method: 'POST', // or 'PUT'
|
||||||
|
@ -15,3 +17,7 @@ export function request (data = {}, success = () => {}, error = () => {}/* , fai
|
||||||
return success(response.data);
|
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 { addMessage } from "../utilities";
|
||||||
import { setupLogoutButton } from "./setupListeners";
|
import { setupLogoutButton } from "./setupListeners";
|
||||||
import { renderAccountSettings } from "./render";
|
import { renderAccountSettings } from "./render";
|
||||||
|
@ -25,6 +25,7 @@ export function logIn() {
|
||||||
password,
|
password,
|
||||||
}, successData => {
|
}, successData => {
|
||||||
console.log(successData);
|
console.log(successData);
|
||||||
|
saveToken(successData.token);
|
||||||
}, errorData => {
|
}, errorData => {
|
||||||
errorHTML += errorData;
|
errorHTML += errorData;
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
|
@ -84,6 +85,7 @@ export function createAccount() {
|
||||||
allowEmail,
|
allowEmail,
|
||||||
},
|
},
|
||||||
}, responseData => {
|
}, responseData => {
|
||||||
|
saveToken(responseData.token);
|
||||||
return responseData;
|
return responseData;
|
||||||
}, errorData => {
|
}, errorData => {
|
||||||
errorHTML += `<p class="bold red">${errorData}</p>`;
|
errorHTML += `<p class="bold red">${errorData}</p>`;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { addWord } from './wordManagement';
|
import { addWord } from './wordManagement';
|
||||||
|
import { getCookie } from './StackOverflow/cookie';
|
||||||
|
|
||||||
export function getNextId() {
|
export function getNextId() {
|
||||||
const lastId = window.currentDictionary.words.reduce((highestId, word) => {
|
const lastId = window.currentDictionary.words.reduce((highestId, word) => {
|
||||||
|
@ -147,3 +148,7 @@ export function addMessage(messageText, time = 5000) {
|
||||||
|
|
||||||
setTimeout(closeMessage, time);
|
setTimeout(closeMessage, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function hasToken() {
|
||||||
|
return getCookie('token') !== '';
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue