Skip to content

Commit

Permalink
Improve nextjs cookie usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Jan 30, 2024
1 parent 423a94e commit 6784539
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ await userData = await this.getUserIdentity();
```

## Advanced
Curious exactly how these methods work and when they should be used? We have some advanced guidance available for each of the methods on the [method documentation](./docs/advanced.md).
Curious exactly how these methods work and when they should be used? We have some advanced guidance available for each of the methods on the:

[Method documentation](./docs/advanced.md)

## Contributing

Expand Down
7 changes: 3 additions & 4 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ Navigate the user to the profile screen to configure their MFA options. A common

Make sure to set `use client` when using the the @authress/login SDK, and make sure to `use server` when using the @authress/sdk. These are specific to login and authorization calls.

For server components, when you would like to check if the user is logged in, the `authorization` cookie is available:
For server components, when you would like to check if the user is logged in, the `authorization` cookie is available. How to do this is going to depend explicitly on the type of Server Side context you have. Next.js has different functionality for each of `SSR App Routing`, `ServerComponents`, `Server Actions`, and `API Requests`. For more details about how to explicitly do it in the your example, check out the [Next.js cookie guide](https://nextjs.org/docs/app/api-reference/functions/cookies) and the [Next.js cookie library](https://github.com/andreizanik/cookies-next?tab=readme-ov-file#client-and-server).


```jsx
import { cookies } from 'next/headers';
import { hasCookie } from 'cookies-next';

export default function Page() {
const cookieStore = cookies();
const userIsLoggedIn = !!cookieStore.has('authorization');
const userIsLoggedIn = !!hasCookie('authorization');
if (userIsLoggedIn) {
return <Page />;
}
Expand Down

0 comments on commit 6784539

Please sign in to comment.