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

header options #9579

Open
dineshreddypaidi opened this issue Oct 28, 2024 · 1 comment
Open

header options #9579

dineshreddypaidi opened this issue Oct 28, 2024 · 1 comment

Comments

@dineshreddypaidi
Copy link

iam working with token autentication
for some of endpoint need auth token and need to be included in header options..
but in static web browser doesnot show any of header options and for every endpoint iam getting an auth token error
i checked endpoints with other clients postman. it is working fine..
can add a feature for posting header option also to the static web browser views

@Dhruv-Bajaj-code
Copy link

Dhruv-Bajaj-code commented Nov 10, 2024

After receiving the token from the backend, it should be saved in the browser's local storage for future authentication, as follows:

const authToken = response.headers.get('Authorization');
if (authToken) {
    localStorage.setItem('authToken', authToken);
}

Now, with every request, you will need to include the token in the header. You can do it as follows:

function fetchWithAuth(url, options = {}) {
    const authToken = localStorage.getItem('authToken'); 

    
    const headers = {
        ...options.headers,
        'Authorization': authToken ? authToken : '',
        'Content-Type': 'application/json', 
    };

    /
    return fetch(url, {
        ...options,
        headers,
    });
}

Both are parts of the frontend.

please let me know if this worked.

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

2 participants