Fix logout, and redirect loops.

pull/3890/head
Julio Montoya 4 years ago
parent 7f71ddc4d6
commit 31a9fa18bc
  1. 34
      assets/vue/App.vue
  2. 3
      assets/vue/api/security.js
  3. 4
      assets/vue/store/index.js
  4. 18
      assets/vue/store/security.js
  5. 5
      assets/vue/views/Login.vue
  6. 3
      config/routes.yaml
  7. 4
      src/CoreBundle/Controller/SecurityController.php
  8. 4
      src/CoreBundle/EventListener/LogoutListener.php

@ -1,5 +1,6 @@
<template> <template>
<component :is="layout"> <component :is="layout">
<!-- <Breadcrumb :legacy="this.breadcrumb"/>-->
<router-view /> <router-view />
<div id="legacy_content" <div id="legacy_content"
v-html="legacyContent" v-html="legacyContent"
@ -457,8 +458,10 @@ export default {
var n = url.indexOf("main/"); var n = url.indexOf("main/");
if (n > 0) { if (n > 0) {
if (this.firstTime) { if (this.firstTime) {
console.log('firstTime: 1.');
let content = document.querySelector("#sectionMainContent"); let content = document.querySelector("#sectionMainContent");
if (content) { if (content) {
console.log('legacyContent updated');
content.style.display = 'block'; content.style.display = 'block';
document.querySelector("#sectionMainContent").remove(); document.querySelector("#sectionMainContent").remove();
this.legacyContent = content.outerHTML; this.legacyContent = content.outerHTML;
@ -469,6 +472,8 @@ export default {
console.log('remove'); console.log('remove');
} }
console.log('Replace URL', url);
window.location.replace(url); window.location.replace(url);
/*axios.get(url, { /*axios.get(url, {
@ -494,13 +499,16 @@ export default {
} }
} else { } else {
if (this.firstTime) { if (this.firstTime) {
console.log('firstTime 2.');
let content = document.querySelector("#sectionMainContent"); let content = document.querySelector("#sectionMainContent");
if (content) { if (content) {
console.log('legacyContent updated');
content.style.display = 'block'; content.style.display = 'block';
document.querySelector("#sectionMainContent").remove(); document.querySelector("#sectionMainContent").remove();
this.legacyContent = content.outerHTML; this.legacyContent = content.outerHTML;
} }
} else { } else {
console.log('legacyContent cleaned');
let content = document.querySelector("#sectionMainContent"); let content = document.querySelector("#sectionMainContent");
if (content) { if (content) {
document.querySelector("#sectionMainContent").remove(); document.querySelector("#sectionMainContent").remove();
@ -518,24 +526,13 @@ export default {
}, },
created() { created() {
console.log('created'); console.log('created');
// @todo
if (this.isAuthenticated) {
this.links1.unshift({icon: 'user-circle', url: '/account/profile', text: this.currentUser.username});
}
let app = document.getElementById('app'); let app = document.getElementById('app');
this.legacyContent = ''; this.legacyContent = '';
console.log('updated empty created'); console.log('updated empty created');
let isAuthenticated = false;
/*if (app && app.attributes['data-is-authenticated'].value) {
isAuthenticated = JSON.parse(app.attributes['data-is-authenticated'].value);
}*/
console.log('isAuthenticated');
console.log(isAuthenticated);
console.log(window.user);
let isAuthenticated = false;
if (!isEmpty(window.user)) { if (!isEmpty(window.user)) {
// console.log('is logged in as ' + window.user.username); console.log('is logged in as ' + window.user.username);
this.user = window.user; this.user = window.user;
this.userAvatar = window.userAvatar; this.userAvatar = window.userAvatar;
isAuthenticated = true; isAuthenticated = true;
@ -549,6 +546,10 @@ export default {
let payload = {isAuthenticated: isAuthenticated, user: this.user}; let payload = {isAuthenticated: isAuthenticated, user: this.user};
this.$store.dispatch("security/onRefresh", payload); this.$store.dispatch("security/onRefresh", payload);
if (isAuthenticated) {
this.links1.unshift({icon: 'user-circle', url: '/account/profile', text: this.currentUser.username});
}
if (app && app.attributes["data-flashes"]) { if (app && app.attributes["data-flashes"]) {
let flashes = JSON.parse(app.attributes["data-flashes"].value); let flashes = JSON.parse(app.attributes["data-flashes"].value);
if (flashes) { if (flashes) {
@ -565,11 +566,16 @@ export default {
} }
axios.interceptors.response.use(undefined, (err) => { axios.interceptors.response.use(undefined, (err) => {
console.log('interceptor');
console.log(err.response.status);
return new Promise(() => { return new Promise(() => {
// Unauthorized. // Unauthorized.
if (401 === err.response.status) { if (401 === err.response.status) {
// Redirect to the login if status 401. // Redirect to the login if status 401.
this.$router.push({path: "/login"}).catch(()=>{}); //this.$router.replace({path: "/login"}).catch(()=>{});
// Real redirect to avoid loops with Login.vue page.
window.location.href = '/login';
} else if (500 === err.response.status) { } else if (500 === err.response.status) {
if (err.response) { if (err.response) {
// Request made and server responded // Request made and server responded

@ -7,5 +7,8 @@ export default {
password: password, password: password,
csrf_token: token csrf_token: token
}); });
},
logout() {
return axios.get("/logout");
} }
} }

@ -1,10 +1,10 @@
import { createStore } from "vuex"; import { createStore, createLogger } from "vuex";
import notifications from './modules/notifications'; import notifications from './modules/notifications';
import SecurityModule from "./security"; import SecurityModule from "./security";
import createPersistedState from "vuex-persistedstate"; import createPersistedState from "vuex-persistedstate";
export default createStore({ export default createStore({
plugins: [createPersistedState()], plugins: [createLogger(), createPersistedState()],
modules: { modules: {
notifications, notifications,
security: SecurityModule, security: SecurityModule,

@ -3,6 +3,7 @@ import SecurityAPI from "../api/security";
const AUTHENTICATING = "AUTHENTICATING", const AUTHENTICATING = "AUTHENTICATING",
AUTHENTICATING_SUCCESS = "AUTHENTICATING_SUCCESS", AUTHENTICATING_SUCCESS = "AUTHENTICATING_SUCCESS",
AUTHENTICATING_ERROR = "AUTHENTICATING_ERROR", AUTHENTICATING_ERROR = "AUTHENTICATING_ERROR",
AUTHENTICATING_LOGOUT = "AUTHENTICATING_LOGOUT",
PROVIDING_DATA_ON_REFRESH_SUCCESS = "PROVIDING_DATA_ON_REFRESH_SUCCESS"; PROVIDING_DATA_ON_REFRESH_SUCCESS = "PROVIDING_DATA_ON_REFRESH_SUCCESS";
export default { export default {
@ -64,6 +65,13 @@ export default {
state.isAuthenticated = false; state.isAuthenticated = false;
state.user = null; state.user = null;
}, },
[AUTHENTICATING_LOGOUT](state, error) {
console.log('AUTHENTICATING_LOGOUT');
state.isLoading = false;
state.error = error;
state.isAuthenticated = false;
state.user = null;
},
[PROVIDING_DATA_ON_REFRESH_SUCCESS](state, payload) { [PROVIDING_DATA_ON_REFRESH_SUCCESS](state, payload) {
state.isLoading = false; state.isLoading = false;
state.error = null; state.error = null;
@ -81,6 +89,16 @@ export default {
commit(AUTHENTICATING_ERROR, error); commit(AUTHENTICATING_ERROR, error);
}); });
}, },
async logout({commit}) {
console.log('logout store/security');
await SecurityAPI.logout().then(response => {
commit(AUTHENTICATING_LOGOUT);
return response.data;
}).catch(error => {
commit(AUTHENTICATING_ERROR, error);
});
},
onRefresh({commit}, payload) { onRefresh({commit}, payload) {
commit(PROVIDING_DATA_ON_REFRESH_SUCCESS, payload); commit(PROVIDING_DATA_ON_REFRESH_SUCCESS, payload);
} }

@ -87,12 +87,14 @@ export default {
}), }),
}, },
created() { created() {
console.log('Login created');
let redirect = this.$route.query.redirect; let redirect = this.$route.query.redirect;
if (this.$store.getters["security/isAuthenticated"]) { if (this.$store.getters["security/isAuthenticated"]) {
console.log(redirect);
if (typeof redirect !== "undefined") { if (typeof redirect !== "undefined") {
this.$router.push({path: redirect}); this.$router.push({path: redirect});
} else { } else {
this.$router.push({path: "/courses"}); this.$router.push({path: "/"});
} }
} }
}, },
@ -102,6 +104,7 @@ export default {
this.performLogin(); this.performLogin();
}, },
async performLogin() { async performLogin() {
console.log('performLogin');
let payload = {login: this.$data.login, password: this.$data.password}; let payload = {login: this.$data.login, password: this.$data.password};
let redirect = this.$route.query.redirect; let redirect = this.$route.query.redirect;
await this.$store.dispatch("security/login", payload); await this.$store.dispatch("security/login", payload);

@ -1,6 +1,3 @@
login_check:
path: /login_check
logout: logout:
path: /logout path: /logout

@ -33,7 +33,7 @@ class SecurityController extends AbstractController
//$lastUsername = $authenticationUtils->getLastUsername(); //$lastUsername = $authenticationUtils->getLastUsername();
/** @var User $user */ /** @var User $user */
$user = $this->getUser(); /*$user = $this->getUser();
$data = []; $data = [];
if ($user) { if ($user) {
$userClone = clone $user; $userClone = clone $user;
@ -41,7 +41,7 @@ class SecurityController extends AbstractController
$data = $this->serializer->serialize($userClone, JsonEncoder::FORMAT); $data = $this->serializer->serialize($userClone, JsonEncoder::FORMAT);
} }
return new JsonResponse($data, Response::HTTP_OK, [], true); return new JsonResponse($data, Response::HTTP_OK, [], true);*/
} }
/** /**

@ -10,7 +10,9 @@ use Chamilo\CoreBundle\Entity\User;
use Database; use Database;
use Doctrine\DBAL\Cache\ArrayStatement; use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@ -90,5 +92,7 @@ class LogoutListener
$login = $this->router->generate('home'); $login = $this->router->generate('home');
return new RedirectResponse($login); return new RedirectResponse($login);
//return new JsonResponse('logout out', 200);
} }
} }

Loading…
Cancel
Save