54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import {
|
|
createUserWithEmailAndPassword,
|
|
signInWithEmailAndPassword,
|
|
signOut,
|
|
updateProfile,
|
|
} from 'firebase/auth';
|
|
import { serverTimestamp, setDoc } from 'firebase/firestore';
|
|
|
|
import { auth } from '@/lib/firebase';
|
|
import { userDoc } from '@/lib/firestore';
|
|
|
|
type RegisterParams = {
|
|
email: string;
|
|
password: string;
|
|
displayName: string;
|
|
};
|
|
|
|
export async function loginWithEmail(email: string, password: string) {
|
|
return signInWithEmailAndPassword(auth, email.trim(), password);
|
|
}
|
|
|
|
export async function registerWithEmail({
|
|
email,
|
|
password,
|
|
displayName,
|
|
}: RegisterParams) {
|
|
const credential = await createUserWithEmailAndPassword(
|
|
auth,
|
|
email.trim(),
|
|
password
|
|
);
|
|
|
|
const normalizedDisplayName = displayName.trim();
|
|
|
|
await updateProfile(credential.user, {
|
|
displayName: normalizedDisplayName,
|
|
});
|
|
|
|
await setDoc(userDoc(credential.user.uid), {
|
|
uid: credential.user.uid,
|
|
email: credential.user.email,
|
|
displayName: normalizedDisplayName,
|
|
photoURL: credential.user.photoURL,
|
|
createdAt: serverTimestamp(),
|
|
updatedAt: serverTimestamp(),
|
|
});
|
|
|
|
return credential;
|
|
}
|
|
|
|
export async function logout() {
|
|
return signOut(auth);
|
|
}
|