#Firebase Credential Auth - Next Auth

6 messages · Page 1 of 1 (latest)

stiff sable
#

I'm using Next Auth, and I was wondering how I would use the credentials provider with firebase?

Current code:

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: "[email protected]" };

        if (user) {
          return user;
        } else {
          return null;
        }
      },
    }),
  ],
  pages: {
    signIn: "/login",
  },
});
left compassBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
zinc tendon
#
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!");
}
#
  1. 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
}
stiff sable
#

Nevermind, thank you for this!