All skills
firebase avatar

/firebase-auth-basics

@4e28cb3
by firebasefirebase/skills461 stars
102

Guide for setting up and using Firebase Authentication. Use this skill when the user's app requires user sign-in, user management, or secure data access using auth rules.

Use this Skill: https://skilld.dev/gh/firebase/skills/firebase-auth-basics

This session only. Nothing lands on disk.

referencesclient_sdk_web.md

≈1.9k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Firebase Authentication Web SDK

Initialization

First, ensure you have initialized the Firebase App (see firebase-basics skill). Then, initialize the Auth service:

import { getAuth } from "firebase/auth";
import { app } from "./firebase"; // Your initialized Firebase App

const auth = getAuth(app);
export { auth };

Connect to Emulator

If you are running the Authentication emulator (usually on port 9099), connect to it immediately after initialization.

import { getAuth, connectAuthEmulator } from "firebase/auth";

const auth = getAuth();
// Connect to emulator if running locally
if (location.hostname === "localhost") {
  connectAuthEmulator(auth, "http://localhost:9099");
}

Sign Up with Email/Password

import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";

const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
  .then((userCredential) => {
    const user = userCredential.user;
    // ...
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    // ..
  });

Sign In with Google (Popup)

import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";

const auth = getAuth();
const provider = new GoogleAuthProvider();

signInWithPopup(auth, provider)
  .then((result) => {
    // This gives you a Google Access Token. You can use it to access the Google API.
    const credential = GoogleAuthProvider.credentialFromResult(result);
    const token = credential.accessToken;
    // The signed-in user info.
    const user = result.user;
    // ...
  })
  .catch((error) => {
    // Handle Errors here.
    const errorCode = error.code;
    const errorMessage = error.message;
    // ...
  });

