im trying to create a user in the firestore database using nextauth adapter for firebase, but im always unauthenicated when using the adapter and the user is not created in the database, it works when i remove the adapter.

i have tried multiple metodes of creating the adapter but they all have failed

nextauth opions

export const authOptions : NextAuthOptions = {
    pages: {
        signIn: '/auth/login',
    },
    providers: [
        CredentialsProvider({
            name: "Credentials",
            credentials: {  },
            async authorize(credentials) : Promise<any> {
                return await signInWithEmailAndPassword(auth, (credentials as any).email || '', (credentials as any).password || '')
                    .then(userCredential => {
                        if (userCredential.user) {
                            return userCredential.user
                        }
                        return null
                    })
                .catch(error => console.log(error))
            }
        }),
        GoogleProvider({
            clientId: process.env.GOOGLE_CLIENT_ID!,
            clientSecret: process.env.GOOGLE_CLIENT_SECRET!
        }),
        GitHubProvider({
            clientId: process.env.GITHUB_CLIENT_ID!,
            clientSecret: process.env.GITHUB_CLIENT_SECRET!
        }),
        DiscordProvider({
            clientId: process.env.DISCORD_CLIENT_ID,
            clientSecret: process.env.DISCORD_CLIENT_SECRET
        }),
    ],
    /* adapter: FirestoreAdapter({
        credential: cert({
          projectId: process.env.FIREBASE_PROJECT_ID,
          clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
          privateKey: process.env.FIREBASE_PRIVATE_KEY!.replace(/\\n/g, '\n')
        })
      }) */
    // adapter: FirestoreAdapter(db.app)
}

export default NextAuth(authOptions)

it works when i use the google proivder but not when using creds,

code for signup with creds:

const signup = (formdata : FormData) => {
setEmail((formdata.get("email") as string))
setPassword((formdata.get("password") as string))
createUserWithEmailAndPassword(auth, email, password).then(res => {
  console.log(res)
});
router.push("/dashboard")

};

signup with provider: function Signup(provider : string) { signIn(provider) } // provider is the name: google, github, discord

1

There are 1 answers

0
specter On

for anybody that finds this, you can't use adapters with the CredentialsProvider as vagely stated here: https://next-auth.js.org/providers/credentials

you will need to use other providers like google or maybe email,

if this is not what you want, here is an example code for creating a user with firestore where document names are the userid

// signup function

async function signup(formdata : FormData) {
    let user = null
    setEmail((formdata.get("email") as string))
    setPassword((formdata.get("password") as string))
    await createUserWithEmailAndPassword(auth, email, 
    password).then(res => {
      user = res.user
    });
    create_user(user)

    await signIn('credentials', {
      email: email, 
      password: password,
      redirect: true, 
      callbackUrl: '/dashboard'}
    )
  };

// firestore create user document with uid as doc id

export function create_user(user : User) {
    const base_user : ScalarUser = {
        email: user?.email, 
        photoURL: user?.photoURL || "", 
        displayName: user?.displayName ||  "",
    } 
    
    setDoc(doc(db, 'Users', user.uid), base_user, { merge: true })
}