W
W
Wytpry2018-06-10 16:55:24
React
Wytpry, 2018-06-10 16:55:24

How to implement tokens in react?

Good afternoon! What is the best way to implement tokens in react using something like this:

const instance = axios.create({
  baseURL: "http://.../api/",
  timeout: 1000,
  headers: {Authorization: 'Bearer '}
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-06-10
@Wytpry

You can write a function that will write the token to the default headers and call it when the token is received.

import Axios from 'axios';

const setupAxios = token =>
  (Axios.defaults.headers = {
    Authorization: `Bearer ${token}`,
  });

export default setupAxios;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question