Skip to content

Commit

Permalink
Set default token dynamically in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourgreens committed Oct 23, 2024
1 parent 4d181ec commit fa6b682
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 46 deletions.
36 changes: 24 additions & 12 deletions packages/lib-user/src/hooks/usePanoptesAuthToken.js
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
}
7 changes: 0 additions & 7 deletions packages/lib-user/src/hooks/usePanoptesMemberships.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { panoptes } from '@zooniverse/panoptes-js'
import auth from 'panoptes-client/lib/auth'
import useSWR from 'swr'

import usePanoptesAuthToken from './usePanoptesAuthToken'

const isBrowser = typeof window !== 'undefined'

const SWROptions = {
revalidateIfStale: true,
revalidateOnMount: true,
Expand All @@ -14,10 +11,6 @@ const SWROptions = {
refreshInterval: 0
}

if (isBrowser) {
auth.checkCurrent()
}

async function fetchMemberships({ query, token }) {
const authorization = `Bearer ${token}`
if (!token) return null
Expand Down
7 changes: 0 additions & 7 deletions packages/lib-user/src/hooks/usePanoptesProjects.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { projects as panoptesProjects } from '@zooniverse/panoptes-js'
import auth from 'panoptes-client/lib/auth'
import useSWR from 'swr'

import usePanoptesAuthToken from './usePanoptesAuthToken'

const isBrowser = typeof window !== 'undefined'

const SWROptions = {
revalidateIfStale: true,
revalidateOnMount: true,
Expand All @@ -14,10 +11,6 @@ const SWROptions = {
refreshInterval: 0
}

if (isBrowser) {
auth.checkCurrent()
}

async function fetchProjects({ query, token }) {
const authorization = token ? `Bearer ${token}` : undefined

Expand Down
6 changes: 0 additions & 6 deletions packages/lib-user/src/hooks/usePanoptesUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import useSWR from 'swr'

import usePanoptesAuthToken from './usePanoptesAuthToken'

const isBrowser = typeof window !== 'undefined'

const SWROptions = {
revalidateIfStale: true,
revalidateOnMount: true,
Expand All @@ -14,10 +12,6 @@ const SWROptions = {
refreshInterval: 0
}

if (isBrowser) {
auth.checkCurrent()
}

async function getUser({ query, token }) {
const authorization = `Bearer ${token}`

Expand Down
7 changes: 0 additions & 7 deletions packages/lib-user/src/hooks/usePanoptesUserGroup.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { panoptes } from '@zooniverse/panoptes-js'
import auth from 'panoptes-client/lib/auth'
import useSWR from 'swr'

import usePanoptesAuthToken from './usePanoptesAuthToken'

const isBrowser = typeof window !== 'undefined'

const SWROptions = {
revalidateIfStale: true,
revalidateOnMount: true,
Expand All @@ -14,10 +11,6 @@ const SWROptions = {
refreshInterval: 0
}

if (isBrowser) {
auth.checkCurrent()
}

async function fetchPanoptesUserGroup({ groupId, token }) {
const authorization = token ? `Bearer ${token}` : undefined

Expand Down
7 changes: 0 additions & 7 deletions packages/lib-user/src/hooks/useStats.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { env } from '@zooniverse/panoptes-js'
import auth from 'panoptes-client/lib/auth'
import useSWR from 'swr'
import usePanoptesAuthToken from './usePanoptesAuthToken'

const defaultEndpoint = '/classifications/users'

const isBrowser = typeof window !== 'undefined'

const SWROptions = {
revalidateIfStale: true,
revalidateOnMount: true,
Expand All @@ -15,10 +12,6 @@ const SWROptions = {
refreshInterval: 0
}

if (isBrowser) {
auth.checkCurrent()
}

function statsHost(env) {
switch (env) {
case 'production':
Expand Down

0 comments on commit fa6b682

Please sign in to comment.