Phauxth v2.0.0-alpha.0 Phauxth.Authenticate.Base behaviour View Source
Base module for authentication.
This is use
-d by Phauxth.Authenticate and Phauxth.Remember, and it is also
extended by Phauxth.Authenticate.Token.
This module can also be used to produce a custom authentication module, as outlined below.
Custom authentication modules
The next section gives examples of extending this module to create custom authentication modules.
Examples
Authentication for use with Phoenix channels
In this example, after adding the user struct to the current_user value, a token is added (for use with Phoenix channels).
defmodule MyAppWeb.ChannelAuthenticate do
use Phauxth.Authenticate.Base
def set_user(nil, conn), do: assign(conn, :current_user, nil)
def set_user(user, conn) do
token = Phoenix.Token.sign(conn, "user salt", %{"user_id" => user.email})
assign(conn, :current_user, user)
|> assign(:user_token, token)
end
end
MyAppWeb.ChannelAuthenticate is called in the same way as Phauxth.Authenticate.
You can then use Phoenix.Token.verify, in the user_socket.ex
file, to
verify the token.
Link to this section Summary
Callbacks
Gets the user based on the session or token data
Logs the result of the authentication and return the user struct or nil
Sets the current_user
variable
Link to this section Callbacks
get_user(Plug.Conn.t(), tuple()) :: map() | {:error, String.t()} | nil
Gets the user based on the session or token data.
This function also calls the database to get user information.
Logs the result of the authentication and return the user struct or nil.
set_user(map() | nil, Plug.Conn.t()) :: Plug.Conn.t()
Sets the current_user
variable.