From 217e56604d18e3197062586a32e889dd6586e2d2 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Tue, 16 Jun 2020 13:05:43 -0400 Subject: [PATCH] use separate appLogin endpoint --- src/_services/user.service.js | 15 +++++++++-- src/_store/account.module.js | 48 ++++++++++++++++++++++++++++++----- src/auth/AppLoginPage.vue | 4 +-- 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/_services/user.service.js b/src/_services/user.service.js index 0b198d0..b241fa1 100644 --- a/src/_services/user.service.js +++ b/src/_services/user.service.js @@ -7,6 +7,7 @@ import { util } from '../_helpers'; export const userService = { login, + appLogin, logout, forgotPassword, register, @@ -55,6 +56,16 @@ function login(name, password, totpToken, unlockKey, messages, errors) { .then(setSessionUser); } +function appLogin(session, messages, errors) { + const requestOptions = { + method: 'POST', + headers: { 'Content-Type': 'application/json' } + }; + return fetch(`${config.apiUrl}/auth/appLogin/${session}`, requestOptions) + .then(handleAuthResponse(messages, errors)) + .then(setSessionUser); +} + function logout(messages, errors) { if (util.currentUser() === null) { console.log('userService.logout: already logged out'); @@ -84,8 +95,8 @@ function searchAccounts(query, messages, errors) { return fetch(`${config.apiUrl}/search/account`, util.postWithAuth(query)).then(util.handleCrudResponse(messages, errors)); } -function getMe(messages, errors, session) { - return fetch(`${config.apiUrl}/me`, util.getWithAuth(session)).then( +function getMe(messages, errors) { + return fetch(`${config.apiUrl}/me`, util.getWithAuth()).then( response => { if (!response.ok && response.status === 404) util.logout(); return response; diff --git a/src/_store/account.module.js b/src/_store/account.module.js index d3898a7..e9790b2 100644 --- a/src/_store/account.module.js +++ b/src/_store/account.module.js @@ -38,11 +38,11 @@ const actions = { refreshUser({ commit }) { commit('refreshUser', JSON.parse(localStorage.getItem(util.USER_KEY))); }, - checkSession({ commit }, { messages, errors, session, uri }) { + checkSession({ commit }, { messages, errors }) { commit('checkSessionRequest'); - userService.getMe(messages, errors, session) + userService.getMe(messages, errors) .then( - user => commit('checkSessionSuccess', {user, uri}), + user => commit('checkSessionSuccess', user), error => { commit('checkSessionFailure', error); if (error === 'Unauthorized' || error === 'Not Found' || error === 'Forbidden' ) { @@ -85,6 +85,22 @@ const actions = { error => commit('loginFailure', error) ); }, + appLogin({ commit }, { session, uri, messages, errors }) { + commit('appLoginRequest'); + userService.appLogin(session, messages, errors) + .then( + user => commit('appLoginSuccess', {user, uri}), + error => { + commit('appLoginFailure', error); + if (error === 'Unauthorized' || error === 'Not Found' || error === 'Forbidden' ) { + userService.logout(messages, errors).then( + ok => router.replace('/login'), + error => router.replace('/login') + ); + } + } + ); + }, logout({ commit }, {messages, errors}) { commit('logoutRequest'); userService.logout(messages, errors) @@ -202,10 +218,9 @@ const mutations = { state.user = user; }, checkSessionRequest(state) {}, - checkSessionSuccess(state, {user, uri}) { - let hasUri = typeof uri !== 'undefined' && uri != null; + checkSessionSuccess(state, user) { if (user.token) { - if (util.currentUser() === null && !hasUri) { + if (util.currentUser() === null) { // we must have logged out while this request was in flight... do nothing state.user = null; } else { @@ -215,7 +230,6 @@ const mutations = { } } state.locale = (typeof user.locale !== 'undefined' && user.locale !== null ? user.locale : state.locale); - if (hasUri && user.token) router.replace(uri); }, checkSessionFailure(state, error) { state.user = null; @@ -244,6 +258,26 @@ const mutations = { state.user = null; }, + appLoginRequest(state) {}, + appLoginSuccess(state, {user, uri}) { + if (user.token) { + if (util.currentUser() === null) { + // we must have logged out while this request was in flight... do nothing + state.user = null; + } else { + localStorage.setItem(util.USER_KEY, JSON.stringify(user)); + state.status = Object.assign({}, state.status, {loggingIn: false, loggedIn: true}); + state.user = user; + } + } + state.locale = (typeof user.locale !== 'undefined' && user.locale !== null ? user.locale : state.locale); + if (user.token) router.replace(uri); + }, + appLoginFailure(state, error) { + state.user = null; + state.status = Object.assign({}, state.status, {loggedIn: false}); + }, + logoutRequest(state) {}, logoutSuccess(state) { state.status = Object.assign({}, defaultStatus); diff --git a/src/auth/AppLoginPage.vue b/src/auth/AppLoginPage.vue index 747ab1b..ec1d643 100644 --- a/src/auth/AppLoginPage.vue +++ b/src/auth/AppLoginPage.vue @@ -48,10 +48,10 @@ uri = '/'; } localStorage.setItem(util.USER_KEY, JSON.stringify(user)); - this.checkSession({messages: this.messages, errors: this.errors, session: session, uri: uri}); + this.appLogin({session: session, uri: uri, messages: this.messages, errors: this.errors}); }, methods: { - ...mapActions('account', ['login', 'logout', 'checkSession']), + ...mapActions('account', ['login', 'logout', 'appLogin']), ...mapActions('system', ['loadSystemConfigs', 'loadMessages']), } };