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

Bug Report: User login requires refresh to fetch data #1498

Closed
kanhaiya04 opened this issue Jan 25, 2024 · 4 comments
Closed

Bug Report: User login requires refresh to fetch data #1498

kanhaiya04 opened this issue Jan 25, 2024 · 4 comments
Assignees
Labels
bug Something isn't working feature request no-issue-activity No issue activity

Comments

@kanhaiya04
Copy link
Contributor

Describe the bug
After logging as user we need to refresh the page to fetch the data.

To Reproduce
Steps to reproduce the behavior:

  1. Login as user
  2. You will notice no data is present, showing undefined.

Expected behavior
Data of the user should be fetched after successful login.

Actual behavior
Data is not fetched properly after the login, need to refresh to the page to get the data.

Screenshots
If applicable, add screenshots to help explain your problem.

Screencast.from.26-01-24.12.49.00.AM.IST.mp4

Additional details
Add any other context or screenshots about the feature request here.

Potential internship candidates
Please read this if you are planning to apply for a Palisadoes Foundation internship PalisadoesFoundation/talawa#359

@kanhaiya04 kanhaiya04 added the bug Something isn't working label Jan 25, 2024
@kanhaiya04
Copy link
Contributor Author

@palisadoes Please assign me this issue.

@kanhaiya04
Copy link
Contributor Author

After debugging the issue, I found the reason why this is happening.

The header for the HTTP server is not set with the token after a successful login.

const httpLink = new HttpLink({ uri: BACKEND_URL, headers: { authorization: 'Bearer ' + localStorage.getItem('token') || '', }, });

This piece of code in index.js sets the header with the token initially as null when the client starts, but the header value dynamically does not change as the value changes in local storage.

That's why it needs a refresh after login to fetch the token from local storage and set the header.

To fix this, we have a few options:

First: Use the setContext function from ApolloClient to update the header dynamically as the value changes in local storage. It requires a few changes in how the ApolloClient is created in the index.js file.

Second: Pass the header along with the request. Example:
const { data: userData, error: errorUser, }: { data: InterfaceUserType | undefined; loading: boolean; error?: Error | undefined; } = useQuery(USER_ORGANIZATION_LIST, { variables: { id: localStorage.getItem('id') }, context: { headers: { authorization: Bearer ${localStorage.getItem('token')} }, }, });

Third:: use window.location.replace('url') after successful login to redirect the user.

@palisadoes I'd appreciate your view on this.

@palisadoes
Copy link
Contributor

Open a PR. Discuss your approach with the reviewers

Copy link

This issue did not get any activity in the past 10 days and will be closed in 180 days if no update occurs. Please check if the develop branch has fixed it and report again or close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature request no-issue-activity No issue activity
Projects
None yet
Development

No branches or pull requests

3 participants