Search code examples
next.jsnext-auth

Auth.js (next-auth) callback on successful login


Is there any auth.js (next-auth) callback that is called after successful login (OAUTH, Magic Link, Credentials)? It can be used for keeping history of login activities, etc.

Thanks


Solution

  • There is a signIn event that can be used perhaps? It is an event sent on a successful sign in you can implement your logic for logs etc. there. Look at implementing NextAuth

    import NextAuth from "next-auth";
    export const {
      auth,
      handlers: {
        GET,
        POST
      },
      signIn,
      signOut,
    } = NextAuth({
      // your pages, callbacks, etc.
      events: {
        async signIn({
          user,
          account,
          profile,
          isNewUser
        }) {
          // your logic here
    
        }
      }
    })

    you can find the documentation here: https://next-auth.js.org/configuration/events