Class: Fizzy::MagicLinkFlow

Inherits:
Object
  • Object
show all
Defined in:
lib/fizzy/magic_link_flow.rb

Overview

Orchestrates the passwordless magic link authentication flow.

The flow works in two steps:

  1. Call CreateSession with an email address - this sends a magic link email

  2. Call RedeemMagicLink with the token from the magic link URL

After redemption, the response contains a session token that can be used with CookieAuth or BearerAuth for subsequent requests.

Examples:

flow = Fizzy::MagicLinkFlow.new(base_url: "https://fizzy.do")
flow.request_magic_link(email: "user@example.com")
# User clicks magic link in email, your app extracts the token
session = flow.redeem(token: "magic-link-token-from-url")
# Use session token for authenticated requests
client = Fizzy.client(auth: Fizzy::CookieAuth.new(session["session_token"]))

Defined Under Namespace

Classes: NullAuth

Instance Method Summary collapse

Constructor Details

#initialize(base_url: Config::DEFAULT_BASE_URL, hooks: nil) ⇒ MagicLinkFlow

Returns a new instance of MagicLinkFlow.

Parameters:

  • base_url (String) (defaults to: Config::DEFAULT_BASE_URL)

    Fizzy API base URL

  • hooks (Hooks, nil) (defaults to: nil)

    observability hooks



23
24
25
26
27
# File 'lib/fizzy/magic_link_flow.rb', line 23

def initialize(base_url: Config::DEFAULT_BASE_URL, hooks: nil)
  @config = Config.new(base_url: base_url)
  @hooks = hooks || NoopHooks.new
  @http = Http.new(config: @config, auth_strategy: NullAuth.new, hooks: @hooks)
end

Instance Method Details

#redeem(token:) ⇒ Hash

Step 2: Redeem a magic link token to get a session.

Parameters:

  • token (String)

    the magic link token from the URL

Returns:

  • (Hash)

    response containing session_token and user info



42
43
44
45
# File 'lib/fizzy/magic_link_flow.rb', line 42

def redeem(token:)
  response = @http.post("/sessions/redeem", body: { token: token })
  response.json
end

Step 1: Request a magic link email.

Parameters:

  • email (String)

    the user’s email address

Returns:

  • (Hash)

    response from the API



33
34
35
36
# File 'lib/fizzy/magic_link_flow.rb', line 33

def request_magic_link(email:)
  response = @http.post("/sessions", body: { email: email })
  response.json
end