<reponame>scarletstudio/butterfly<filename>frontend/src/app/mock/MockLogin.js<gh_stars>0import { getAuth, signInWithEmailAndPassword, updateProfile } from 'firebase/auth'

const MOCK_USER_PASSWORD = import.meta.env.VITE_firebase_mockUserPassword

export const MOCK_USERS = [
    {
        displayName: 'Pradeep Balaram',
        email: 'anpch@example.com',
        photoURL: 'https://randomuser.me/api/portraits/men/45.jpg',
    },
    {
        displayName: 'Si-Woo Yun-Seo',
        email: 'kenaa@example.com',
        photoURL: 'https://randomuser.me/api/portraits/men/92.jpg',
    },
    {
        displayName: 'Ilya Viktoria',
        email: 'nnheo@example.com',
        photoURL: 'https://randomuser.me/api/portraits/women/31.jpg',
    },
    {
        displayName: 'Asabe Oyibo',
        email: 'hzdkv@example.com',
        photoURL: 'https://randomuser.me/api/portraits/women/89.jpg',
    },
    {
        displayName: 'Ernesto Saúl',
        email: 'efpyi@example.com',
        photoURL: 'https://randomuser.me/api/portraits/men/61.jpg',
    },
    {
        displayName: 'Quyên Hương',
        email: 'anpch@example.com',
        photoURL: 'https://randomuser.me/api/portraits/women/12.jpg',
    },
]
const MOCK_USER_MAP = MOCK_USERS.reduce((a, v) => ({ ...a, [v.email]: v }), {})

export async function signInMockUser(mockUserEmail) {
    const mockUser = MOCK_USER_MAP?.[mockUserEmail]
    if (!mockUser) {
        return {
            user: null,
            signInError: `No mock user for email: '${mockUserEmail}'`,
        }
    }
    try {
        const auth = getAuth()
        const { email, displayName, photoURL } = mockUser
        // Currently, we will only use email and password authentication
        // for mock users. Real users should login with Google.
        const { user } = await signInWithEmailAndPassword(auth, email, MOCK_USER_PASSWORD)
        // Update the Firebase authentication user profile for the mock user
        await updateProfile(auth.currentUser, { displayName, photoURL })
        // Return the authenticated user
        return {
            user,
            signInError: null,
        }
    } catch (err) {
        return {
            user: null,
            signInError: err,
        }
    }
}
