NextAuth + TwitterProvider + FirestoreAdapter => Missing Screen Name

Viewed 26

I'm using next-auth along with next-auth/firebase-adapter packages to log to my web app via Twitter. The flow is working nicely, but I can not obtain the user's screenname (aka handle).

Before using next-auth I've been using Firebase auth(). In their package, the user's screen name has always been available.

I've been playing around with NextAuthOptions but to no avail. Does anyone know how to make add the user's screenname to the session?

export const authOptions: NextAuthOptions = {
    session: {
        strategy: 'jwt',
    },
    adapter: FirestoreAdapter(firebaseConfig),
    pages: {
        signIn: '/sign-in',
    },
    callbacks: {
        session: ({ session, token }) => {
            return {
                ...session,
                user: {
                    ...session.user,
                    id: token.sub!,
                },
            }
        },
    },
    events: {
        createUser: async ({ user }) => {
            const ref = admin
                .firestore()
                .collection(Constants.UsersTable)
                .doc(user.id)

            await ref.set(
                {
                    screenName: <MISSING>,
                    daily_target: 0,
                    onboarded: false,
                },
                { merge: true }
            )
        },
    },
    providers: [
        TwitterProvider({
            clientId: process.env.TWITTER_AUTH_CLIENTID,
            clientSecret: process.env.TWITTER_AUTH_CLIENTSECRET,
            version: '2.0',
        }),
    ],
}
1 Answers

Instead of obtaining the screenname I decided to obtain the providerAccountToken and add it to my JWT token with a next-auth callback.

This is even better as I use the unique user id instead of a public handle (which the user might change in the future).

    callbacks: {
        jwt: ({ token, account = {} }) => {
            // Persist the OAuth access_token to the token right after signin
            if (account.providerAccountId) {
                token.providerAccountId = account.providerAccountId
            }

            return token
        },
    },
Related