Class: Backlex::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/backlex/auth.rb

Overview

Auth surface. In app mode (workspace set) calls target that workspace’s own auth pool (“/api/t/<slug>/auth/…”); otherwise the control plane.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Auth

Returns a new instance of Auth.



9
10
11
# File 'lib/backlex/auth.rb', line 9

def initialize(client)
  @client = client
end

Instance Method Details

#change_password(new_password, current_password, revoke_other_sessions: false) ⇒ Object

Change the signed-in user’s password (requires the current password).



70
71
72
73
74
75
76
# File 'lib/backlex/auth.rb', line 70

def change_password(new_password, current_password, revoke_other_sessions: false)
  @client.request("POST", "#{base}/change-password", {
                    "newPassword" => new_password,
                    "currentPassword" => current_password,
                    "revokeOtherSessions" => revoke_other_sessions
                  })
end

#list_sessionsObject

List the signed-in user’s active sessions (one row per device/login).



101
102
103
# File 'lib/backlex/auth.rb', line 101

def list_sessions
  @client.request("GET", "#{base}/list-sessions")
end

#providersObject

Public auth surface (provider list + policy flags).



121
122
123
# File 'lib/backlex/auth.rb', line 121

def providers
  @client.request("GET", "#{base}/providers")["data"]
end

#refreshObject

Mint a fresh access JWT from the stored session token (app mode).



65
66
67
# File 'lib/backlex/auth.rb', line 65

def refresh
  @client.request("POST", "#{base}/token/refresh", { "refreshToken" => @client.app_token })
end

#request_password_reset(email, redirect_to: nil) ⇒ Object

Clear the session; in app mode also drops the captured token. Send a password-reset email. redirect_to is the link target.



53
54
55
56
57
# File 'lib/backlex/auth.rb', line 53

def request_password_reset(email, redirect_to: nil)
  body = { "email" => email }
  body["redirectTo"] = redirect_to if redirect_to
  @client.request("POST", "#{base}/request-password-reset", body)
end

#reset_password(new_password, token) ⇒ Object

Complete a reset with the token from the email and a new password.



60
61
62
# File 'lib/backlex/auth.rb', line 60

def reset_password(new_password, token)
  @client.request("POST", "#{base}/reset-password", { "newPassword" => new_password, "token" => token })
end

#revoke_other_sessionsObject

Revoke every session except the current one (sign out other devices).



111
112
113
# File 'lib/backlex/auth.rb', line 111

def revoke_other_sessions
  @client.request("POST", "#{base}/revoke-other-sessions")
end

#revoke_session(token) ⇒ Object

Revoke one session by its token (from #list_sessions).



106
107
108
# File 'lib/backlex/auth.rb', line 106

def revoke_session(token)
  @client.request("POST", "#{base}/revoke-session", { "token" => token })
end

#revoke_sessionsObject

Revoke all sessions, including the current one.



116
117
118
# File 'lib/backlex/auth.rb', line 116

def revoke_sessions
  @client.request("POST", "#{base}/revoke-sessions")
end

#send_verification_email(email, callback_url: nil) ⇒ Object

Send an email-verification link.



84
85
86
87
88
# File 'lib/backlex/auth.rb', line 84

def send_verification_email(email, callback_url: nil)
  body = { "email" => email }
  body["callbackURL"] = callback_url if callback_url
  @client.request("POST", "#{base}/send-verification-email", body)
end

#send_verification_otp(email, type: "sign-in") ⇒ Object

Email a one-time numeric code (requires the email-otp provider). type is “sign-in” (default), “email-verification” or “forget-password”. Complete a sign-in with #sign_in_email_otp.



41
42
43
# File 'lib/backlex/auth.rb', line 41

def send_verification_otp(email, type: "sign-in")
  @client.request("POST", "#{base}/email-otp/send-verification-otp", { "email" => email, "type" => type })
end

#sessionObject

Current session payload, or { “user” => nil }.



96
97
98
# File 'lib/backlex/auth.rb', line 96

def session
  @client.request("GET", "#{base}/get-session")
end

#sign_in(email, password) ⇒ Object



19
20
21
# File 'lib/backlex/auth.rb', line 19

def (email, password)
  capture(@client.request("POST", "#{base}/sign-in/email", { "email" => email, "password" => password }))
end

#sign_in_email_otp(email, otp) ⇒ Object

Complete an email-OTP sign-in with the code from #send_verification_otp. In app mode the returned session token is captured.



47
48
49
# File 'lib/backlex/auth.rb', line 47

def (email, otp)
  capture(@client.request("POST", "#{base}/sign-in/email-otp", { "email" => email, "otp" => otp }))
end

Send a one-time sign-in link by email.



32
33
34
35
36
# File 'lib/backlex/auth.rb', line 32

def (email, callback_url: nil)
  body = { "email" => email }
  body["callbackURL"] = callback_url if callback_url
  @client.request("POST", "#{base}/sign-in/magic-link", body)
end

#sign_in_social(provider, callback_url: nil, error_callback_url: nil) ⇒ Object

Begin an OAuth sign-in; navigate the user to the returned URL.



24
25
26
27
28
29
# File 'lib/backlex/auth.rb', line 24

def (provider, callback_url: nil, error_callback_url: nil)
  body = { "provider" => provider, "disableRedirect" => true }
  body["callbackURL"] = callback_url if callback_url
  body["errorCallbackURL"] = error_callback_url if error_callback_url
  @client.request("POST", "#{base}/sign-in/social", body)
end

#sign_outObject



90
91
92
93
# File 'lib/backlex/auth.rb', line 90

def sign_out
  @client.request("POST", "#{base}/sign-out")
  @client.app_token = nil if workspace?
end

#sign_up(email, password, name = nil) ⇒ Object



13
14
15
16
17
# File 'lib/backlex/auth.rb', line 13

def (email, password, name = nil)
  body = { "email" => email, "password" => password }
  body["name"] = name if name
  capture(@client.request("POST", "#{base}/sign-up/email", body))
end

#tokenObject

Current workspace session token (app mode); persist and restore via Client.new(token:).



126
127
128
# File 'lib/backlex/auth.rb', line 126

def token
  @client.app_token
end

#token=(value) ⇒ Object

Restore a workspace session token (app mode).



131
132
133
# File 'lib/backlex/auth.rb', line 131

def token=(value)
  @client.app_token = value
end

#update_user(attributes) ⇒ Object

Update the signed-in user’s profile (e.g. name / image).



79
80
81
# File 'lib/backlex/auth.rb', line 79

def update_user(attributes)
  @client.request("POST", "#{base}/update-user", attributes)
end