Validate token if one exists; Save account data

This commit is contained in:
Robbie Antenesse 2019-05-23 16:06:16 -06:00
parent f0b146e5fc
commit f8b9503960
4 changed files with 52 additions and 8 deletions

View File

@ -1,9 +1,8 @@
import '../../scss/Account/main.scss';
import { renderLoginForm } from "./render";
import { triggerLoginChanges } from './login';
import { validateToken } from './login';
import {
syncDictionary,
uploadWords,
uploadDetails,
uploadWholeDictionary,
@ -17,8 +16,7 @@ export function showLoginForm() {
}
export function loginWithToken() {
triggerLoginChanges();
syncDictionary();
validateToken();
}
export function syncImportedDictionary() {

View File

@ -3,7 +3,8 @@ import { saveToken } from "./utilities";
import { addMessage } from "../utilities";
import { setupLogoutButton } from "./setupListeners";
import { renderAccountSettings } from "./render";
import { uploadWholeDictionary } from "./sync";
import { uploadWholeDictionary, syncDictionary } from "./sync";
import { setCookie } from "../StackOverflow/cookie";
export function logIn() {
const email = document.getElementById('loginEmail').value.trim(),
@ -26,8 +27,8 @@ export function logIn() {
email,
password,
}, successData => {
console.log(successData);
saveToken(successData.token);
window.account = successData.user;
}, errorData => {
errorHTML += errorData;
}).then(() => {
@ -36,7 +37,7 @@ export function logIn() {
const loginModal = document.getElementById('loginModal');
loginModal.parentElement.removeChild(loginModal);
triggerLoginChanges();
addMessage(`Welcome! You are logged in.`);
addMessage(`Welcome${window.account.publicName !== '' ? ', ' + window.account.publicName : ''}! You are logged in.`);
}
}).catch(err => console.error(err));
}
@ -88,6 +89,7 @@ export function createAccount() {
},
}, responseData => {
saveToken(responseData.token);
window.account = responseData.user;
if (responseData.hasOwnProperty('dictionary')) {
uploadWholeDictionary(); // Saves external id
}
@ -111,8 +113,22 @@ export function createAccount() {
}
}
export function validateToken() {
request({
action: 'validate-token',
}, userData => {
window.account = userData;
triggerLoginChanges();
addMessage(`Welcome${window.account.publicName !== '' ? ', ' + window.account.publicName : ''}! You are logged in.`, 10000);
syncDictionary();
}, error => {
addMessage(error + '. Logging Out.', undefined, 'error');
setCookie('token', '', -1);
});
}
export function triggerLoginChanges() {
const loginButton = document.getElementById('loginCreateAccountButton')
const loginButton = document.getElementById('loginCreateAccountButton');
const logoutButton = document.createElement('a');
logoutButton.classList.add('button');
logoutButton.id = 'logoutButton';

View File

@ -73,6 +73,16 @@ VALUES (?, ?, ?, ?, ?)';
);
}
public function validateToken ($token) {
$token_data = $this->token->decode($token);
if ($token_data !== false) {
if (isset($token_data->id)) {
return $this->getUserData($token_data->id);
}
}
return false;
}
public function setUserData ($token, $user_data) {
$token_data = $this->token->decode($token);
if ($token_data !== false) {

View File

@ -18,6 +18,26 @@ $action = isset($request['action']) ? $request['action'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : false;
switch ($action) {
case 'validate-token': {
if ($token !== false) {
$user = new User();
$user_data = $user->validateToken($token);
if ($user_data !== false) {
return Response::json(array(
'data' => $user_data,
'error' => false,
), 200);
}
return Response::json(array(
'data' => 'Could not validate token: incorrect data',
'error' => true,
), 401);
}
return Response::json(array(
'data' => 'Could not validate token: required information missing',
'error' => true,
), 400);
}
case 'login': {
if (isset($request['email']) && isset($request['password'])) {
$user = new User();