[Vuejs]-Navigation guard keeps looping

0👍

My problem was in passing a value to the next() function call. By removing that out it worked alright:

function guard (to, from, next) {

  console.log('To:')
  console.log(to)
  console.log('From:')
  console.log(from)
  console.log('EntryUrl: ' + entryUrl)

  // 1 - If no entry path was provided then set to 
  if(entryUrl == null || entryUrl == undefined){
    entryUrl = to.path
    console.log('EntryUrl: ' + entryUrl)
  }

  // 2 - Check if the user is marked as being logged in
  var loggedin_state = store.state.user.auth.loggedin
  if(loggedin_state == undefined) {
    store.commit('user/set_delete_session', null)
    return next("/login");
  }

  // 3 - If they are marked as logged in continue
  var user = store.state.user.user
  var token = store.state.user.auth.token
  if(loggedin_state == true) {

    // If the user isn't authorised with a token then send them to the log in page
    if(token == null) {
      store.commit('user/set_delete_session', null)
      return next("/login");
    }

    // If they've got a token but no user profile data acquire it
    if(user == null) {
      UserApi.get_user_information(response.data.token)
      .then(response => {
          store.commit('user/set_user', response.data)
      })
    }

    // If they tried a route before logging in that would have been stored
    if(entryUrl) {
      console.log('Go to saved URL')
      // Store the url before wiping it
      let url = entryUrl;
      // Wipe the entry url variable
      entryUrl = null;
      // Go to stored url
      return go_to_url(url);
    } else {
      console.log('Go to pointed url')
      // Carry on to permission checking function
      return go_to_url(to.path);
    }
  } else {

    // The user is not logged in. Store the URL they were trying to visit and redirect them to the login page
    entryUrl = to.path
    console.log('EntryUrl: ' + entryUrl)
    return next("/login");

  }

  function go_to_url(url) {
    console.log(url)

    // 1 - Grab the user permissions from the user profile
    var permissions_array = null
    if(user !== null) {
      permissions_array = user.permissions
    }
    console.log(permissions_array)

    // 2 - Check and route
    if(permissions_array !== null) {

      // Find the relevant permission object based upon the route name and the area key
      var view_permissions = permissions_array.find(view => view.area === to.name);
      console.log(view_permissions)

      // If a permission object was found check its status, if no object found assume it is okay to view
      if(view_permissions !== undefined) {
        // If set to 1 the user can view this route, else reroute to a permissions denied page 
        if(view_permissions.read == 1) {
          // Go to url
          console.log('GUARD - PROCEED')
          console.log(to.name)
          next();
        } else {
          console.log('GUARD - BLOCKED')
          return next("/permission-denied");
        }
      } else {
        return next()
      }

    }

  };

};

Leave a comment