|
1 | 1 | --- |
2 | | -slug: sdk/use-user |
| 2 | +title: useUser |
3 | 3 | --- |
4 | 4 |
|
5 | | -`useUser` is a hook that returns the user object if the user is authenticated; otherwise, it returns `null` by default. However, if you pass in `{ or: "redirect" }` or `{ or: "throw" }` as an option, it will redirect to the login page or throw an error respectively when the user is not authenticated. |
| 5 | +# useUser |
6 | 6 |
|
7 | | -If you want to learn more about the `User` object, check out the [User](./current-user.mdx) documentation. |
| 7 | +A React hook that retrieves the current user information. It can return different user types based on the provided options. |
8 | 8 |
|
9 | | -## Default Usage |
| 9 | +## Parameters |
10 | 10 |
|
11 | | -Check if the user is authenticated and display the user's name. |
12 | | -```jsx |
13 | | -import { useUser } from "@stackframe/stack"; |
| 11 | +- `options` (optional): `GetUserOptions` - Options for retrieving the user. |
| 12 | + - `or` (optional): `'redirect' | 'throw'` - Specifies behavior if user is not found. |
| 13 | + - `projectIdMustMatch` (optional): `'internal'` - Ensures the project ID matches 'internal'. |
14 | 14 |
|
15 | | -function MyComponent() { |
16 | | - const user = useUser(); // user can be null |
17 | | - if (!user) { |
18 | | - return <div>Not authenticated</div>; |
19 | | - } |
20 | | - return <div>Hello, {user.name}</div>; |
21 | | -} |
22 | | -``` |
| 15 | +## Returns |
23 | 16 |
|
24 | | -## Redirect when not authenticated |
25 | | -By passing `{ or: "redirect" }` to the hook, it will redirect to the login page if the user is not authenticated. You can configure the login page in the StackApp component. |
26 | | -```jsx |
27 | | -import { useUser } from "@stackframe/stack"; |
| 17 | +- `CurrentUser | CurrentInternalUser | null` depending on the options provided. |
28 | 18 |
|
29 | | -function MyComponent() { |
30 | | - const user = useUser({ or: "redirect" }); // user is garanteed to be non-null |
31 | | - return <div>Hello, {user.name}</div>; |
32 | | -} |
33 | | -``` |
| 19 | +## Examples |
34 | 20 |
|
35 | | -The same hook can also be used to protect a page. (You might also want to check out the server-side version [here](../getting-started/users.mdx)) |
36 | | -```jsx |
37 | | -import { useUser } from "@stackframe/stack"; |
| 21 | +```tsx |
| 22 | +import { useUser } from '@stackframe/stack'; |
| 23 | + |
| 24 | +// Basic usage |
| 25 | +const user = useUser(); |
| 26 | + |
| 27 | +// With options |
| 28 | +const internalUser = useUser({ projectIdMustMatch: 'internal' }); |
| 29 | + |
| 30 | +// With redirect or throw option |
| 31 | +const authenticatedUser = useUser({ or: 'redirect' }); |
| 32 | + |
| 33 | +// Combining options |
| 34 | +const authenticatedInternalUser = useUser({ |
| 35 | + or: 'throw', |
| 36 | + projectIdMustMatch: 'internal' |
| 37 | +}); |
| 38 | +```import { useUser } from "@stackframe/stack"; |
38 | 39 |
|
39 | 40 | function MyProtectedPage() { |
40 | 41 | useUser({ or: "redirect" }); |
|
0 commit comments