Beamlet.Web.Auth (Beamlet v0.1.1)

Copy Markdown View Source

The owner's sign-in to the beamlet's own pages.

The sign-in is kept in a cookie of its own, scoped to /beamlet, apart from your endpoint's session.

The one part you use is session_options/0, for the socket the beamlet's own pages connect to:

socket "/beamlet/app/live", Phoenix.LiveView.Socket,
  websocket: [connect_info: [session: {Beamlet.Web.Auth, :session_options, []}]]

Beamlet.Router and the beamlet's pages use the rest.

Summary

Functions

Assigns :current_user from the session.

Signs the owner in.

Signs the owner out.

The hooks for a live_session.

Halts a signed-out request with a redirect to the login page.

The Plug.Session options for the sign-in's cookie.

Functions

fetch_current_user(conn, opts)

@spec fetch_current_user(Plug.Conn.t(), keyword()) :: Plug.Conn.t()

Assigns :current_user from the session.

It is nil when nobody is signed in.

log_in(conn, user)

@spec log_in(Plug.Conn.t(), %Beamlet.User{
  __meta__: term(),
  email: term(),
  id: term(),
  inserted_at: term(),
  password: term(),
  password_hash: term(),
  updated_at: term()
}) :: Plug.Conn.t()

Signs the owner in.

It creates a session, renews the cookie, and stores the session's secret and the id the app's LiveView sockets on it take.

log_out(conn)

@spec log_out(Plug.Conn.t()) :: Plug.Conn.t()

Signs the owner out.

It disconnects the app's LiveViews on the session, deletes the session, and clears and renews the cookie.

on_mount(atom, params, session, socket)

@spec on_mount(
  :require_auth | :fetch_current_user,
  map(),
  map(),
  Phoenix.LiveView.Socket.t()
) ::
  {:cont, Phoenix.LiveView.Socket.t()} | {:halt, Phoenix.LiveView.Socket.t()}

The hooks for a live_session.

:require_auth assigns :current_user, or halts with a redirect to the login page. :fetch_current_user assigns it, nil when nobody is signed in.

require_auth(conn, opts)

@spec require_auth(Plug.Conn.t(), keyword()) :: Plug.Conn.t()

Halts a signed-out request with a redirect to the login page.

The path of a GET is stored, so the sign-in returns there.

session_options()

@spec session_options() :: keyword()

The Plug.Session options for the sign-in's cookie.

Beamlet.Router plugs them in for the beamlet's own pages, and your endpoint names them for the socket at /beamlet/app/live.