From 96504fd7378989dabe37701b019f9b2befd5122a Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Wed, 30 Mar 2022 22:23:49 +0200 Subject: [PATCH] Moves to Google Log in --- .idea/vcs.xml | 6 +++ src/jsMain/kotlin/Main.kt | 45 ++++---------------- src/jsMain/resources/firebase-ports/index.js | 31 +++----------- 3 files changed, 20 insertions(+), 62 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/jsMain/kotlin/Main.kt b/src/jsMain/kotlin/Main.kt index 024712f..0b14b02 100644 --- a/src/jsMain/kotlin/Main.kt +++ b/src/jsMain/kotlin/Main.kt @@ -14,23 +14,16 @@ data class FirebaseUser( val email: String, val uid: String, val accessToken: String, - val lastLogin: Long, - val createdAt: Long ) @JsModule("@jlengrand/firebase-ports") @JsNonModule external object FirebasePorts{ - - fun signUp(email: String, password: String) : Promise - fun logIn(email: String, password: String) : Promise + fun logIn() : Promise fun logOut() - } fun main() { - var email by mutableStateOf("") - var password by mutableStateOf("") var user : FirebaseUser? by mutableStateOf(null) renderComposable(rootElementId = "root") { @@ -44,30 +37,10 @@ fun main() { if(user == null) { Div({ style { padding(25.px) } }) { - TextArea(value = email, - attrs = { - onInput { email = it.value } - }) - - TextArea(value = password, - attrs = { - onInput { password = it.value } - }) - Button(attrs = { onClick { GlobalScope.launch { - user = FirebasePorts.signUp(email, password).await() - } - } - }) { - Text("Sign Up!") - } - - Button(attrs = { - onClick { - GlobalScope.launch { - user = FirebasePorts.logIn(email, password).await() + user = FirebasePorts.logIn().await() } } }) { @@ -86,15 +59,15 @@ fun main() { Text("LogOut!") } } - } - Div({ style { padding(25.px) } }){ - P(){ - Text("---------------") - } + Div({ style { padding(25.px) } }){ + P(){ + Text("---------------") + } - P(){ - Text(JSON.stringify(user)) + P(){ + Text(JSON.stringify(user)) + } } } } diff --git a/src/jsMain/resources/firebase-ports/index.js b/src/jsMain/resources/firebase-ports/index.js index 24b1d6b..709c095 100644 --- a/src/jsMain/resources/firebase-ports/index.js +++ b/src/jsMain/resources/firebase-ports/index.js @@ -1,43 +1,22 @@ import {FIREBASE_CONFIG} from "./constants"; import { initializeApp } from "firebase/app"; -import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut } from "firebase/auth"; +import { getAuth, signInWithPopup, GoogleAuthProvider, signOut } from "firebase/auth"; const firebaseApp = initializeApp(FIREBASE_CONFIG); +const provider = new GoogleAuthProvider(); const auth = getAuth(); -export async function signUp(email, password){ +export async function logIn(){ - console.log("Signing Up"); + console.log("Logging in!"); try { - const userCred = await createUserWithEmailAndPassword(auth, email, password); + const userCred = await signInWithPopup(auth, provider); return { accessToken: userCred.user.accessToken, email: userCred.user.email, uid: userCred.user.uid, - lastLogin: userCred.user.metadata.lastLoginAt, - createdAt : userCred.user.metadata.createdAt - } - } - catch (e) { - console.log(e); - } -} - -export async function logIn(email, password){ - - console.log("Signing In"); - - try { - const userCred = await signInWithEmailAndPassword(auth, email, password); - - return { - accessToken: userCred.user.accessToken, - email: userCred.user.email, - uid: userCred.user.uid, - lastLogin: userCred.user.metadata.lastLoginAt, - createdAt : userCred.user.metadata.createdAt } } catch (e) {