Better redirections while logged in

* If the user is already logged in and tries to go to /users/login,
redirect him to the default page
* Add check on the /users/edit page, it was not secure
This commit is contained in:
Grégoire Delattre 2017-05-21 16:12:50 +02:00
parent 316a850eec
commit 638fc971b2

View File

@ -81,8 +81,8 @@ export function startPollingTorrents() {
) )
} }
var pollingTorrentsId; // This function returns true if the user is logged in, false otherwise
const loginCheck = function(nextState, replace, next, f) { function isLoggedIn() {
const state = store.getState(); const state = store.getState();
const isLogged = state.userStore.isLogged; const isLogged = state.userStore.isLogged;
let token = localStorage.getItem('token'); let token = localStorage.getItem('token');
@ -98,10 +98,23 @@ const loginCheck = function(nextState, replace, next, f) {
}); });
} }
if (!isLogged && (!token || token === "")) { if (isLogged || (token && token !== "")) {
return true
}
return false
}
var pollingTorrentsId;
const loginCheck = function(nextState, replace, next, f = null) {
const loggedIn = isLoggedIn();
if (!loggedIn) {
replace('/users/login'); replace('/users/login');
} else { } else {
if (f) {
f(); f();
}
// Poll torrents once logged // Poll torrents once logged
if (!pollingTorrentsId) { if (!pollingTorrentsId) {
// Fetch the torrents every 10s // Fetch the torrents every 10s
@ -114,15 +127,34 @@ const loginCheck = function(nextState, replace, next, f) {
next(); next();
} }
const defaultRoute = '/movies/explore/yts/seeds';
const routes = { const routes = {
path: '/', path: '/',
component: App, component: App,
indexRoute: {onEnter: ({params}, replace) => replace('/movies/explore/yts/seeds')}, indexRoute: {onEnter: ({params}, replace) => replace(defaultRoute)},
childRoutes: [ childRoutes: [
{ path: '/users/login' , component: UserLoginForm }, {
{ path: '/users/signup' , component: UserSignUp }, path: '/users/signup',
{ path: '/users/edit' , component: UserEdit }, component: UserSignUp
{ path: '/users/signup' , component: UserSignUp }, },
{
path: '/users/login',
component: UserLoginForm,
onEnter: function(nextState, replace, next) {
if (isLoggedIn()) {
// User is already logged in, redirect him to the default route
replace(defaultRoute);
}
next();
},
},
{
path: '/users/edit',
component: UserEdit,
onEnter: function(nextState, replace, next) {
loginCheck(nextState, replace, next);
},
},
{ {
path: '/users/logout', path: '/users/logout',
onEnter: function(nextState, replace, next) { onEnter: function(nextState, replace, next) {