Files
pet-companion-front/src/services/auth/auth.ts
T

41 lines
1.2 KiB
TypeScript

import {
signInWithEmailAndPassword,
createUserWithEmailAndPassword,
signOut as firebaseSignOut,
User,
GoogleAuthProvider,
signInWithPopup
} from 'firebase/auth';
import { auth } from '../../config/firebase';
export async function login(email: string, password: string): Promise<string> {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
const token = await userCredential.user.getIdToken();
localStorage.setItem('TOKEN', token);
return token;
}
export async function register(email: string, password: string): Promise<string> {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
const token = await userCredential.user.getIdToken();
localStorage.setItem('TOKEN', token);
return token;
}
export async function signOut(): Promise<void> {
await firebaseSignOut(auth);
localStorage.removeItem('TOKEN');
}
export function getCurrentUser(): User | null {
return auth.currentUser;
}
export async function signInWithGoogle(): Promise<string> {
const provider = new GoogleAuthProvider();
const userCredential = await signInWithPopup(auth, provider);
const token = await userCredential.user.getIdToken();
localStorage.setItem('TOKEN', token);
return token;
}