Files
ui/pkg/auth-firebase/service/firebase-auth-service.ts
zeekay ce8cf94ef8 feat(auth): make Firebase optional, add @hanzo/auth-firebase package
- Create @hanzo/auth-firebase v1.0.0 as optional Firebase provider
- Update @hanzo/auth v2.6.0 with pluggable auth provider system
- Update @hanzo/commerce v7.4.0 with optional Firebase peer dependency
- Add StubAuthService for graceful failures when no provider configured
- Add registerAuthProvider() for runtime provider registration

BREAKING CHANGE: Firebase is no longer bundled with @hanzo/auth.
Install @hanzo/auth-firebase and register it if you need Firebase auth:

  import { FirebaseAuthService } from '@hanzo/auth-firebase'
  import { registerAuthProvider } from '@hanzo/auth'
  registerAuthProvider('firebase', FirebaseAuthService)
2026-01-24 13:14:08 -08:00

234 lines
6.1 KiB
TypeScript

/**
* Firebase Authentication Service Implementation
* Implements the AuthService interface using Firebase as the auth provider.
*/
import { makeAutoObservable, makeObservable, computed } from 'mobx'
import type { AuthService, AuthServiceConf, HanzoUserInfo, HanzoUserInfoValue } from '@hanzo/auth'
import {
auth as fbAuth,
signupWithEmailAndPassword,
loginWithCustomToken,
loginWithEmailAndPassword,
loginWithProvider,
logoutBackend,
isFirebaseConfigured
} from './firebase-support'
import { associateWalletAddressWithAccount, getAssociatedWalletAddress } from './wallet-support'
class HanzoUserInfoStore implements HanzoUserInfo {
constructor() {
makeAutoObservable(this)
}
_email: string = ''
_displayName: string | null = null
_walletAddress: string | null = null
get email(): string { return this._email}
get displayName(): string | null { return this._displayName}
get walletAddress(): string | null { return this._walletAddress}
clear():void {
this._email = ''
this._displayName = null
this._walletAddress = null
}
set(v: HanzoUserInfoValue):void {
this._email = v.email
this._displayName = v.displayName
this._walletAddress = v.walletAddress
}
get isValid(): boolean {
return (this._email.length > 0)
}
}
export class FirebaseAuthService implements AuthService {
private _hzUser = new HanzoUserInfoStore()
constructor(conf: AuthServiceConf, user: HanzoUserInfoValue | null) {
makeObservable(this, {
loggedIn: computed,
user: computed
})
if (user) {
this._hzUser.set(user)
}
}
static isConfigured(): boolean {
return isFirebaseConfigured()
}
get user(): HanzoUserInfo | null {
return this._hzUser.isValid ? this._hzUser : null
}
get loggedIn(): boolean {
return this._hzUser.isValid
}
signupEmailAndPassword = async (
email: string,
password: string
): Promise<{success: boolean, userInfo: HanzoUserInfo | null, message?: string}> => {
try {
this._hzUser.clear()
const res = await signupWithEmailAndPassword(email, password)
if (res.success && res.user) {
const walletAddress = res.user.email ? await getAssociatedWalletAddress(res.user.email) : undefined
this._hzUser.set({
email: res.user.email ?? '',
displayName : res.user.displayName ?? null,
walletAddress : walletAddress?.result ?? null
})
return {
success: true,
userInfo: this._hzUser,
message: res.message
}
}
return {
success: false,
userInfo: null,
message: res.message
}
}
catch (e) {
console.error('Error signing in with Firebase auth', e)
return {success: false, userInfo: null, message: 'Error signing in with Firebase auth'}
}
}
loginEmailAndPassword = async (
email: string,
password: string
): Promise<{success: boolean, userInfo: HanzoUserInfo | null, message?: string}> => {
try {
this._hzUser.clear()
const res = await loginWithEmailAndPassword(email, password)
if (res.success && res.user) {
const walletAddress = res.user.email ? await getAssociatedWalletAddress(res.user.email) : undefined
this._hzUser.set({
email: res.user.email ?? '',
displayName : res.user.displayName ?? null,
walletAddress : walletAddress?.result ?? null
})
return {
success: true,
userInfo: this._hzUser,
message: res.message
}
}
return {
success: false,
userInfo: null,
message: res.message
}
}
catch (e) {
console.error('Error signing in with Firebase auth', e)
return {success: false, userInfo: null, message: 'Error signing in with Firebase auth'}
}
}
loginWithProvider = async (
provider: 'google' | 'facebook' | 'github'
): Promise<{success: boolean, userInfo: HanzoUserInfo | null}> => {
try {
this._hzUser.clear()
const res = await loginWithProvider(provider)
if (res.success && res.user) {
const walletAddress = res.user.email ? await getAssociatedWalletAddress(res.user.email) : undefined
this._hzUser.set({
email: res.user.email ?? '',
displayName : res.user.displayName ?? null,
walletAddress : walletAddress?.result ?? null
})
return {
success: true,
userInfo: this._hzUser
}
}
return {
success: false,
userInfo: null
}
}
catch (e) {
console.error('Error signing in with Firebase auth', e)
return {success: false, userInfo: null}
}
}
loginWithCustomToken = async (
token: string
): Promise<{success: boolean, userInfo: HanzoUserInfo | null}> => {
try {
this._hzUser.clear()
const res = await loginWithCustomToken(token)
if (res.success && res.user) {
const walletAddress = res.user.email ? await getAssociatedWalletAddress(res.user.email) : undefined
this._hzUser.set({
email: res.user.email ?? '',
displayName : res.user.displayName ?? null,
walletAddress : walletAddress?.result ?? null
})
return {
success: true,
userInfo: this._hzUser
}
}
return {
success: false,
userInfo: null
}
}
catch (e) {
console.error('Error signing in with Firebase auth', e)
return {success: false, userInfo: null}
}
}
associateWallet = async (): Promise<void> => {
if (this._hzUser.isValid) {
const res = await associateWalletAddressWithAccount(this._hzUser.email)
if (!res.error) {
this._hzUser._walletAddress = res.result ?? null
}
}
}
logout = async (): Promise<{ success: boolean }> => {
if (fbAuth) {
await fbAuth.signOut()
}
this._hzUser.clear()
return await logoutBackend()
}
setServerSideUser = (user: HanzoUserInfoValue | null) => {
if (user) {
this._hzUser.set(user)
}
}
}
export default FirebaseAuthService