Guides / Signups & Auth

How to Track Signups with NextAuth.js

3 min readยทSignups & Auth

The Problem

NextAuth.js handles your auth, but it doesn't tell you when new users appear. You're logging in to check your database instead of getting a notification on your phone.

Use the signIn callback

NextAuth provides lifecycle callbacks. The signIn callback fires on every login, so you can check for new users by looking at whether their account was just created.

ts
// app/api/auth/[...nextauth]/route.ts
import NextAuth from "next-auth";
import { logit } from "@/lib/logit";

const handler = NextAuth({
  providers: [
    // your providers
  ],
  callbacks: {
    async signIn({ user, account, isNewUser }) {
      if (isNewUser) {
        await logit.now("signups", {
          event: "New user signed up",
          description: user.email ?? "Unknown",
          icon: "๐Ÿ‘ค",
          notify: true,
          tags: { provider: account?.provider ?? "unknown" },
          metadata: { email: user.email, name: user.name },
        });
      }
      return true;
    },
  },
});

export { handler as GET, handler as POST };

Using the events option (v4+)

In NextAuth v4, you can also use the events option which fires after successful operations:

ts
events: {
  async createUser({ user }) {
    await logit.now("signups", {
      event: "New user created",
      description: user.email ?? "Unknown",
      icon: "๐ŸŽ‰",
      notify: true,
      metadata: { userId: user.id, email: user.email },
    });
  },
},

What isNewUser actually means

Note: isNewUser is only available when using a database adapter. Without an adapter, all logins look the same โ€” use the database adapter and the createUser event instead.

Environment

bash
LOGIT_API_KEY=lk_live_xxxxxxxxxxxxxxxx
NEXTAUTH_SECRET=your-secret

Try LogIt free

7-day trial. No credit card required.

Start free