diff --git a/src/js/StackOverflow/cookie.js b/src/js/StackOverflow/cookie.js new file mode 100644 index 0000000..f09bbd2 --- /dev/null +++ b/src/js/StackOverflow/cookie.js @@ -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 ''; +} \ No newline at end of file diff --git a/src/js/account/helpers.js b/src/js/account/helpers.js index 6e22e66..40ae175 100644 --- a/src/js/account/helpers.js +++ b/src/js/account/helpers.js @@ -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); } \ No newline at end of file diff --git a/src/js/account/login.js b/src/js/account/login.js index 69885be..47e4e18 100644 --- a/src/js/account/login.js +++ b/src/js/account/login.js @@ -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 += `

${errorData}

`; + errorHTML += `

${errorData}

`; }).then(responseData => { console.log(responseData); createAccountErrorMessages.innerHTML = errorHTML; diff --git a/src/js/utilities.js b/src/js/utilities.js index ba9770e..bc70206 100644 --- a/src/js/utilities.js +++ b/src/js/utilities.js @@ -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') !== ''; +}