Skip to content

Commit

Permalink
Add stimulus controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Pablo Gil committed Oct 26, 2022
1 parent 8f6d400 commit 3988a9e
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 3988a9e

Please sign in to comment.