-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(frontend): refreshToken before graphql request (#192)
* fix(frontend): update rereshToken * wup * clear token * fix: auth * fix * remove logs * remove logs
- Loading branch information
Lionel
authored
Nov 23, 2020
1 parent
994d326
commit 1d98dfc
Showing
10 changed files
with
119 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { errorExchange, makeOperation } from "@urql/core"; | ||
import { authExchange } from "@urql/exchange-auth"; | ||
import { auth, getToken, isTokenExpired, setToken } from "src/lib/auth/token"; | ||
|
||
import { request } from "../request"; | ||
|
||
export function customAuthExchange(ctx) { | ||
return authExchange({ | ||
addAuthToOperation: function addAuthToOperation({ authState, operation }) { | ||
if (!authState?.token) { | ||
return operation; | ||
} | ||
const fetchOptions = | ||
typeof operation.context.fetchOptions === "function" | ||
? operation.context.fetchOptions() | ||
: operation.context.fetchOptions || {}; | ||
|
||
return makeOperation(operation.kind, operation, { | ||
...operation.context, | ||
fetchOptions: { | ||
...fetchOptions, | ||
headers: { | ||
...fetchOptions.headers, | ||
Authorization: `Bearer ${authState.token}`, | ||
}, | ||
}, | ||
}); | ||
}, | ||
|
||
didAuthError: ({ error }) => { | ||
// check if the error was an auth error (this can be implemented in various ways, e.g. 401 or a special error code) | ||
return error.graphQLErrors.some( | ||
(e) => e.extensions?.code === "invalid-jwt" | ||
); | ||
}, | ||
|
||
getAuth: async ({ authState }) => { | ||
// for initial launch, fetch the auth state from storage (local storage, async storage etc) | ||
console.log("getAuth", { authState }); | ||
if (!authState) { | ||
const token = getToken() || (await auth(ctx)); | ||
if (token) { | ||
return { token: token.jwt_token }; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* the following code gets executed when an auth error has occurred | ||
* we should refresh the token if possible and return a new auth state | ||
* If refresh fails, we should log out | ||
**/ | ||
|
||
// if your refresh logic is in graphQL, you must use this mutate function to call it | ||
// if your refresh logic is a separate RESTful endpoint, use fetch or similar | ||
setToken(null); | ||
const result = await auth(ctx); | ||
console.log({ result }); | ||
if (result?.jwt_token) { | ||
// return the new tokens | ||
return { token: result.jwt_token }; | ||
} | ||
|
||
return null; | ||
}, | ||
|
||
willAuthError: ({ authState }) => { | ||
// e.g. check for expiration, existence of auth etc | ||
if (!authState || isTokenExpired()) return true; | ||
return false; | ||
}, | ||
}); | ||
} | ||
|
||
export function customErrorExchange() { | ||
return errorExchange({ | ||
onError: (error) => { | ||
const { graphQLErrors } = error; | ||
// we only get an auth error here when the auth exchange had attempted to refresh auth and getting an auth error again for the second time | ||
const isAuthError = graphQLErrors.some( | ||
(e) => e.extensions?.code === "invalid-jwt" | ||
); | ||
if (isAuthError) { | ||
// clear storage, log the user out etc | ||
// your app logout logic should trigger here | ||
console.log("errorExchange", "logout"); | ||
request("/api/logout", { | ||
credentials: "include", | ||
mode: "same-origin", | ||
}); | ||
} | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3447,7 +3447,7 @@ | |
"@typescript-eslint/types" "4.4.0" | ||
eslint-visitor-keys "^2.0.0" | ||
|
||
"@urql/core@^1.15.1": | ||
"@urql/core@>=1.14.0", "@urql/core@^1.15.1": | ||
version "1.15.1" | ||
resolved "https://registry.yarnpkg.com/@urql/core/-/core-1.15.1.tgz#fa49909f2841d092796dd540cef6e9df89222560" | ||
integrity sha512-a05ablx/aKNCUc9dEbx0GvE28UC0sJ1FmfsSfmJNqecYlYeb4XvSQW4FLVy0e/MjQeB9op/weiVIEw+za2ssGw== | ||
|
@@ -3462,6 +3462,14 @@ | |
dependencies: | ||
wonka ">= 4.0.9" | ||
|
||
"@urql/exchange-auth@^0.1.2": | ||
version "0.1.2" | ||
resolved "https://registry.yarnpkg.com/@urql/exchange-auth/-/exchange-auth-0.1.2.tgz#67a76ef78ab4ea9dc51c050d1b79362e9abbffc6" | ||
integrity sha512-SIrcnbom+nro0pALmqQmcMxUcy8qZUoykoPs5LezYVPmvZXx4ErXwly58g0XoU4eX10U2gakjCFPTyleF8Nt6Q== | ||
dependencies: | ||
"@urql/core" ">=1.14.0" | ||
wonka "^4.0.14" | ||
|
||
"@vercel/[email protected]", "@vercel/ncc@^0.24.1": | ||
version "0.24.1" | ||
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.24.1.tgz#3ea2932c85ba87f4de6fe550d60e1bf5c005985e" | ||
|