-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
lib/generators/auth/templates/javascript/controllers/sign_in_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |