From 3988a9eeb2e86c3a9931202b232985b1743e00be Mon Sep 17 00:00:00 2001 From: Juan Pablo Gil Date: Wed, 26 Oct 2022 04:46:13 +0000 Subject: [PATCH] Add stimulus controller --- .../controllers/sign_in_controller.js | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/generators/auth/templates/javascript/controllers/sign_in_controller.js diff --git a/lib/generators/auth/templates/javascript/controllers/sign_in_controller.js b/lib/generators/auth/templates/javascript/controllers/sign_in_controller.js new file mode 100644 index 0000000..103f250 --- /dev/null +++ b/lib/generators/auth/templates/javascript/controllers/sign_in_controller.js @@ -0,0 +1,46 @@ +import { Controller } from "@hotwired/stimulus" + +// Using firebase v9 compatibility until firebaseui gets updated to work with +// firebase v9 modules: +import firebase from "@firebase/app-compat" +import "@firebase/auth-compat" +import * as firebaseui from "firebaseui" + +const signInOptions = [ + { provider: firebase.auth.EmailAuthProvider.PROVIDER_ID } +] + +// Connects to data-controller="sign-in" +export default class extends Controller { + static targets = [ "sessionForm", "token" ] + + initialize() { + firebase.initializeApp({ + apiKey: "AIzaSyCnp5HKhIoBR6MWerr3AejI69y5hgHommk", + authDomain: "fir-rails-f5432.firebaseapp.com", + projectId: "fir-rails-f5432", + storageBucket: "fir-rails-f5432.appspot.com", + messagingSenderId: "1012453872416", + appId: "1:1012453872416:web:aa41b65339ed1bdadb3d45" + }) + } + + connect() { + const firebaseAuth = firebase.auth() + const firebaseAuthUI = new firebaseui.auth.AuthUI(firebaseAuth) + const signInSuccessWithAuthResult = this.successCallBack.bind(this) + + firebaseAuthUI.start("#auth-container", { + signInOptions, callbacks: { signInSuccessWithAuthResult } + }) + } + + successCallBack(authResult) { + authResult.user.getIdToken(true).then(token => { + this.tokenTarget.value = token + this.sessionFormTarget.submit() + }) + + return false + } +}