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

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
fayddelight
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement