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
67 stars 18 forks source link

Help needed: TypeScript Error: 'onSuccess' not recognized in HandleAuthOptions #110

Closed mouise1111 closed 1 month ago

mouise1111 commented 1 month ago

TypeScript Error: 'onSuccess' not recognized in HandleAuthOptions

Description

When using the handleAuth function from @workos-inc/authkit-nextjs, I'm encountering a TypeScript error during the build process (npm run build). The error occurs when trying to add custom logic to handle successful authentication.

Current Code


import { handleAuth } from '@workos-inc/authkit-nextjs';
import { redirect } from 'next/navigation';
import { isProfileComplete } from '@/app/actions/userActions';

export const GET = handleAuth({
  async onSuccess(user) {
    const profileComplete = await isProfileComplete();
    if (profileComplete) {
      redirect('/');
    } else {
      redirect('/user');
    }
  },
});

Error Message

Type error: Object literal may only specify known properties, and 'onSuccess' does not exist in type 'HandleAuthOptions'.

Additional Context

This code works locally but fails during the build process. The previous implementation without custom logic worked fine:

export const GET = handleAuth({returnPathname: '/user'});

Any guidance on how to properly type the onSuccess callback or alternative approaches to achieve this functionality would be greatly appreciated.

PaulAsjes commented 1 month ago

This isn't a TypeScript error but a syntax error. handleAuth only accepts one property in its options object, and onSuccess isn't it.

If you want to add custom logic to the callback handler, you can do something like this:

import { handleAuth } from "@workos-inc/authkit-nextjs";
import { NextRequest } from "next/server";

export async function GET(request: NextRequest) {
  const response = await handleAuth()(request);

  // Do custom logic here

  // Return the response or redirect
  return response;  
}