-1

I'm developing an application with Vue 3 and Laravel 9.

I did all login, registration and logout. However, I had an idea to keep the session unique per browser. I delete all tokens on user login if they exist. That way I can have front-end control with just one session open per browser, as I've already done this logic there.

The problem knowing the ways I can remove the token on the front-end to logout/redirect the user to login, after receiving the "Unauthenticated". For I check the vue routes with the token in localStorage.

Remembering, I'm using sanctum.

I'm looking for procedures. However, I'm afraid of doing something wrong, as I wonder about data security and vulnerabilities, as these processes of building a software require caution and a lot of testing.

0

1 Answer 1

1

I'm not sure if i got the question right, but you would normalle use an axios interceptor like so:

_axios.interceptors.response.use(
  response => response,
  error => {
        
    if (error.response.status === 401) {
      console.log('Intercept: 401 - Unauthenticated')
      //DELETE THE TOKEN FROM STORAGE AND FROM AXIOS HEADER
      delete window.axios.defaults.headers.common.Authorization
      localStorage.removeItem('token')

      return router.replace({ name: 'login' })
    }
    if (error.response.status === 403) {
      console.log('Intercept: 403 - Unauthorized')
    }
    if (error.response.status === 422) {
      console.log('Intercept: 422 - Validation Error')
    }
        
    return Promise.reject(error)
  },
)
3
  • I'm from Brazil and I'm using the translator, so it may have been bad reading for you. On the other hand, that was it, I went to research about interceptors in axios. So I realized that this was what I needed to log out the user and set the token without refreshing the page in my SPA.
    – Mateus
    Commented Jan 1, 2023 at 13:48
  • Thank you so much for your answer. I'm making a system from scratch for the first time with API.
    – Mateus
    Commented Jan 1, 2023 at 13:49
  • No Problem, you probably want to read about state management with vuex or pina to store your user.
    – ToniLu
    Commented Jan 2, 2023 at 17:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.