2021-09-23 23:34:33 -04:00
|
|
|
import firebase from 'firebase'
|
|
|
|
import 'firebase/auth'
|
2021-09-23 22:37:47 -04:00
|
|
|
|
2021-09-23 23:34:33 -04:00
|
|
|
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",
|
|
|
|
})
|
2021-09-23 22:37:47 -04:00
|
|
|
|
2021-09-23 23:34:33 -04:00
|
|
|
const auth = app.auth()
|
2021-09-23 22:37:47 -04:00
|
|
|
|
|
|
|
// auto-login
|
2021-09-23 23:34:33 -04:00
|
|
|
export function subscribeToAuthState(handleUser: (user: {uid: string; displayName: string} | null) => void) {
|
|
|
|
return auth.onAuthStateChanged(handleUser)
|
|
|
|
}
|
2021-09-23 22:37:47 -04:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Email/password
|
|
|
|
///
|
|
|
|
|
2021-09-23 23:34:33 -04:00
|
|
|
export async function registerBasic(email: string, password: string) {
|
|
|
|
const user = await auth.createUserWithEmailAndPassword(email, password)
|
|
|
|
}
|
2021-09-23 22:37:47 -04:00
|
|
|
|
2021-09-23 23:34:33 -04:00
|
|
|
export async function loginBasic(email: string, password: string): Promise<string | null> {
|
|
|
|
const credentials = await auth.signInWithEmailAndPassword(email, password)
|
|
|
|
return credentials.user?.uid ?? null
|
|
|
|
}
|
2021-09-23 22:37:47 -04:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Google
|
|
|
|
///
|
|
|
|
|
2021-09-23 23:34:33 -04:00
|
|
|
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
|
|
|
|
}
|