[!IMPORTANT] Troubleshooting auth/unauthorized-domain: If the popup opens and immediately closes with error [firebase_auth/unauthorized-domain], it means the domain hosting your app is not authorized for OAuth operations in your Firebase project.

  • Fix: Add your domain (e.g., localhost for local testing) to the Authorized Domains list in the Firebase Console (Authentication > Settings > Authorized domains).
  • CRITICAL: Do NOT include the protocol or port number when adding the domain (e.g., use localhost, NOT http://localhost:9090).

Sign In with Facebook (Popup)

import { getAuth, signInWithPopup, FacebookAuthProvider } from "firebase/auth";

const auth = getAuth();
const provider = new FacebookAuthProvider();

signInWithPopup(auth, provider)
  .then((result) => {
    // The signed-in user info.
    const user = result.user;
    // This gives you a Facebook Access Token. You can use it to access the Facebook API.
    const credential = FacebookAuthProvider.credentialFromResult(result);
    const accessToken = credential.accessToken;
  })
  .catch((error) => {
    // Handle Errors here.
  });

Sign In with Apple (Popup)

import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth";

const auth = getAuth();
const provider = new OAuthProvider('apple.com');

signInWithPopup(auth, provider)
  .then((result) => {
    const user = result.user;
    // Apple credential
    const credential = OAuthProvider.credentialFromResult(result);
    const accessToken = credential.accessToken;
  })
  .catch((error) => {
    // Handle Errors here.
  });

Sign In with Twitter (Popup)

import { getAuth, signInWithPopup, TwitterAuthProvider } from "firebase/auth";

const auth = getAuth();
const provider = new TwitterAuthProvider();

signInWithPopup(auth, provider)
  .then((result) => {
    const user = result.user;
    // Twitter credential
    const credential = TwitterAuthProvider.credentialFromResult(result);
    const token = credential.accessToken;
    const secret = credential.secret;
  })
  .catch((error) => {
    // Handle Errors here.
  });

Sign In with GitHub (Popup)

import { getAuth, signInWithPopup, GithubAuthProvider } from "firebase/auth";

const auth = getAuth();
const provider = new GithubAuthProvider();

signInWithPopup(auth, provider)
  .then((result) => {
    const user = result.user;
    const credential = GithubAuthProvider.credentialFromResult(result);
    const token = credential.accessToken;
  })
  .catch((error) => {
    // Handle Errors here.
  });

Sign In with Microsoft (Popup)

import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth";

const auth = getAuth();
const provider = new OAuthProvider('microsoft.com');

signInWithPopup(auth, provider)
  .then((result) => {
    const user = result.user;
    const credential = OAuthProvider.credentialFromResult(result);
    const accessToken = credential.accessToken;
  })
  .catch((error) => {
    // Handle Errors here.
  });

Sign In with Yahoo (Popup)

import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth";

const auth = getAuth();
const provider = new OAuthProvider('yahoo.com');

signInWithPopup(auth, provider)
  .then((result) => {
    const user = result.user;
    const credential = OAuthProvider.credentialFromResult(result);
    const accessToken = credential.accessToken;
  })
  .catch((error) => {
    // Handle Errors here.
  });

Sign In Anonymously

import { getAuth, signInAnonymously } from "firebase/auth";

const auth = getAuth();
signInAnonymously(auth)
  .then(() => {
    // Signed in..
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
  });

Email Link Authentication

1. Send Auth Link

import { getAuth, sendSignInLinkToEmail } from "firebase/auth";

const auth = getAuth();
const actionCodeSettings = {
  // URL you want to redirect back to. The domain must be in the authorized domains list in Firebase Console.
  url: 'https://www.example.com/finishSignUp?cartId=1234',
  handleCodeInApp: true,
};

sendSignInLinkToEmail(auth, email, actionCodeSettings)
  .then(() => {
    // Save the email locally so you don't need to ask the user for it again
    window.localStorage.setItem('emailForSignIn', email);
  })
  .catch((error) => {
    // Error
  });

2. Complete Sign In (on landing page)

import { getAuth, isSignInWithEmailLink, signInWithEmailLink } from "firebase/auth";

const auth = getAuth();

if (isSignInWithEmailLink(auth, window.location.href)) {
  let email = window.localStorage.getItem('emailForSignIn');
  if (!email) {
    email = window.prompt('Please provide your email for confirmation');
  }

  signInWithEmailLink(auth, email, window.location.href)
    .then((result) => {
      window.localStorage.removeItem('emailForSignIn');
      // You can check result.user
    })
    .catch((error) => {
      // Error
    });
}

Observe Auth State

Recommended way to get the current user. This listener triggers whenever the user signs in or out.

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

Sign Out

import { getAuth, signOut } from "firebase/auth";

const auth = getAuth();
signOut(auth).then(() => {
  // Sign-out successful.
}).catch((error) => {
  // An error happened.
});

Source: SKILL.md on GitHub

No alerts2d5 checks · Risk SAFE
  • Gen Agent Trust Hub2d

    This skill provides a comprehensive and secure guide for integrating Firebase Authentication into Web, Android, iOS, and Flutter applications. It correctly guides users through using official Firebase CLI tools and SDKs, provides essential troubleshooting for common OAuth issues, and emphasizes the importance of using Firebase Security Rules to protect data. No malicious patterns or security risks were detected.

  • Socket2d

    No alerts

  • Snyk2d

    Risk: LOW · No issues

  • Runlayer6mo

    3 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 4e28cb3. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 days ago.

Activeupdated last week
metadata
{
  "category": "Identity"
}
Other metadata
compatibility
This skill is best used with the Firebase CLI, but does not require it. Firebase CLI can be accessed through `npx -y firebase-tools@latest`.
  • Auth
  • firebase
  • authentication
  • web
  • flutter
  • android
  • oauth
  • jwt
  • firestore
  • security-rules

README badge

README badge for firebase/skills/firebase-auth-basics

Provides guidance for setting up Firebase Authentication, including user management, identity providers (email/password, Google Sign-In, phone, anonymous, custom), token handling, and security rules. Use this when building sign-in flows or securing data access with auth rules in Firebase projects.

Generated from the current SKILL.md.

Does this skill require the Firebase CLI?
No, but it is recommended. The Firebase CLI can be accessed via `npx -y firebase-tools@latest` and is needed to deploy auth configuration changes.
Which identity providers can be enabled via CLI?
Only Google Sign In, anonymous auth, and email/password auth can be configured through the CLI. Other providers (Facebook, Twitter, GitHub, etc.) must be enabled in the Firebase Console.
What do I do if Google Sign-In returns an 'unauthorized-domain' error?
The domain must be added to the Authorized Domains list. For local development, add `localhost` (without protocol or port) to the list in the Firebase Console or via the `authorizedDomains` field in `firebase.json`.
Does this skill cover all platforms?
The skill includes references for Web, Flutter, and Android (Kotlin) client setup, but the core guide focuses on configuration and concepts. Platform-specific implementation details are in separate reference files.
What happens after I configure firebase.json with auth settings?
You must run `npx -y firebase-tools@latest deploy --only auth` to deploy the configuration to Firebase and auto-generate the necessary OAuth clients for your app.

Generated from the current SKILL.md. These answers refresh after source changes.