fief-dev / fief-js

Fief client for JavaScript
https://docs.fief.dev/integrate/javascript/
MIT License
8 stars 2 forks source link

In NextJS, client-side, there is no way to distinguish between not-yet authenticated and unauthenticated #8

Open SaraBMo opened 7 months ago

SaraBMo commented 7 months ago

Describe the bug

In NextJS, client-side, you cannot distinguish between not-yet authenticated and unauthenticated. Client-side, you are using the current user and token hooks populated by FiefAuthProvider. Problem is, on page load, you get null values, that will either get populated with the user info when the request to /current-user resolves and fills in the context, or simply never be changed if the user is unauthenticated. That causes issues as we use fief to authenticate our external api calls, and is inelegant for showing user information (Flashes log in before showing user info, instead of a loading state).

The solution I propose is simple, but can be discussed of course : have the context provide an initial value to indicate loading, and only go null when we know user is unauthenticated.

Related to : https://github.com/orgs/fief-dev/discussions/292

To Reproduce

Steps to reproduce the behavior:

  1. Setup a Nexjs project with fief.
  2. Get authenticated.
  3. Reload the page and observe current user and token, they start null, then are populated.

Expected behavior

An authenticated user first loading the page should be distinguishable from an unauthenticated user.

Configuration

frankie567 commented 7 months ago

Hi @SaraBMo :wave:

That's indeed a limitation we've identified. We talk about it in the following discussion: https://github.com/orgs/fief-dev/discussions/292

I've a few ideas to improve this.

tito commented 7 months ago

@frankie567 @SaraBMo and i are working on the same project using fief-js; as you were asking for help, maybe you could expose the idea you got to improve it ?

matteius commented 7 months ago

Came here tonight to report the same thing 👍

DoWhileGeek commented 7 months ago

I'm running into this issue as well 💀

frankie567 commented 7 months ago

I've started to work on this here: https://github.com/fief-dev/fief-js/pull/10

This will also be the occasion to spin up a complete example for Next.js App Router.

le-dav commented 5 months ago

+1 on this issue, I'm struggling a bit to handle Fief with the app routing, as I'm unable to retrieve the access token despite being authenticated Happy to see it's gonna be taken care of!