From 8c7a001bca2337ca8596a66575e46b5a45f35a31 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 8 Nov 2018 01:08:09 +0000 Subject: [PATCH] --- app/ayanova/src/api/apiutil.js | 3 ++ app/ayanova/src/api/auth.js | 51 ++++++++----------- .../src/utils/{auth.js => authUtil.js} | 6 +-- app/ayanova/src/views/login.vue | 40 ++++++++++++--- 4 files changed, 60 insertions(+), 40 deletions(-) rename app/ayanova/src/utils/{auth.js => authUtil.js} (97%) diff --git a/app/ayanova/src/api/apiutil.js b/app/ayanova/src/api/apiutil.js index fc2d53b9..41ec356e 100644 --- a/app/ayanova/src/api/apiutil.js +++ b/app/ayanova/src/api/apiutil.js @@ -1,8 +1,11 @@ +import logger from "../utils/logit"; + export default { status(response) { if (response.status >= 200 && response.status < 300) { return Promise.resolve(response); } else { + logger.log("API error", response.statusText); return Promise.reject(new Error(response.statusText)); } }, diff --git a/app/ayanova/src/api/auth.js b/app/ayanova/src/api/auth.js index e53cdae2..08fe0dd8 100644 --- a/app/ayanova/src/api/auth.js +++ b/app/ayanova/src/api/auth.js @@ -1,37 +1,30 @@ import config from "../utils/config"; import api from "./apiutil"; -import { processLogin, processLogout } from "../utils/auth"; +import { processLogin, processLogout } from "../utils/authUtil"; export default { async authenticate(login, password) { - return ( - fetch(config.apiUrl + "auth", { - method: "post", - mode: "cors", - headers: { - Accept: "application/json, text/plain, */*", - "Content-Type": "application/json" - }, - body: JSON.stringify({ - login: login, - password: password - }) + return fetch(config.apiUrl + "auth", { + method: "post", + mode: "cors", + headers: { + Accept: "application/json, text/plain, */*", + "Content-Type": "application/json" + }, + body: JSON.stringify({ + login: login, + password: password }) - .then(api.status) - .then(api.json) - .then(processLogin) - .then(() => { - return Promise.resolve(true); - }) //succeeded, nothing to return - // .then(function(data) { - // //todo: this should just return a bool on successful login and let the util\auth handle storing token etc - // //router will handle views available based on roles etc so login only really needs to know if it succeeded or not. - // return data; - // }) - .catch(function(error) { - processLogout(); - return Promise.reject(error); - }) - ); + }) + .then(api.status) + .then(api.json) + .then(processLogin) + .then(() => { + return Promise.resolve(true); + }) //succeeded, nothing to return + .catch(function(error) { + processLogout(); + return Promise.reject(error); + }); } }; diff --git a/app/ayanova/src/utils/auth.js b/app/ayanova/src/utils/authUtil.js similarity index 97% rename from app/ayanova/src/utils/auth.js rename to app/ayanova/src/utils/authUtil.js index 18dd047d..3455bc6f 100644 --- a/app/ayanova/src/utils/auth.js +++ b/app/ayanova/src/utils/authUtil.js @@ -1,5 +1,3 @@ -/* eslint-disable */ - import decode from "jwt-decode"; import config from "./config"; import logger from "./logit"; @@ -131,8 +129,8 @@ export function setToken(token) { } export function isLoggedIn() { - const token = getToken(); - return !!token && !isTokenExpired(token); + //const token = getToken(); + return !!config.apiToken && !isTokenExpired(config.apiToken); } function getTokenExpirationDate(encodedToken) { diff --git a/app/ayanova/src/views/login.vue b/app/ayanova/src/views/login.vue index 1978b9ad..3dbbb8ce 100644 --- a/app/ayanova/src/views/login.vue +++ b/app/ayanova/src/views/login.vue @@ -1,11 +1,37 @@