Class: Stytch::MagicLinks

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

Defined Under Namespace

Classes: Email

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestHelper

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

Constructor Details

#initialize(connection) ⇒ MagicLinks

Returns a new instance of MagicLinks.



16
17
18
19
20
# File 'lib/stytch/magic_links.rb', line 16

def initialize(connection)
  @connection = connection

  @email = Stytch::MagicLinks::Email.new(@connection)
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



14
15
16
# File 'lib/stytch/magic_links.rb', line 14

def email
  @email
end

Instance Method Details

#authenticate(token:, attributes: nil, options: nil, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil, code_verifier: nil) ⇒ Object

Authenticate a User given a Magic Link. This endpoint verifies that the Magic Link token is valid, hasn’t expired or been previously used, and any optional security settings such as IP match or user agent match are satisfied.

Parameters:

token

The Magic Link ‘token` from the `?token=` query parameter in the URL.

The redirect URL will look like `https://example.com/authenticate?stytch_token_type=magic_links&token=rM_kw42CWBhsHLF62V75jELMbvJ87njMe3tFVj7Qupu7`

In the redirect URL, the `stytch_token_type` will be `magic_link`. See [here](https://stytch.com/docs/guides/dashboard/redirect-urls) for more detail.

The type of this field is String.

attributes

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

options

Specify optional security settings. The type of this field is nilable Options (object).

session_token

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

session_duration_minutes

Set the session lifetime to be this many minutes from now. This will start a new session if one doesn’t already exist, returning both an opaque ‘session_token` and `session_jwt` for this session. Remember that the `session_jwt` will have a fixed lifetime of five minutes regardless of the underlying session duration, and will need to be refreshed over time.

This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).

If a ‘session_token` or `session_jwt` is provided then a successful authentication will continue to extend the session this many minutes.

If the ‘session_duration_minutes` parameter is not specified, a Stytch session will not be created. The type of this field is nilable Integer.

session_jwt

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

session_custom_claims

Add a custom claims map to the Session being authenticated. Claims are only created if a Session is initialized by providing a value in ‘session_duration_minutes`. Claims will be included on the Session object and in the JWT. To update a key in an existing Session, supply a new value. To delete a key, supply a null value.

Custom claims made with reserved claims (“iss”, “sub”, “aud”, “exp”, “nbf”, “iat”, “jti”) will be ignored. Total custom claims size cannot exceed four kilobytes. The type of this field is nilable object.

code_verifier

A base64url encoded one time secret used to validate that the request starts and ends on the same device. 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.

method_id

The ‘email_id` or `phone_id` involved in the given authentication. The type of this field is String.

session_token

A secret token for a given Stytch Session. The type of this field is String.

session_jwt

The JSON Web Token (JWT) for a given Stytch Session. The type of this field is String.

user

The ‘user` object affected by this API call. See the [Get user endpoint](stytch.com/docs/api/get-user) for complete response field details. The type of this field is User (object).

reset_sessions

Indicates if all other of the User’s Sessions need to be reset. You should check this field if you aren’t using Stytch’s Session product. If you are using Stytch’s Session product, we revoke the User’s other sessions for you. 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.

session

If you initiate a Session, by including ‘session_duration_minutes` in your authenticate call, you’ll receive a full Session object in the response.

See [GET sessions](stytch.com/docs/api/session-get) for complete response fields.

The type of this field is nilable Session (object).



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/stytch/magic_links.rb', line 96

def authenticate(
  token:,
  attributes: nil,
  options: nil,
  session_token: nil,
  session_duration_minutes: nil,
  session_jwt: nil,
  session_custom_claims: nil,
  code_verifier: nil
)
  headers = {}
  request = {
    token: token
  }
  request[:attributes] = attributes unless attributes.nil?
  request[:options] = options unless options.nil?
  request[:session_token] = session_token unless session_token.nil?
  request[:session_duration_minutes] = session_duration_minutes unless session_duration_minutes.nil?
  request[:session_jwt] = session_jwt unless session_jwt.nil?
  request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil?
  request[:code_verifier] = code_verifier unless code_verifier.nil?

  post_request('/v1/magic_links/authenticate', request, headers)
end

#create(user_id:, expiration_minutes: nil, attributes: nil) ⇒ Object

Create an embeddable Magic Link token for a User. Access to this endpoint is restricted. To enable it, please send us a note at [email protected].

### Next steps Send the returned ‘token` value to the end user in a link which directs to your application. When the end user follows your link, collect the token, and call [Authenticate Magic Link](stytch.com/docs/api/authenticate-magic-link) to complete authentication.

Parameters:

user_id

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

expiration_minutes

Set the expiration for the Magic Link ‘token` in minutes. By default, it expires in 1 hour. The minimum expiration is 5 minutes and the maximum is 7 days (10080 mins). The type of this field is nilable Integer.

attributes

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

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.

token

The Magic Link ‘token` that you’ll include in your contact method of choice, e.g. email or SMS. 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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/stytch/magic_links.rb', line 151

def create(
  user_id:,
  expiration_minutes: nil,
  attributes: nil
)
  headers = {}
  request = {
    user_id: user_id
  }
  request[:expiration_minutes] = expiration_minutes unless expiration_minutes.nil?
  request[:attributes] = attributes unless attributes.nil?

  post_request('/v1/magic_links', request, headers)
end