2021-11-08 05:08:29 -05:00
|
|
|
import {initializeApp} from 'firebase/app'
|
|
|
|
import {
|
|
|
|
getAuth,
|
|
|
|
onAuthStateChanged,
|
|
|
|
createUserWithEmailAndPassword,
|
|
|
|
signInWithEmailAndPassword,
|
|
|
|
GoogleAuthProvider,
|
|
|
|
signInWithPopup,
|
|
|
|
} from 'firebase/auth'
|
|
|
|
|
|
|
|
const app = initializeApp({
|
2021-09-23 23:34:33 -04:00
|
|
|
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-11-08 05:08:29 -05:00
|
|
|
const auth = getAuth(app)
|
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) {
|
2021-11-08 05:08:29 -05:00
|
|
|
return onAuthStateChanged(auth, handleUser)
|
2021-09-23 23:34:33 -04:00
|
|
|
}
|
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) {
|
2021-11-08 05:08:29 -05:00
|
|
|
const user = await createUserWithEmailAndPassword(auth, email, password)
|
2021-09-23 23:34:33 -04:00
|
|
|
}
|
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> {
|
2021-11-08 05:08:29 -05:00
|
|
|
const credentials = await signInWithEmailAndPassword(auth, email, password)
|
2021-09-23 23:34:33 -04:00
|
|
|
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
|
2021-11-08 05:08:29 -05:00
|
|
|
const provider = new GoogleAuthProvider(auth);
|
|
|
|
const {accessToken, credential, user} = await signInWithPopup(auth, provider)
|
2021-09-23 23:34:33 -04:00
|
|
|
return user.uid
|
|
|
|
}
|