srhwork
srhwork

Reputation: 33

Use Guardian without Model

I want to know if it is possible to use Guardian without a user model, and how to use it.

I use Phoenix just for a server, tiny API and channels. I have a database in the browser with PouchDB, and I want to authorize access to the tiny API.

The thing is, I don't have, and I don't want to have, models in my backend, but Guardian needs a resource in order to serialize the token.

Is it possible to do something in this scenario?

When I call the method Guardian.serializer.for_token(user) or Guardian.encode_and_sign(:token) (with a user map) the return is Unknown resource type.

My guardian_serializer.ex is:

@behaviour Guardian.Serializer

def for_token(user = %{:id => 88888888}), do: {:ok, "User:#{user.id}"}
def for_token(_), do: {:error, "Unknown resource type"}

def from_token("User:" <> id), do: {:ok, "User:#{id}"}
def from_token(_), do: {:error, "Unknown resource type"}

Thanks.

Upvotes: 3

Views: 256

Answers (1)

Chris
Chris

Reputation: 2800

If you don't want Guardian to match tokens against user existing in your db, I thing you can stub your serializer as such:

def for_token(_),  do: {:ok, "ok"}    
def from_token(_), do: {:ok, "ok"}

Then you will have nothing else than "ok" when calling Guardian.Plug.current_resource

Upvotes: 0

Related Questions