sawthinkar

sawthinkar

OAuth2 Authentication flow error

Error: Getting this error message when the user tries to log in using OAuth2.

[debug] Processing with MyAppWeb.UserController.update/2
  Parameters: %{"id" => "undefined", "user" => %{"bio" => "bio", "email" => "email@gmail.com", "github_token" => "XXX", "github_uid" => "XXX", "image_url" => "https://avatars3.githubusercontent.com", "name" => "Name", "password" => "[FILTERED]", "username" => "username"}}
  Pipelines: [:api]
[info] Sent 400 in 14ms
[debug] ** (Ecto.Query.CastError) deps/ecto/lib/ecto/repo/queryable.ex:348: value `"undefined"` in `where` cannot be cast to type :id in query:

from u in MyApp.Accounts.User,
  where: u.id == ^"undefined",
  select: u

This is the UserController.ex

def update(conn, %{"id" => id, "user" => user_params}) do
  user = Accounts.get_user!(id)

  with {:ok, %User{} = user} <- Accounts.update_user(user, user_params) do
    render(conn, "show.json", user: user)
  end
end

The issue, so far I can think of, is when the user logs in using third party app, the account is not created in the database first, instead it is directed to the update profile page in the front-end which is in React. Then when the user submits the update profile form, id is undefined and is not able to get_user!(id).

  • Do I modify update() ? Adding case statement?

    undefined -> create()
    id -> update()
    

    But in this case, "undefined" seems different from nil, but I don’t know how.

  • Do I modify get_user()? Without !.

  • Do I create check_user_id()?

  • Do I change the flow of the OAuth2 Authentication; create the user before redirecting to update profile page?

  • Do I make changes from front-end before passing the parameters?

  • What are some decent standard options?

Ref: how to check if user id exists

Marked As Solved

jeremyjh

jeremyjh

update should only be called once the user record exists. The front-end should call a create route when id is undefined.

Also Liked

sneako

sneako

Since the id param is coming in as ‘undefined’, I would have to guess that your issue is in your JavaScript somewhere

rickmutua

rickmutua

thanks for the heads-up. came across the same issue and fixed it

Last Post!

rickmutua

rickmutua

thanks for the heads-up. came across the same issue and fixed it

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New

We're in Beta

About us Mission Statement