Class: Stytch::OTPs::Email

Inherits:
Object
  • Object
show all
Includes:
RequestHelper
Defined in:
lib/stytch/otps.rb

Instance Method Summary collapse

Methods included from RequestHelper

#delete_request, #get_request, #post_request, #put_request, #request_with_query_params

Constructor Details

#initialize(connection) ⇒ Email

Returns a new instance of Email.



448
449
450
# File 'lib/stytch/otps.rb', line 448

def initialize(connection)
  @connection = connection
end

Instance Method Details

#login_or_create(email:, expiration_minutes: nil, attributes: nil, create_user_as_pending: nil, locale: nil, login_template_id: nil, signup_template_id: nil) ⇒ Object

Send a one-time passcode (OTP) to a User using their email. If the email is not associated with a User already, a User will be created.

### Next steps

Collect the OTP which was delivered to the User. Call [Authenticate OTP](stytch.com/docs/api/authenticate-otp) using the OTP ‘code` along with the `phone_id` found in the response as the `method_id`.

Parameters:

email

The email address of the user to send the one-time passcode to. You may use [email protected] to test this endpoint, see [Testing](stytch.com/docs/home#resources_testing) for more detail. The type of this field is String.

expiration_minutes

Set the expiration for the one-time passcode, in minutes. The minimum expiration is 1 minute and the maximum is 10 minutes. The default expiration is 2 minutes. The type of this field is nilable Integer.

attributes

Provided attributes help with fraud detection. The type of this field is nilable Attributes (object).

create_user_as_pending

Flag for whether or not to save a user as pending vs active in Stytch. Defaults to false.

If true, users will be saved with status pending in Stytch's backend until authenticated.
If false, users will be created as active. An example usage of
a true flag would be to require users to verify their phone by entering the OTP code before creating
an account for them.

The type of this field is nilable Boolean.

locale

Used to determine which language to use when sending the user this delivery method. Parameter is a [IETF BCP 47 language tag](www.w3.org/International/articles/language-tags/), e.g. ‘“en”`.

Currently supported languages are English (‘“en”`), Spanish (`“es”`), and Brazilian Portuguese (`“pt-br”`); if no value is provided, the copy defaults to English.

Request support for additional languages [here](docs.google.com/forms/d/e/1FAIpQLScZSpAu_m2AmLXRT3F3kap-s_mcV6UTBitYn6CdyWP0-o7YjQ/viewform?usp=sf_link“)!

The type of this field is nilable +LoginOrCreateRequestLocale+ (string enum).
login_template_id

Use a custom template for login emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Login. The type of this field is nilable String.

signup_template_id

Use a custom template for sign-up emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Sign-up. The type of this field is nilable String.

Returns:

An object with the following fields:

request_id

Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is String.

user_id

The unique ID of the affected User. The type of this field is String.

email_id

The unique ID of a specific email address. The type of this field is String.

user_created

In ‘login_or_create` endpoints, this field indicates whether or not a User was just created. The type of this field is Boolean.

status_code

The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is Integer.



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/stytch/otps.rb', line 590

def (
  email:,
  expiration_minutes: nil,
  attributes: nil,
  create_user_as_pending: nil,
  locale: nil,
  login_template_id: nil,
  signup_template_id: nil
)
  headers = {}
  request = {
    email: email
  }
  request[:expiration_minutes] = expiration_minutes unless expiration_minutes.nil?
  request[:attributes] = attributes unless attributes.nil?
  request[:create_user_as_pending] = create_user_as_pending unless create_user_as_pending.nil?
  request[:locale] = locale unless locale.nil?
  request[:login_template_id] =  unless .nil?
  request[:signup_template_id] =  unless .nil?

  post_request('/v1/otps/email/login_or_create', request, headers)
end

#send(email:, expiration_minutes: nil, attributes: nil, locale: nil, user_id: nil, session_token: nil, session_jwt: nil, login_template_id: nil, signup_template_id: nil) ⇒ Object

Send a One-Time Passcode (OTP) to a User using their email. If you’d like to create a user and send them a passcode with one request, use our [log in or create endpoint](stytch.com/docs/api/log-in-or-create-user-by-email-otp).

### Add an email to an existing user This endpoint also allows you to add a new email address to an existing Stytch User. Including a ‘user_id`, `session_token`, or `session_jwt` in your Send one-time passcode by email request will add the new, unverified email address to the existing Stytch User. If the user successfully authenticates within 5 minutes, the new email address will be marked as verified and remain permanently on the existing Stytch User. Otherwise, it will be removed from the User object, and any subsequent login requests using that email address will create a new User.

### Next steps Collect the OTP which was delivered to the user. Call [Authenticate OTP](stytch.com/docs/api/authenticate-otp) using the OTP ‘code` along with the `email_id` found in the response as the `method_id`.

Parameters:

email

The email address of the user to send the one-time passcode to. You may use [email protected] to test this endpoint, see [Testing](stytch.com/docs/home#resources_testing) for more detail. The type of this field is String.

expiration_minutes

Set the expiration for the one-time passcode, in minutes. The minimum expiration is 1 minute and the maximum is 10 minutes. The default expiration is 2 minutes. The type of this field is nilable Integer.

attributes

Provided attributes help with fraud detection. The type of this field is nilable Attributes (object).

locale

Used to determine which language to use when sending the user this delivery method. Parameter is a [IETF BCP 47 language tag](www.w3.org/International/articles/language-tags/), e.g. ‘“en”`.

Currently supported languages are English (‘“en”`), Spanish (`“es”`), and Brazilian Portuguese (`“pt-br”`); if no value is provided, the copy defaults to English.

Request support for additional languages [here](docs.google.com/forms/d/e/1FAIpQLScZSpAu_m2AmLXRT3F3kap-s_mcV6UTBitYn6CdyWP0-o7YjQ/viewform?usp=sf_link“)!

The type of this field is nilable +SendRequestLocale+ (string enum).
user_id

The unique ID of a specific User. The type of this field is nilable String.

session_token

The ‘session_token` associated with a User’s existing Session. The type of this field is nilable String.

session_jwt

The ‘session_jwt` associated with a User’s existing Session. The type of this field is nilable String.

login_template_id

Use a custom template for login emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Login. The type of this field is nilable String.

signup_template_id

Use a custom template for sign-up emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Sign-up. The type of this field is nilable String.

Returns:

An object with the following fields:

request_id

Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is String.

user_id

The unique ID of the affected User. The type of this field is String.

email_id

The unique ID of a specific email address. The type of this field is String.

status_code

The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is Integer.



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/stytch/otps.rb', line 508

def send(
  email:,
  expiration_minutes: nil,
  attributes: nil,
  locale: nil,
  user_id: nil,
  session_token: nil,
  session_jwt: nil,
  login_template_id: nil,
  signup_template_id: nil
)
  headers = {}
  request = {
    email: email
  }
  request[:expiration_minutes] = expiration_minutes unless expiration_minutes.nil?
  request[:attributes] = attributes unless attributes.nil?
  request[:locale] = locale unless locale.nil?
  request[:user_id] = user_id unless user_id.nil?
  request[:session_token] = session_token unless session_token.nil?
  request[:session_jwt] = session_jwt unless session_jwt.nil?
  request[:login_template_id] =  unless .nil?
  request[:signup_template_id] =  unless .nil?

  post_request('/v1/otps/email/send', request, headers)
end