-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #574 from logto-io/gao-add-react-query-smaple
feat: add react-query sample
- Loading branch information
Showing
14 changed files
with
248 additions
and
40 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,8 @@ body { | |
button { | ||
cursor: pointer; | ||
} | ||
|
||
li { | ||
text-align: left; | ||
margin: 0.5rem 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useLogto } from '@logto/react'; | ||
import { useQuery, QueryClient, QueryClientProvider } from 'react-query'; | ||
|
||
import { endpoint } from '../../consts'; | ||
|
||
/** | ||
* A custom hook that returns a fetcher function that automatically adds the access token to the | ||
* request. It uses `fetch` under the hood, and has the same signature. | ||
*/ | ||
const useApi = () => { | ||
const { getAccessToken } = useLogto(); | ||
const fetcher: typeof fetch = async (input, init) => { | ||
const accessToken = await getAccessToken(); | ||
|
||
if (!accessToken) { | ||
throw new Error('No access token available.'); | ||
} | ||
|
||
const response = await fetch(input, { | ||
...init, | ||
headers: { | ||
...init?.headers, | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
return response; | ||
}; | ||
|
||
return fetcher; | ||
}; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
const ReactQuery = () => { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<Content /> | ||
</QueryClientProvider> | ||
); | ||
}; | ||
|
||
const Content = () => { | ||
const api = useApi(); | ||
const query = useQuery('userinfo', async () => { | ||
const response = await api(new URL('/oidc/me', endpoint)); | ||
return response.json(); | ||
}); | ||
|
||
if (query.isLoading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
if (query.isError) { | ||
return ( | ||
<div> | ||
<h1>Error</h1> | ||
<p>{JSON.stringify(query.error)}</p> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div> | ||
<h1>Success</h1> | ||
<p>You are seeing this page because react-query successfully fetched the user info.</p> | ||
<p>{JSON.stringify(query.data)}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ReactQuery; |
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
Oops, something went wrong.