stackernews / stacker.news

Internet communities that pay you Bitcoin
https://stacker.news
MIT License
434 stars 112 forks source link

Refresh JWTs for account switching #1596

Open ekzyis opened 4 hours ago

ekzyis commented 4 hours ago

Describe the problem you're trying to solve

The JWT stored in the multi_auth cookies are never refreshed. This means they expire in 30 days.

This could be the reason for #1573 and why account switching stops working after some time. It would at least make sense.

Currently, account switching probably just completely stops working and you need to clear cookies to recover. This is pretty bad and something I didn't really make sure fails gracefully in #489.

Describe the solution you'd like

Refresh all JWTs stored multi_auth cookies on every request—or at least the one that is currently used since we always use the multi_auth cookie if the pointer cookie is set.

Describe alternatives you've considered

Fail gracefully if JWTs are expired

Additional context

No response

ekzyis commented 4 hours ago

Maybe we can refresh all JWTs at the same time we're checking them which user we should use.

We do this here:

https://github.com/stackernews/stacker.news/blob/8c43caed80b38b7450796df5a35bea196ed1845a/pages/api/graphql.js#L85-L123

and we use encode from next-auth/jwt to create new JWTs. See here:

https://github.com/stackernews/stacker.news/blob/8c43caed80b38b7450796df5a35bea196ed1845a/pages/api/auth/%5B...nextauth%5D.js#L94-L103

encode was imported as encodeJWT here