How to access to Nuxt context from js file

Viewed 513

I make requests to the API in separate files, for example:

// ~/api/request.ts

import axios from 'axios'

export const testMethod = async (
  email: string
) => {
  try {
    return await axios.post('/test-route/', { email })
  } catch (error) {
    if (error.code === '401') {
      //
    }
  }
}

I want:

  1. To add error handling, if the token has expired, then redirect to the authorization page and clear store.
  2. Create axios instance with Authorization header, with token from Vuex

How do I access the Nuxt context from this file?

1 Answers

I can suggest putting this logic to the vue mixin. Then you can interact with the vue/nuxt context.

Related