0

i'm using jwt token login in spring. when login success, server send redirect response include access-token at response header. so, i want catch the response at front-side, set the token at request header and send fetch request to response header 'location'.

but, fetch cannot catch the redirect response. fetch automatically try to redirect and catch after all redirect ends(200 status code).

this is my front code.

function sub(url, form) {
        var option = {
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: 'id=' + form.id.value + "&password=" + form.password.value
        }
        fetch(url, option)
            .then(response => {   // here that i want to catch redirect response. but status is 200
                if (response.status === 302) {
                    fetch(response.headers.get('location'), {
                        headers: {
                            Authentication: response.headers.get('Authentication')
                        }
                    })
                }
            })
    }

please tell me how can i catch redirect response before redirect..

enter image description here

this is my browser network tab.

potato
  • 1
  • You can’t catch the URL for the response prior to the redirect — browsers (by design and per requirements in the relevant specs) automatically follow all redirects, and do not expose the redirect details to frontend JavaScript code – sideshowbarker Jan 17 '23 at 03:41
  • @sideshowbarker thanks! do you know the best way to redirect user to some page needs authorization using token? i tried.. but login is success as well, but when redirect server cannot permit user.. i guess before redirect(after login success) server has to set access-token to request header but i dont know how to do this.. i'm tried searching but cannot find example. – potato Jan 18 '23 at 00:36

0 Answers0