tome-web/client/api/auth.ts
2021-09-23 23:38:12 -04:00

43 lines
1.3 KiB
TypeScript

import firebase from 'firebase'
import 'firebase/auth'
const app = firebase.initializeApp({
apiKey: "AIzaSyBICkyQIt-9lTsxHvRS1MOlZ-UdFVj8rsA",
authDomain: "tomecraft-87ce5.firebaseapp.com",
projectId: "tomecraft-87ce5",
storageBucket: "tomecraft-87ce5.appspot.com",
messagingSenderId: "1021868917316",
appId: "1:1021868917316:web:6c58534dd0ed491beab211",
})
const auth = app.auth()
// auto-login
export function subscribeToAuthState(handleUser: (user: {uid: string; displayName: string} | null) => void) {
return auth.onAuthStateChanged(handleUser)
}
///
/// Email/password
///
export async function registerBasic(email: string, password: string) {
const user = await auth.createUserWithEmailAndPassword(email, password)
}
export async function loginBasic(email: string, password: string): Promise<string | null> {
const credentials = await auth.signInWithEmailAndPassword(email, password)
return credentials.user?.uid ?? null
}
///
/// Google
///
export async function loginGoogle(): Promise<string> {
// @ts-ignore deno does not recognize firebase.auth the way it is imported, but it exists
const provider = new firebase.auth.GoogleAuthProvider();
const {accessToken, credential, user} = await auth.signInWithPopup(provider)
return user.uid
}