-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set default token dynamically in the browser
- Loading branch information
1 parent
4d181ec
commit fa6b682
Showing
6 changed files
with
24 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,35 @@ | ||
import auth from "panoptes-client/lib/auth"; | ||
import { useState } from "react"; | ||
import auth from "panoptes-client/lib/auth" | ||
import { useState } from "react" | ||
|
||
const isBrowser = typeof window !== "undefined"; | ||
const isBrowser = typeof window !== "undefined" | ||
|
||
if (isBrowser) { | ||
auth.checkCurrent(); | ||
} | ||
let defaultToken | ||
/* | ||
Top-level await in modules has been supported in Node | ||
and in all browsers since 2021. However, ES modules are still | ||
not supported in the monorepo. An immediately-invoked async | ||
function is a workaround when top-level await is not supported. | ||
https://v8.dev/features/top-level-await | ||
*/ | ||
(async function getDefaultToken() { | ||
defaultToken = null | ||
if (isBrowser) { | ||
await auth.checkCurrent() | ||
defaultToken = await auth.checkBearerToken() | ||
} | ||
})() | ||
|
||
export default function usePanoptesAuthToken() { | ||
const [token, setToken] = useState(null); | ||
const [token, setToken] = useState(defaultToken) | ||
|
||
async function fetchPanoptesAuthToken() { | ||
await auth.checkCurrent(); | ||
const newToken = await auth.checkBearerToken(); | ||
await auth.checkCurrent() | ||
const newToken = await auth.checkBearerToken() | ||
if (newToken !== token) { | ||
setToken(newToken); | ||
setToken(newToken) | ||
} | ||
} | ||
|
||
fetchPanoptesAuthToken(); | ||
return token; | ||
fetchPanoptesAuthToken() | ||
return token | ||
} |
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 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