added image upload feature
This commit is contained in:
@@ -29,13 +29,23 @@ import {
|
||||
getEncryptedSecretKey,
|
||||
} from '../lib/crypto'
|
||||
|
||||
type MongoUser = {
|
||||
id: string
|
||||
email: string
|
||||
displayName?: string
|
||||
photoURL?: string
|
||||
theme?: string
|
||||
}
|
||||
|
||||
type AuthContextValue = {
|
||||
user: User | null
|
||||
userId: string | null
|
||||
mongoUser: MongoUser | null
|
||||
loading: boolean
|
||||
secretKey: Uint8Array | null
|
||||
signInWithGoogle: () => Promise<void>
|
||||
signOut: () => Promise<void>
|
||||
refreshMongoUser: () => Promise<void>
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null)
|
||||
@@ -43,6 +53,7 @@ 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 [mongoUser, setMongoUser] = useState<MongoUser | null>(null)
|
||||
const [secretKey, setSecretKey] = useState<Uint8Array | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
@@ -114,9 +125,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
// Try to get existing user
|
||||
try {
|
||||
console.log('[Auth] Fetching user by email:', email)
|
||||
const existingUser = await getUserByEmail(email, token) as { id: string }
|
||||
const existingUser = await getUserByEmail(email, token) as MongoUser
|
||||
console.log('[Auth] Found existing user:', existingUser.id)
|
||||
setUserId(existingUser.id)
|
||||
setMongoUser(existingUser)
|
||||
} catch (error) {
|
||||
console.warn('[Auth] User not found, registering...', error)
|
||||
// User doesn't exist, register them
|
||||
@@ -127,9 +139,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
photoURL: authUser.photoURL || undefined,
|
||||
},
|
||||
token
|
||||
) as { id: string }
|
||||
) as MongoUser
|
||||
console.log('[Auth] Registered new user:', newUser.id)
|
||||
setUserId(newUser.id)
|
||||
setMongoUser(newUser)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Auth] Error syncing user with database:', error)
|
||||
@@ -148,6 +161,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
} else {
|
||||
setUserId(null)
|
||||
setMongoUser(null)
|
||||
setSecretKey(null)
|
||||
}
|
||||
setLoading(false)
|
||||
@@ -160,9 +174,22 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
await signInWithPopup(auth, googleProvider)
|
||||
}
|
||||
|
||||
async function refreshMongoUser() {
|
||||
if (!user) return
|
||||
try {
|
||||
const token = await user.getIdToken()
|
||||
const email = user.email!
|
||||
const updated = await getUserByEmail(email, token) as MongoUser
|
||||
setMongoUser(updated)
|
||||
} catch (error) {
|
||||
console.error('[Auth] Error refreshing mongo user:', error)
|
||||
}
|
||||
}
|
||||
|
||||
async function signOut() {
|
||||
// Clear secret key from memory
|
||||
setSecretKey(null)
|
||||
setMongoUser(null)
|
||||
// Reset onboarding so tour shows again on next login
|
||||
localStorage.removeItem('gj-onboarding-done')
|
||||
localStorage.removeItem('gj-tour-pending-step')
|
||||
@@ -175,10 +202,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const value: AuthContextValue = {
|
||||
user,
|
||||
userId,
|
||||
mongoUser,
|
||||
secretKey,
|
||||
loading,
|
||||
signInWithGoogle,
|
||||
signOut,
|
||||
refreshMongoUser,
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user