5

Say I want to create an AuthService in vue-router to validate the current session before proceeding to the next route, like the example on:

http://vuejs.github.io/vue-router/en/api/before-each.html

router.beforeEach(function ({ to, next }) {
  if (to.path === '/auth-required') {
    // return a Promise that resolves to true or false
    return AuthService.isLoggedIn()
  } else {
    next()
  }
})
  • How would one approach this for Laravel 5.1 use without using JSON Web Tokens(JWT)?
  • Is there a "best-pratice" approach to SESSION_DRIVER in Laravel, ex. Redis, for this scenario?

I've searched around the web alot but never seen any attempt do authenticate a session with vue-router without JWT.

3
  • what's the point of authenticating a session within the browser anyways? Commented Feb 15, 2016 at 22:43
  • Why are you against using JWT ? Commented Mar 28, 2016 at 17:13
  • 1
    A best practice would be to use JWTs or some sort of API token. Commented May 13, 2016 at 15:06

1 Answer 1

3

For your code, you want to move the isLoggedIn() auth check into the if statement. Your auth service should return a boolean if the user is logged in. Within the if, you would route the user to the appropriate path. beforeEach works like "What should we do before each route is processed?" so you do not need to return a truthy value inside the if statement.

router.beforeEach(function (transition) {
    if (transition.to.auth && !AuthService.isLoggedIn()) {
        // if route requires auth and user isn't authenticated
        transition.redirect('/login')
    } else {
        transition.next()
    }
})

If you want to "validate the current session before proceeding to the next route" each time, your isLoggedIn() would need to call your login API each time. It's usually not best practice because once you login, why do you need to check again? That's why tokens and JWT exist. After you login, you're given a token, you remember this token and send the token in upcoming requests.

How would one approach this for Laravel 5.1 use without using JSON Web Tokens(JWT)?

Not technically JWTs, you can use API tokens. API tokens can be generated with Laravel's str_random() function. You'd associate 1 token per user and keep the tokens unique. You can put this token in 2 places: 1. in the URL for parameter ?api_token=XXX 2. in the header for "Authorization: Bearer XXX".

If you're going with headers, in Vue.js, you'd setup vue-resource as such:

Vue.http.headers.common['Authorization'] = 'Bearer ' + token;

and then all your requests now contain the API token.

Is there a "best-pratice" approach to SESSION_DRIVER in Laravel, ex. Redis, for this scenario?

Not 100% sure what you mean here, but tokens are considered one of the best practices when interacting with APIs. You exchange the token with each web request so that you do not need to send a username/password each time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.