From 6784539b1c98953db20144deab17a600e6d89d83 Mon Sep 17 00:00:00 2001 From: Warren Parad Date: Tue, 30 Jan 2024 16:05:45 +0100 Subject: [PATCH] Improve nextjs cookie usage. --- README.md | 4 +++- docs/advanced.md | 7 +++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 682f4e3..f65c86a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/advanced.md b/docs/advanced.md index 18f6d09..172a71b 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -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 ; }