TSOA How to save login/reg body's token as a header for all other requests

543 Views Asked by At

I want to save the token which comes from my login or register response body, and use it on all my other requests without copy pasting it into every request header param, is that possible? (I'm using swagger to test it)

The documentation is really minimal and they dont really mention anything like this.

1

There are 1 best solutions below

0
On

I think there are some options.

  1. One way is to use the set-cookie of the browser. To implement this, you have to use an express session.

  2. Another way is to save the token received from the server, in local storage and append the token to the header of each request. I think you can implement this using axios interceptor simply.

    import axios from 'axios';

    const axiosIns = axios.create({ baseURL: ${...}, });

    axiosIns.interceptors.request.use((config) => { config['headers'] = { ...(config.headers ?? {}), ...(getToken() ? { Authorization: Bearer ${getToken()} } : {}), }; return config; });

    export default axiosIns;