Skip to content

Commit 8225f3c

Browse files
committed
Initial setup
1 parent 779f5be commit 8225f3c

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

docs/reference/hooks/use-auth.mdx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,89 @@ sdk: astro, chrome-extension, expo, nextjs, react, react-router, remix, tanstack
55
---
66

77
<Typedoc src="clerk-react/use-auth" />
8+
9+
## Example
10+
11+
The following example demonstrates how to use the `useAuth()` hook to access the current auth state, like whether the user is signed in or not. It also includes a basic example for using the `getToken()` method to retrieve a session token for fetching data from an external resource.
12+
13+
<If sdk="react">
14+
```tsx {{ filename: 'src/pages/ExternalDataPage.tsx' }}
15+
import { useAuth } from '@clerk/clerk-react'
16+
17+
export default function ExternalDataPage() {
18+
const { userId, sessionId, getToken, isLoaded, isSignedIn } = useAuth()
19+
20+
const fetchExternalData = async () => {
21+
const token = await getToken()
22+
23+
// Fetch data from an external API
24+
const response = await fetch('https://api.example.com/data', {
25+
headers: {
26+
Authorization: `Bearer ${token}`,
27+
},
28+
})
29+
30+
return response.json()
31+
}
32+
33+
if (!isLoaded) {
34+
return <div>Loading...</div>
35+
}
36+
37+
if (!isSignedIn) {
38+
return <div>Sign in to view this page</div>
39+
}
40+
41+
return (
42+
<div>
43+
<p>
44+
Hello, {userId}! Your current active session is {sessionId}.
45+
</p>
46+
<button onClick={fetchExternalData}>Fetch Data</button>
47+
</div>
48+
)
49+
}
50+
```
51+
</If>
52+
53+
<If sdk="nextjs">
54+
```tsx {{ filename: 'app/external-data/page.tsx' }}
55+
'use client';
56+
57+
import { useAuth } from '@clerk/nextjs';
58+
59+
export default function ExternalDataPage() {
60+
const { userId, sessionId, getToken, isLoaded, isSignedIn } = useAuth();
61+
62+
const fetchExternalData = async () => {
63+
const token = await getToken();
64+
65+
// Fetch data from an external API
66+
const response = await fetch('https://api.example.com/data', {
67+
headers: {
68+
Authorization: `Bearer ${token}`,
69+
},
70+
});
71+
72+
return response.json();
73+
};
74+
75+
if (!isLoaded) {
76+
return <div>Loading...</div>;
77+
}
78+
79+
if (!isSignedIn) {
80+
return <div>Sign in to view this page</div>;
81+
}
82+
83+
return (
84+
<div>
85+
<p>
86+
Hello, {userId}! Your current active session is {sessionId}.
87+
</p>
88+
<button onClick={fetchExternalData}>Fetch Data</button>
89+
</div>
90+
);
91+
}
92+
```
93+
</If>

docs/reference/hooks/use-sign-in.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,49 @@ sdk: chrome-extension, expo, nextjs, react, react-router, remix, tanstack-react-
55
---
66

77
<Typedoc src="clerk-react/use-sign-in" />
8+
9+
## Examples
10+
11+
### Check the current state of a sign-in
12+
13+
The following example uses the `useSignIn()` hook to access the [`SignIn`](/docs/reference/javascript/sign-in) object, which contains the current sign-in attempt status and methods to create a new sign-in attempt. The `isLoaded` property is used to handle the loading state.
14+
15+
<If sdk="react">
16+
```tsx {{ filename: 'src/pages/SignInPage.tsx' }}
17+
import { useSignIn } from '@clerk/clerk-react'
18+
19+
export default function SignInPage() {
20+
const { isLoaded, signIn } = useSignIn()
21+
22+
if (!isLoaded) {
23+
// Handle loading state
24+
return null
25+
}
26+
27+
return <div>The current sign-in attempt status is {signIn?.status}.</div>
28+
}
29+
```
30+
</If>
31+
32+
<If sdk="nextjs">
33+
```tsx {{ filename: 'app/sign-in/page.tsx' }}
34+
'use client';
35+
36+
import { useSignIn } from '@clerk/nextjs';
37+
38+
export default function SignInPage() {
39+
const { isLoaded, signIn } = useSignIn();
40+
41+
if (!isLoaded) {
42+
// Handle loading state
43+
return null;
44+
}
45+
46+
return <div>The current sign-in attempt status is {signIn?.status}.</div>;
47+
}
48+
```
49+
</If>
50+
51+
### Create a custom sign-in flow with `useSignIn()`
52+
53+
The `useSignIn()` hook can also be used to build fully custom sign-in flows, if Clerk's prebuilt components don't meet your specific needs or if you require more control over the authentication flow. Different sign-in flows include email and password, email and phone codes, email links, and multifactor (MFA). To learn more about using the `useSignIn()` hook to create custom flows, see the [custom flow guides](/docs/guides/development/custom-flows/overview).

0 commit comments

Comments
 (0)