Authex v0.3.0 Authex.Serializer behaviour View Source
Defines a serializer.
A serializer is used to convert a resource into a token, as well as a token back into a resource. A typical resource would be something like a user struct.
defmodule MyApp.Auth.UserSerializer do
use Authex.Serializer
@impl Authex.Serializer
def for_token(user, opts) do
{:ok, MyApp.Auth.token([sub: user.id, scopes: user.scopes], opts)}
end
@impl Authex.Serializer
def from_token(token, _opts) do
{:ok, %MyApp.User{id: token.sub, scopes: token.scopes}}
end
end
With our serilializer defined, we must add callbacks for for_token/2
to
convert our user into a token, as well as for from_token/2
to convert a
token into a user
Link to this section Summary
Callbacks
Converts a resource into an Authex.Token
struct
Converts an Authex.Token
struct into a resource
Link to this section Types
Link to this type
t()
View Source
t()
View Source
t() :: module()
t() :: module()
Link to this section Callbacks
Link to this callback
for_token(term, options)
View Source
for_token(term, options)
View Source
for_token(term(), options :: Authex.Token.options()) ::
{:ok, Authex.Token.t()} | {:error, any()}
for_token(term(), options :: Authex.Token.options()) :: {:ok, Authex.Token.t()} | {:error, any()}
Converts a resource into an Authex.Token
struct.
Must return {:ok, token}
on sucess
Options
:time
- The base time (timestamp format) in which to use.:ttl
- The time-to-live for the token in seconds. The lifetime is based on the time provided via the options, or the current time if not provided.
Link to this callback
from_token(token, options)
View Source
from_token(token, options)
View Source
from_token(token :: Authex.Token.t(), options :: keyword()) ::
{:ok, term()} | {:error, term()}
from_token(token :: Authex.Token.t(), options :: keyword()) :: {:ok, term()} | {:error, term()}
Converts an Authex.Token
struct into a resource.
Options
Any additional options that your serializer might need.