How can I get the JWT token after authenticating?

0

I have a Rest spring-boot API which, when a user authenticates the api returns the token jwt, I noticed in the browser that the token appears in the Response Header > Authentication and in the tests with the Postman it shows in the Body.

How can I get this token to store in the browser's Local Storage by Reactjs?

My code that makes the requests looks like this:

import { ACCESS_TOKEN, API_BASE_URL } from '../constants';

export function request (options) {
    const headers = {
        'Content-Type': 'application/json',
    }

    if(localStorage.getItem(ACCESS_TOKEN)) {
        headers.append('Authorzation', 'Bearer ' + localStorage.getItem(ACCESS_TOKEN))

    }

    return fetch(API_BASE_URL+options.url, {
        method: options.method,
        headers: headers,
        body: options.body
    })
    .then(function(response){ 

        // Falta pegar o token e gravar na local estorage

        if(!response.ok) {
            return Promise.reject(json);
        }
        return json;

    });
};
    
asked by anonymous 16.11.2018 / 15:58

1 answer

0

To add to the localStorage just use the setItem (KEY, VALUE). I do not know what structure of your response, but it would look something like this:

localStorage.setItem(ACCESS_TOKEN, response.value.jwt);
    
04.12.2018 / 13:41