ueberauth / guardian

Elixir Authentication
MIT License
3.44k stars 381 forks source link

this clause cannot match error in Guardian.ex #505

Closed shahryarjb closed 6 years ago

shahryarjb commented 6 years ago

Hello , I have copied Guardian.ex code on your document, but I have 2 errors while I am trying to compile my code , it should be noted my code works for me.

error :

warning: this clause cannot match because a previous clause at line 4 always matches
  lib/guardian_lib/guardian.ex:14

warning: this clause cannot match because a previous clause at line 18 always matches
  lib/guardian_lib/guardian.ex:27

all codes of mine :

defmodule TrangellApiGateway.GuardianLib.Guardian do
  use Guardian, otp_app: :trangell_api_gateway

  def subject_for_token(resource, _claims) do
    # You can use any value for the subject of your token but
    # it should be useful in retrieving the resource later, see
    # how it being used on `resource_from_claims/1` function.
    # A unique `id` is a good subject, a non-unique email address
    # is a poor subject.
    sub = to_string(resource.id)
    {:ok, sub}
  end

  def subject_for_token(_, _) do
    {:error, :reason_for_error}
  end

  def resource_from_claims(claims) do
    # Here we'll look up our resource from the claims, the subject can be
    # found in the `"sub"` key. In `above subject_for_token/2` we returned
    # the resource id so here we'll rely on that to look it up.
    id = claims["sub"]
    resource = TrangellApiGateway.get_resource_by_id(id)
    {:ok,  resource}
  end

  def resource_from_claims(_claims) do
    {:error, :reason_for_error}
  end
end

doesn't it make a problem to hitting my security of project ?

yordis commented 6 years ago

@shahryarjb this is nothing to do with security but how Elixir works.

Basically means that your first function declaration always match so the second function will never be called.

That code snippet is a guideline no necessarily the final code, so modify as your needs change.

Related: https://github.com/elixir-lang/elixir/issues/3612