Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement refresh token? #53

Open
RaruRvTLeo opened this issue Sep 21, 2020 · 4 comments
Open

How to implement refresh token? #53

RaruRvTLeo opened this issue Sep 21, 2020 · 4 comments

Comments

@RaruRvTLeo
Copy link

No description provided.

@RaruRvTLeo
Copy link
Author

RaruRvTLeo commented Sep 21, 2020

I had found a code here

But when I am trying this code, its showing an error like this:

./src/interceptor.js
  Line 22:13:  'Services' is not defined  no-undef

My code is like

import React, { useState, useEffect } from 'react'
import fetchIntercept from 'fetch-intercept'
const baseurl = process.env.REACT_APP_API_BASE_URL
export const interceptorVariable = fetchIntercept.register({
  request: function (url, config) {
    var token = localStorage.getItem('loginToken')
    // Modify the url or config here
    if (url.includes(baseurl)) {
      config.headers = { Authorization: token }
    }
    return [url, config]
  },

  requestError: function (error) {
    // Called when an error occured during another 'request' interceptor call
    return Promise.reject(error)
  },

  response: function (response) {
    // Modify the reponse object
    // return response
    if (response.status == 401) {
      Services.refreshToken((res)=>{
        console.log("tok",res)
        if (res.message == 'success') {
          // if token has been refreshed
          // recall the request again
        }else {
          // login again
        }
      })
    }else {
      return response;
    }
  },

  responseError: function (error) {
    // Handle an fetch error
    return Promise.reject(error)
  },
})```

@bleuscyther
Copy link

@RaruRvTLeo use Arrow functions instead (just in case you use this in Class component)

 response:  (response)=> {
   //...
  },

also you did not import Sevices

@awaissahmed80
Copy link

awaissahmed80 commented Apr 6, 2021

please check

#39 (comment)

@dungdv95
Copy link

dungdv95 commented Nov 7, 2023

how to refresh token with multi request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants