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>
<component :is="layout">
<!-- <Breadcrumb :legacy="this.breadcrumb"/>-->
<router-view />
<div id="legacy_content"
v-html="legacyContent"
@ -457,8 +458,10 @@ export default {
var n = url.indexOf("main/");
if (n > 0) {
if (this.firstTime) {
console.log('firstTime: 1.');
let content = document.querySelector("#sectionMainContent");
if (content) {
console.log('legacyContent updated');
content.style.display = 'block';
document.querySelector("#sectionMainContent").remove();
this.legacyContent = content.outerHTML;
@ -469,6 +472,8 @@ export default {
console.log('remove');
}
console.log('Replace URL', url);
window.location.replace(url);
/*axios.get(url, {
@ -494,13 +499,16 @@ export default {
}
} else {
if (this.firstTime) {
console.log('firstTime 2.');
let content = document.querySelector("#sectionMainContent");
if (content) {
console.log('legacyContent updated');
content.style.display = 'block';
document.querySelector("#sectionMainContent").remove();
this.legacyContent = content.outerHTML;
}
} else {
console.log('legacyContent cleaned');
let content = document.querySelector("#sectionMainContent");
if (content) {
document.querySelector("#sectionMainContent").remove();
@ -518,24 +526,13 @@ export default {
},
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');
this.legacyContent = '';
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)) {
// console.log('is logged in as ' + window.user.username);
console.log('is logged in as ' + window.user.username);
this.user = window.user;
this.userAvatar = window.userAvatar;
isAuthenticated = true;
@ -549,6 +546,10 @@ export default {
let payload = {isAuthenticated: isAuthenticated, user: this.user};
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"]) {
let flashes = JSON.parse(app.attributes["data-flashes"].value);
if (flashes) {
@ -565,11 +566,16 @@ export default {
}
axios.interceptors.response.use(undefined, (err) => {
console.log('interceptor');
console.log(err.response.status);
return new Promise(() => {
// Unauthorized.
if (401 === err.response.status) {
// 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) {
if (err.response) {
// Request made and server responded

@ -7,5 +7,8 @@ export default {
password: password,
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 SecurityModule from "./security";
import createPersistedState from "vuex-persistedstate";
export default createStore({
plugins: [createPersistedState()],
plugins: [createLogger(), createPersistedState()],
modules: {
notifications,
security: SecurityModule,

@ -3,6 +3,7 @@ import SecurityAPI from "../api/security";
const AUTHENTICATING = "AUTHENTICATING",
AUTHENTICATING_SUCCESS = "AUTHENTICATING_SUCCESS",
AUTHENTICATING_ERROR = "AUTHENTICATING_ERROR",
AUTHENTICATING_LOGOUT = "AUTHENTICATING_LOGOUT",
PROVIDING_DATA_ON_REFRESH_SUCCESS = "PROVIDING_DATA_ON_REFRESH_SUCCESS";
export default {
@ -64,6 +65,13 @@ export default {
state.isAuthenticated = false;
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) {
state.isLoading = false;
state.error = null;
@ -81,6 +89,16 @@ export default {
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) {
commit(PROVIDING_DATA_ON_REFRESH_SUCCESS, payload);
}

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

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

@ -33,7 +33,7 @@ class SecurityController extends AbstractController
//$lastUsername = $authenticationUtils->getLastUsername();
/** @var User $user */
$user = $this->getUser();
/*$user = $this->getUser();
$data = [];
if ($user) {
$userClone = clone $user;
@ -41,7 +41,7 @@ class SecurityController extends AbstractController
$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 Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@ -90,5 +92,7 @@ class LogoutListener
$login = $this->router->generate('home');
return new RedirectResponse($login);
//return new JsonResponse('logout out', 200);
}
}

Loading…
Cancel
Save