Class: WorkOS::Passwordless

Inherits:
Object
  • Object
show all
Defined in:
lib/workos/passwordless.rb

Overview

Passwordless authentication sessions (magic-link).

session = client.passwordless.create_session(email: "user@example.com")
client.passwordless.send_session(session.id)

Defined Under Namespace

Classes: PasswordlessSession

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Passwordless

Returns a new instance of Passwordless.



31
32
33
# File 'lib/workos/passwordless.rb', line 31

def initialize(client)
  @client = client
end

Instance Method Details

#create_session(email:, type: "MagicLink", redirect_uri: nil, state: nil, connection: nil, expires_in: nil, request_options: {}) ⇒ PasswordlessSession

Create a passwordless session.

Parameters:

  • email (String)

    Email of the user to authenticate.

  • type (String) (defaults to: "MagicLink")

    Session type. Currently only “MagicLink” is supported.

  • redirect_uri (String, nil) (defaults to: nil)

    Where to redirect the user after auth.

  • state (String, nil) (defaults to: nil)

    Arbitrary state echoed back on redirect.

  • connection (String, nil) (defaults to: nil)

    Specific connection ID to use.

  • expires_in (Integer, nil) (defaults to: nil)

    Lifetime in seconds.

  • request_options (Hash) (defaults to: {})

    Per-request overrides.

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/workos/passwordless.rb', line 45

def create_session(email:, type: "MagicLink", redirect_uri: nil, state: nil, connection: nil, expires_in: nil, request_options: {})
  body = {
    "email" => email,
    "type" => type,
    "redirect_uri" => redirect_uri,
    "state" => state,
    "connection" => connection,
    "expires_in" => expires_in
  }.compact
  response = @client.request(method: :post, path: "/passwordless/sessions", auth: true, body: body, request_options: request_options)
  PasswordlessSession.from_hash(JSON.parse(response.body))
end

#send_session(session_id, request_options: {}) ⇒ Hash

Send the magic-link email for an existing passwordless session.

Parameters:

  • session_id (String)

    Unique identifier of the passwordless session.

  • request_options (Hash) (defaults to: {})

    Per-request overrides.

Returns:

  • (Hash)

    Server response payload.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/workos/passwordless.rb', line 63

def send_session(session_id, request_options: {})
  response = @client.request(
    method: :post,
    path: "/passwordless/sessions/#{WorkOS::Util.encode_path(session_id)}/send",
    auth: true,
    body: {},
    request_options: request_options
  )
  JSON.parse(response.body || "{}")
rescue JSON::ParserError
  {}
end