Validate token if one exists; Save account data
This commit is contained in:
parent
f0b146e5fc
commit
f8b9503960
|
@ -1,9 +1,8 @@
|
||||||
import '../../scss/Account/main.scss';
|
import '../../scss/Account/main.scss';
|
||||||
|
|
||||||
import { renderLoginForm } from "./render";
|
import { renderLoginForm } from "./render";
|
||||||
import { triggerLoginChanges } from './login';
|
import { validateToken } from './login';
|
||||||
import {
|
import {
|
||||||
syncDictionary,
|
|
||||||
uploadWords,
|
uploadWords,
|
||||||
uploadDetails,
|
uploadDetails,
|
||||||
uploadWholeDictionary,
|
uploadWholeDictionary,
|
||||||
|
@ -17,8 +16,7 @@ export function showLoginForm() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loginWithToken() {
|
export function loginWithToken() {
|
||||||
triggerLoginChanges();
|
validateToken();
|
||||||
syncDictionary();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function syncImportedDictionary() {
|
export function syncImportedDictionary() {
|
||||||
|
|
|
@ -3,7 +3,8 @@ import { saveToken } from "./utilities";
|
||||||
import { addMessage } from "../utilities";
|
import { addMessage } from "../utilities";
|
||||||
import { setupLogoutButton } from "./setupListeners";
|
import { setupLogoutButton } from "./setupListeners";
|
||||||
import { renderAccountSettings } from "./render";
|
import { renderAccountSettings } from "./render";
|
||||||
import { uploadWholeDictionary } from "./sync";
|
import { uploadWholeDictionary, syncDictionary } from "./sync";
|
||||||
|
import { setCookie } from "../StackOverflow/cookie";
|
||||||
|
|
||||||
export function logIn() {
|
export function logIn() {
|
||||||
const email = document.getElementById('loginEmail').value.trim(),
|
const email = document.getElementById('loginEmail').value.trim(),
|
||||||
|
@ -26,8 +27,8 @@ export function logIn() {
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
}, successData => {
|
}, successData => {
|
||||||
console.log(successData);
|
|
||||||
saveToken(successData.token);
|
saveToken(successData.token);
|
||||||
|
window.account = successData.user;
|
||||||
}, errorData => {
|
}, errorData => {
|
||||||
errorHTML += errorData;
|
errorHTML += errorData;
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
|
@ -36,7 +37,7 @@ export function logIn() {
|
||||||
const loginModal = document.getElementById('loginModal');
|
const loginModal = document.getElementById('loginModal');
|
||||||
loginModal.parentElement.removeChild(loginModal);
|
loginModal.parentElement.removeChild(loginModal);
|
||||||
triggerLoginChanges();
|
triggerLoginChanges();
|
||||||
addMessage(`Welcome! You are logged in.`);
|
addMessage(`Welcome${window.account.publicName !== '' ? ', ' + window.account.publicName : ''}! You are logged in.`);
|
||||||
}
|
}
|
||||||
}).catch(err => console.error(err));
|
}).catch(err => console.error(err));
|
||||||
}
|
}
|
||||||
|
@ -88,6 +89,7 @@ export function createAccount() {
|
||||||
},
|
},
|
||||||
}, responseData => {
|
}, responseData => {
|
||||||
saveToken(responseData.token);
|
saveToken(responseData.token);
|
||||||
|
window.account = responseData.user;
|
||||||
if (responseData.hasOwnProperty('dictionary')) {
|
if (responseData.hasOwnProperty('dictionary')) {
|
||||||
uploadWholeDictionary(); // Saves external id
|
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() {
|
export function triggerLoginChanges() {
|
||||||
const loginButton = document.getElementById('loginCreateAccountButton')
|
const loginButton = document.getElementById('loginCreateAccountButton');
|
||||||
const logoutButton = document.createElement('a');
|
const logoutButton = document.createElement('a');
|
||||||
logoutButton.classList.add('button');
|
logoutButton.classList.add('button');
|
||||||
logoutButton.id = 'logoutButton';
|
logoutButton.id = 'logoutButton';
|
||||||
|
|
|
@ -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) {
|
public function setUserData ($token, $user_data) {
|
||||||
$token_data = $this->token->decode($token);
|
$token_data = $this->token->decode($token);
|
||||||
if ($token_data !== false) {
|
if ($token_data !== false) {
|
||||||
|
|
|
@ -18,6 +18,26 @@ $action = isset($request['action']) ? $request['action'] : '';
|
||||||
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : false;
|
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : false;
|
||||||
|
|
||||||
switch ($action) {
|
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': {
|
case 'login': {
|
||||||
if (isset($request['email']) && isset($request['password'])) {
|
if (isset($request['email']) && isset($request['password'])) {
|
||||||
$user = new User();
|
$user = new User();
|
||||||
|
|
Loading…
Reference in New Issue