-
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.
This hook leverages swr to get a user's data from Gafaelfawr's /auth/api/v1/user-info endpoint. New dependencies: - swr - unfetch as a polyfill for fetch; this is needed because squared isn't inside a Next.js project that bakes the polyfill in for us.
- Loading branch information
1 parent
7bf9e11
commit b765732
Showing
6 changed files
with
670 additions
and
36 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lsst-sqre/squared': minor | ||
--- | ||
|
||
Add a useGafaelfawrUser hook. |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import useSWR, { Fetcher } from 'swr'; | ||
import fetch from 'unfetch'; | ||
|
||
type GafaelfawrGroup = { | ||
name: string; | ||
id?: number; | ||
}; | ||
|
||
type GafaelfawrApiQuota = { | ||
[key: string]: number; | ||
}; | ||
|
||
type GafaelfawrNotebookQuota = { | ||
cpu: number; | ||
memory: number; | ||
}; | ||
|
||
type GafaelfawrQuota = { | ||
api: GafaelfawrApiQuota; | ||
notebook: GafaelfawrNotebookQuota; | ||
}; | ||
|
||
type GafaelfawrUser = { | ||
username: string; | ||
name?: string; | ||
email?: string; | ||
uid?: number; | ||
gid?: number; | ||
groups?: GafaelfawrGroup[]; | ||
quota?: GafaelfawrQuota; | ||
}; | ||
|
||
const fetcher: Fetcher<GafaelfawrUser, string> = (url: string) => | ||
fetch(url).then((res) => res.json()); | ||
|
||
/** | ||
* A React hook for getting data from Gafaelfawr's `/auth/user-info` endpoint | ||
* and establishing in general whether the user is logged in. | ||
*/ | ||
const useGafaelfawrUser = () => { | ||
const { data, error, isLoading, isValidating } = useSWR( | ||
'/auth/api/v1/user-info', | ||
fetcher | ||
); | ||
|
||
const isLoggedIn = !error && data && data.hasOwnProperty('username'); | ||
|
||
return { | ||
user: data, | ||
isLoading, | ||
isValidating, | ||
isLoggedIn, | ||
error, | ||
}; | ||
}; | ||
|
||
export default useGafaelfawrUser; | ||
export type { GafaelfawrUser }; |
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,8 @@ | ||
/* Components */ | ||
export { Button, type ButtonProps } from './Button'; | ||
|
||
/* Hooks */ | ||
export { | ||
default as useGafaelfawrUser, | ||
type GafaelfawrUser, | ||
} from './hooks/useGafaelfawrUser'; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.