bairav
Phoenix with firebase authentication
we are trying to use firebase authentication with phoenix framework, but we failed to pass the session cookie to the browser, using Guardian library.
login page:
<form id="login">
<label>Email</label>
<input type="text" name="email">
<label>Password</label>
<input type="password" name="password">
<button type="submit">Log in</button>
</form>
<script>
window.addEventListener("DOMContentLoaded", () => {
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE);
document
.getElementById("login")
.addEventListener("submit", (event) => {
event.preventDefault();
const email = event.target.email.value;
const password = event.target.password.value;
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then(({ user }) => {
console.log(user)
return user.getIdToken().then((idToken) => {
return fetch("/api/sessionlogin", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"CSRF-Token": Cookies.get("XSRF-TOKEN")
},
body: JSON.stringify({ idToken }),
});
});
})
.then(() => {
return firebase.auth().signOut();
})
.then(() => {
window.location.assign("/dashboard");
});
return false;
});
});
</script>
controller:
def session_login(conn, %{"idToken" => token}) do
{:ok, claims} = ExFirebaseAuth.Token.verify_token(token)
conn
|> Guardian.Plug.sign_in(claims)
|> json(%{state: "success"})
end
pipeline:
defmodule FB.Auth.Pipeline do
use Guardian.Plug.Pipeline,
otp_app: :fb,
error_handler: FB.Auth.ErrorHandler,
module: FB.Auth.Guardian
plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
plug Guardian.Plug.LoadResource, allow_blank: true
end
router:
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
pipeline :auth do
plug FB.Auth.Pipeline
end
scope "/", FBWeb do
pipe_through [:browser]
get "/", PageController, :index
post "/logout", PageController, :logout
end
scope "/", FBWeb do
pipe_through [:browser, :auth]
get "/dashboard", PageController, :dashboard
end
scope "/api", FBWeb do
pipe_through [:api]
post "/sessionlogin", PageController, :session_login
end
Popular in Questions
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Other popular topics
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










