workos / authkit-nextjs

The WorkOS library for Next.js provides convenient helpers for authentication and session management using WorkOS & AuthKit with Next.js.
MIT License
68 stars 19 forks source link

Expecting to be able to call `signOut` from route handler #99

Closed tungstenian closed 1 month ago

tungstenian commented 1 month ago

In my nextjs project (specifically next-on-pages running on cloudflare), I am using the app router and have the following file at app/sign-out/page.tsx

'use server';

import { signOut } from '@workos-inc/authkit-nextjs';

export default async function Page() {
  await signOut();
}

My intent is to be able to route people to https://example.com/sign-out to execute the functionality needed to clear their session.

However, when visiting this page I get the following error.

Unhandled Runtime Error
Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options
PaulAsjes commented 1 month ago

In this case you'd want to define a route rather than a page since you aren't returning any elements.

If you rename that file to app/sign-out/route.ts and use the following it should work as expected:

import { signOut } from "@workos-inc/authkit-nextjs";

export const GET = async () => {
  await signOut();
};