Browse Source

Require login when API returns 401 Unauthorized (#7)

Merge branch 'master' into sfedoriv/RequireLoginWhenAPIReturnsUnauthorized

Redirect to /login on error me call

Require login when API returns 401 Unauthorized

Co-authored-by: jonathan <jonathan@noreply.git.bubblev.org>
Co-authored-by: Svitlana <sfedoriv@itekako.com>
Reviewed-on: https://git.bubblev.org/bubblev/bubble-web/pulls/7
pull/9/head
svitlana 4 years ago
committed by jonathan
parent
commit
2420c8eed0
2 changed files with 14 additions and 4 deletions
  1. +13
    -3
      src/_helpers/util.js
  2. +1
    -1
      src/_store/account.module.js

+ 13
- 3
src/_helpers/util.js View File

@@ -132,9 +132,14 @@ export const util = {
return response.text().then(text => {
const data = text && JSON.parse(text);
if (!response.ok) {
if (response.status === 404) {
if (response.status === 401) {
console.log('handleCrudResponse: unauthenticated request: ' + JSON.stringify(data));
util.logout();
vue.$route.replace('/login');
} else if (response.status === 404) {
// todo: show nicer error message
console.log('handleCrudResponse: received 404: ' + JSON.stringify(data));
console.log('handleCrudResponse: received 404: ' + JSON.stringify(data));

} else if (response.status === 422) {
// console.log('handleCrudResponseA: errors='+JSON.stringify(errors));
@@ -158,7 +163,12 @@ export const util = {
let errData = JSON.parse('' + text) || text;
if (Array.isArray(errData)) errData = errData[0];

if (response.status === 404) {
if (response.status === 401) {
console.log('handlePlaintextResponse: unauthenticated request: ' + JSON.stringify(data));
util.logout();
vue.$route.replace('/login');
} else if (response.status === 404) {
// todo: show nicer error message
console.log('handlePlaintextResponse: received 404: ' + (errData.resource || errData));



+ 1
- 1
src/_store/account.module.js View File

@@ -45,7 +45,7 @@ const actions = {
user => commit('checkSessionSuccess', user),
error => {
commit('checkSessionFailure', error);
if (error === 'Not Found' || error === 'Forbidden') {
if (error ==== 'Unauthorized' || error === 'Not Found' || error === 'Forbidden' ) {
userService.logout(messages, errors).then(
ok => router.replace('/login'),
error => router.replace('/login')


Loading…
Cancel
Save