Guardian v0.13.0 Guardian.Phoenix.Socket
Provides functions for managing authentication with sockets.
Usually you’d use this on the Socket to authenticate on connection on
the connect
function.
There are two main ways to use this module.
- use Guardian.Phoenix.Socket
- import Guardian.Phoenix.Socket
You use this function when you want to automatically sign in a socket
on connect
. The case where authentication information is not provided
is not handled so that you can handle it yourself.
defmodule MyApp.UserSocket do
use Phoenix.Socket
use Guardian.Phoenix.Socket
# This function will be called when there was no authentication information
def connect(_params,socket) do
:error
end
end
If you want more control over the authentication of the connection, then you
should import Guardian.Phoenix.Socket
and use the sign_in
function
to authenticate.
defmodule MyApp.UserSocket do
use Phoenix.Socket
import Guardian.Phoenix.Socket
def connect(%{"guardian_token" => jwt} = params, socket) do
case sign_in(socket, jwt) do
{:ok, authed_socket, guardian_params} ->
{:ok, authed_socket}
_ -> :error
end
end
end
If you want to authenticate on the join of a channel, you can import this module and use the sign_in function as normal.
Summary
Functions
Boolean if the token is present or not to indicate an authenticated socket
Fetches the claims
map that was encoded into the token
Loads the resource from the serializer. The resource is not cached onto the socket so using this function will load a fresh version of the resource each time it’s called
Fetches the JWT that was provided for the initial authentication. This is provided as an encoded string
Set the current claims. Used internally and in tests. Not expected to be used inside channels or sockets
Set the current resource. Used internally and in tests. Not expected to be used inside channels or sockets
Set the current token. Used internally and in tests. Not expected to be used inside channels or sockets
Sign into a socket. Takes a JWT and verifies it. If successful it caches the JWT and decoded claims onto the socket for future use
Sign out of the socket but do not revoke. The token will still be valid for future requests
Signout of the socket and also revoke the token. Using with GuardianDB this will render the token useless for future requests
Functions
Boolean if the token is present or not to indicate an authenticated socket
Loads the resource from the serializer. The resource is not cached onto the socket so using this function will load a fresh version of the resource each time it’s called.
Fetches the JWT that was provided for the initial authentication. This is provided as an encoded string.
Set the current claims. Used internally and in tests. Not expected to be used inside channels or sockets.
Set the current resource. Used internally and in tests. Not expected to be used inside channels or sockets.
Set the current token. Used internally and in tests. Not expected to be used inside channels or sockets.
Sign into a socket. Takes a JWT and verifies it. If successful it caches the JWT and decoded claims onto the socket for future use.
Sign out of the socket but do not revoke. The token will still be valid for future requests.