Skip to content

Commit e82e7b3

Browse files
authored
fix: Login - fix /api/v2/user cache race condition (#47)
While I was testing the Sign-out functionality in #46 , I found a bug on login. Sometimes, intermittently, the login wouldn't 'take' even though the login POST was successful and the subsequent GET to `/api/v2/user` was successful - the user would still be brought to the `/login` screen a second time: ![2022-01-21 19 52 31](https://user-images.githubusercontent.com/88213859/150623831-7ce33fca-97d5-4ea8-8301-9149def1ee40.gif) ^ I log-in, and then the login screen just resets, and logging in a second time works 🤔 I tracked this down and found it is a new race condition that I inadvertently introduced when I migrated to SWR in #46. Because SWR caches responses based on the API path - we have to invalidate the cache for `/api/v2/user` when the user logins, otherwise we'll continue to serve the user-not-found error until SWR decides to retry. However, we weren't waiting for that cache-invalidation to go through - so there was the chance that an `/api/v2/user` request was still in-flight while we were redirecting after a successful login. If the request made it back prior to the redirect - login would work on the first try. If the request took longer - SWR would serve the stale, erroneous user from the cache. Luckily the fix is simple - `mutate` in the SWR API returns a promise, so we can just wait for that to go through before redirecting. Unfortunately, though, this is tricky to exercise in a unit test because with the SWR model, the login logic is spread between `_app.tsx`, `UserContext.tsx`, and `SignInForm.tsx`, and the race condition involves an integration of all of those places. I found that there is a lint rule that can help protect us from making this mistake - [`typescript-eslint/no-floating-promises`](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/no-floating-promises.md) (it's a lint rule we wanted to turn on in `coder/m`, too, actually - just a lot of work to clean up the code for it!), so I turned it on as part of this PR, and it caught a couple extra places to check.
1 parent 4183a4e commit e82e7b3

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

.eslintrc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ rules:
3838
"@typescript-eslint/explicit-function-return-type": "off"
3939
"@typescript-eslint/explicit-module-boundary-types": "error"
4040
"@typescript-eslint/method-signature-style": ["error", "property"]
41+
"@typescript-eslint/no-floating-promises": error
4142
"@typescript-eslint/no-invalid-void-type": error
4243
# We're disabling the `no-namespace` rule to use a pattern of defining an interface,
4344
# and then defining functions that operate on that data via namespace. This is helpful for

site/components/SignIn/SignInForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ export const SignInForm: React.FC<SignInProps> = ({
8787
try {
8888
await loginHandler(email, password)
8989
// Tell SWR to invalidate the cache for the user endpoint
90-
mutate("/api/v2/user")
91-
router.push("/")
90+
await mutate("/api/v2/user")
91+
await router.push("/")
9292
} catch (err) {
9393
helpers.setFieldError("password", "The username or password is incorrect.")
9494
}

site/contexts/UserContext.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export const useUser = (redirectOnError = false): UserContext => {
2323
const requestError = ctx.error
2424
useEffect(() => {
2525
if (redirectOnError && requestError) {
26-
router.push({
26+
// 'void' means we are ignoring handling the promise returned
27+
// from router.push (and lets the linter know we're OK with that!)
28+
void router.push({
2729
pathname: "/login",
2830
query: {
2931
redirect: router.asPath,

0 commit comments

Comments
 (0)