I'm using {:guardian, "~> 1.0"} for my project to generate a token but it's giving me an error.
Here's a code for my guardian file
defmodule Dailyploy.Guardian do
use Guardian, otp_app: :dailyploy
def subject_for_token(user, _claims) do
sub = to_string(user.id)
{:ok, sub}
end
def subject_for_token(_, _) do
{:error, :reason_for_error}
end
def resource_from_claims(claims) do
id = claims["sub"]
resource = Dailyploy.Accounts.get_user!(id)
{:ok, resource}
end
def resource_from_claims(_claims) do
{:error, :reason_for_error}
end
end
It's showing me an error that subject_for_token. This clause cannot match because a previous clause at line 4 always matchesElixir
Can someone please explain why it is not working?
The compiler is complaining because your two definitions of
subject_for_tokenare identical, they both expect the exact same arguments.To fix this, make it explicit that you want to receive a
Userstruct in the first definition:The same can be said about
resource_from_claims; both functions will match the exact same arguments. This too can be fixed: