File

src/app/auth/auth.service.ts

Constructor

constructor(auth: Auth)

Methods

signInWithGoogle
signInWithGoogle()

Google OAuth sign-in (popup)

Returns: void
signInWithEmail
signInWithEmail(email: string, password: string)

Email + password sign-in

Returns: void
registerWithEmail
registerWithEmail(email: string, password: string, displayName: string)

Create account with email + password (optional displayName)

Returns: void
sendPasswordReset
sendPasswordReset(email: string)

Send password reset email

Returns: void
signOut
signOut()

Sign out

Returns: void
idToken
idToken(forceRefresh: boolean)

Fetch ID token (e.g., for calling your backend)

Returns: any

Properties

Private _user
_user: WritableSignal<User>
displayName
displayName: string
email
email: string
isAuthed
isAuthed: any

True if signed in

uid
uid: string

Convenience getters

user
user: any

Current Firebase user (signal)

import { Injectable, computed, signal } from '@angular/core';
import {
  Auth,
  User,
  GoogleAuthProvider,
  onAuthStateChanged,
  signInWithPopup,
  signOut as fbSignOut,
  signInWithEmailAndPassword,
  createUserWithEmailAndPassword,
  sendPasswordResetEmail,
  updateProfile,
  getIdToken
} from '@angular/fire/auth';

@Injectable({ providedIn: 'root' })
export class AuthService {
  private _user = signal<User | null>(null);
  /** Current Firebase user (signal) */
  readonly user = computed(() => this._user());
  /** True if signed in */
  readonly isAuthed = computed(() => this._user() !== null);

  constructor(private auth: Auth) {
    onAuthStateChanged(this.auth, (u) => this._user.set(u));
  }

  /** Google OAuth sign-in (popup) */
  signInWithGoogle() {
    return signInWithPopup(this.auth, new GoogleAuthProvider());
  }

  /** Email + password sign-in */
  signInWithEmail(email: string, password: string) {
    return signInWithEmailAndPassword(this.auth, email, password);
  }

  /** Create account with email + password (optional displayName) */
  async registerWithEmail(email: string, password: string, displayName?: string) {
    const cred = await createUserWithEmailAndPassword(this.auth, email, password);
    if (displayName) {
      await updateProfile(cred.user, { displayName });
      // onAuthStateChanged will update the signal automatically,
      // but we can also set it to reflect the displayName immediately:
      this._user.set({ ...cred.user } as User);
    }
    return cred;
  }

  /** Send password reset email */
  sendPasswordReset(email: string) {
    return sendPasswordResetEmail(this.auth, email);
  }

  /** Sign out */
  signOut() {
    return fbSignOut(this.auth);
  }

  /** Convenience getters */
  get uid(): string | null { return this._user()?.uid ?? null; }
  get displayName(): string | null { return this._user()?.displayName ?? null; }
  get email(): string | null { return this._user()?.email ?? null; }

  /** Fetch ID token (e.g., for calling your backend) */
  async idToken(forceRefresh = false): Promise<string | null> {
    const u = this._user();
    return u ? getIdToken(u, forceRefresh) : null;
  }
}

results matching ""

    No results matching ""