mongog setup
This commit is contained in:
@@ -13,12 +13,12 @@ import {
|
||||
signOut as firebaseSignOut,
|
||||
type User,
|
||||
} from 'firebase/auth'
|
||||
import { auth, googleProvider, db } from '../lib/firebase'
|
||||
import { doc, setDoc } from 'firebase/firestore'
|
||||
import { COLLECTIONS } from '../lib/firestoreConfig'
|
||||
import { auth, googleProvider } from '../lib/firebase'
|
||||
import { registerUser, getUserByEmail } from '../lib/api'
|
||||
|
||||
type AuthContextValue = {
|
||||
user: User | null
|
||||
userId: string | null
|
||||
loading: boolean
|
||||
signInWithGoogle: () => Promise<void>
|
||||
signOut: () => Promise<void>
|
||||
@@ -28,21 +28,33 @@ const AuthContext = createContext<AuthContextValue | null>(null)
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [user, setUser] = useState<User | null>(null)
|
||||
const [userId, setUserId] = useState<string | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
// Save user info to Firestore when they authenticate
|
||||
async function saveUserToFirestore(authUser: User) {
|
||||
// Register or fetch user from MongoDB
|
||||
async function syncUserWithDatabase(authUser: User) {
|
||||
try {
|
||||
const userRef = doc(db, COLLECTIONS.USERS, authUser.uid)
|
||||
await setDoc(userRef, {
|
||||
id: authUser.uid,
|
||||
email: authUser.email || '',
|
||||
displayName: authUser.displayName || '',
|
||||
photoURL: authUser.photoURL || '',
|
||||
lastLoginAt: Date.now(),
|
||||
}, { merge: true })
|
||||
const token = await authUser.getIdToken()
|
||||
const email = authUser.email!
|
||||
|
||||
// Try to get existing user
|
||||
try {
|
||||
const existingUser = await getUserByEmail(email, token)
|
||||
setUserId(existingUser.id)
|
||||
} catch (error) {
|
||||
// User doesn't exist, register them
|
||||
const newUser = await registerUser(
|
||||
{
|
||||
email,
|
||||
displayName: authUser.displayName || undefined,
|
||||
photoURL: authUser.photoURL || undefined,
|
||||
},
|
||||
token
|
||||
)
|
||||
setUserId(newUser.id)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error saving user to Firestore:', error)
|
||||
console.error('Error syncing user with database:', error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +62,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const unsubscribe = onAuthStateChanged(auth, async (u) => {
|
||||
setUser(u)
|
||||
if (u) {
|
||||
await saveUserToFirestore(u)
|
||||
await syncUserWithDatabase(u)
|
||||
} else {
|
||||
setUserId(null)
|
||||
}
|
||||
setLoading(false)
|
||||
})
|
||||
@@ -64,10 +78,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
async function signOut() {
|
||||
await firebaseSignOut(auth)
|
||||
setUserId(null)
|
||||
}
|
||||
|
||||
const value: AuthContextValue = {
|
||||
user,
|
||||
userId,
|
||||
loading,
|
||||
signInWithGoogle,
|
||||
signOut,
|
||||
|
||||
Reference in New Issue
Block a user