Next.js Discord

Discord Forum

Firebase Credential Auth - Next Auth

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
I'm using Next Auth, and I was wondering how I would use the credentials provider with firebase?

Current code:

import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import FacebookProvider from "next-auth/providers/facebook";
import CredentialsProvider from "next-auth/providers/credentials";
import EmailProvider from "next-auth/providers/email";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
import { cert } from "firebase-admin/app";

export default NextAuth({
  adapter: FirestoreAdapter({
    credential: cert({
      projectId: process.env.FIREBASE_PROJECT_ID,
      clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
      privateKey: process.env.FIREBASE_PRIVATE_KEY,
    }),
  }),
  providers: [
    
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    }),
    FacebookProvider({
      clientId: process.env.FACEBOOK_CLIENT_TOKEN as string,
      clientSecret: process.env.FACEBOOK_CLIENT_SECRET as string,
    }),
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: { label: "Email", type: "email" },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        const { email, password } = credentials as {
          email: string;
          password: string;
        };

        const user = { id: "1", name: "J Smith", email: "jsmith@example.com" };

        if (user) {
          return user;
        } else {
          return null;
        }
      },
    }),
  ],
  pages: {
    signIn: "/login",
  },
});
Answered by dracovisk_
2. Using as JSON credentials file path
import admin from "firebase-admin"
import credentials from "@/serviceAccount.json"

export const adminApp = admin.initializeApp({
  credential: admin.credential.cert(credentials as any)
})

export const getUsers = async () => {
  const result = await adminApp.auth().listUsers(10)
  return result
}

export const verifyToken = async (token: any) => {
  const result = await adminApp.auth().verifyIdToken(token)

  return result
}
View full answer

4 Replies

1.

import * as admin from "firebase-admin";
import { getApps, getApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";

const firebaseServiceAccount: { [key: string]: string } | null = process.env
  .SERVER_FIREBASE_SERVICE_ACCOUNT
  ? JSON.parse(process.env.SERVER_FIREBASE_SERVICE_ACCOUNT)
  : null;

export const firebaseClientConfig = {
  apiKey: process.env.CLIENT_FIREBASE_WEB_API_KEY,
  projectId: firebaseServiceAccount?.project_id,
  authDomain: `${firebaseServiceAccount?.project_id}.firebaseapp.com`,
  storageBucket: `${firebaseServiceAccount?.project_id}.appspot.com`,
};

let adminApp;

if (firebaseServiceAccount) {
  adminApp =
    getApps().length === 0
      ? admin.initializeApp({
          credential: admin.credential.cert({ ...firebaseServiceAccount }),
        })
      : getApp();
}

if (!adminApp) {
  throw new Error("Firebase application could not initialized!");
}
2. Using as JSON credentials file path
import admin from "firebase-admin"
import credentials from "@/serviceAccount.json"

export const adminApp = admin.initializeApp({
  credential: admin.credential.cert(credentials as any)
})

export const getUsers = async () => {
  const result = await adminApp.auth().listUsers(10)
  return result
}

export const verifyToken = async (token: any) => {
  const result = await adminApp.auth().verifyIdToken(token)

  return result
}
Answer