Skip to content

Commit 493166e

Browse files
author
patched.codes[bot]
committed
Patched /Users/codelion/Documents/GitHub/stack/docs/fern/docs/pages/sdk/use-user.mdx
1 parent 0fd8017 commit 493166e

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

docs/fern/docs/pages/sdk/use-user.mdx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
---
2-
slug: sdk/use-user
2+
title: useUser
33
---
44

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
66

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.
88

9-
## Default Usage
9+
## Parameters
1010

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'.
1414

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
2316

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.
2818

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
3420

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";
3839
3940
function MyProtectedPage() {
4041
useUser({ or: "redirect" });

0 commit comments

Comments
 (0)