All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.stytch.java.consumer.api.passwordsemail.PasswordsEmail.kt Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package com.stytch.java.consumer.api.passwordsemail

// !!!
// WARNING: This file is autogenerated
// Only modify code within MANUAL() sections
// or your changes may be overwritten later!
// !!!

import com.squareup.moshi.Moshi
import com.stytch.java.common.InstantAdapter
import com.stytch.java.common.StytchResult
import com.stytch.java.consumer.models.passwordsemail.ResetRequest
import com.stytch.java.consumer.models.passwordsemail.ResetResponse
import com.stytch.java.consumer.models.passwordsemail.ResetStartRequest
import com.stytch.java.consumer.models.passwordsemail.ResetStartResponse
import com.stytch.java.http.HttpClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.CompletableFuture

public interface Email {
    /**
     * Initiates a password reset for the email address provided. This will trigger an email to be sent to the address,
     * containing a magic link that will allow them to set a new password and authenticate.
     */
    public suspend fun resetStart(data: ResetStartRequest): StytchResult

    /**
     * Initiates a password reset for the email address provided. This will trigger an email to be sent to the address,
     * containing a magic link that will allow them to set a new password and authenticate.
     */
    public fun resetStart(
        data: ResetStartRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Initiates a password reset for the email address provided. This will trigger an email to be sent to the address,
     * containing a magic link that will allow them to set a new password and authenticate.
     */
    public fun resetStartCompletable(data: ResetStartRequest): CompletableFuture>

    /**
     * Reset the user’s password and authenticate them. This endpoint checks that the magic link `token` is valid, hasn’t
     * expired, or already been used – and can optionally require additional security settings, such as the IP address and
     * user agent matching the initial reset request.
     *
     * The provided password needs to meet our password strength requirements, which can be checked in advance with the
     * password strength endpoint. If the token and password are accepted, the password is securely stored for future
     * authentication and the user is authenticated.
     *
     * Note that a successful password reset by email will revoke all active sessions for the `user_id`.
     */
    public suspend fun reset(data: ResetRequest): StytchResult

    /**
     * Reset the user’s password and authenticate them. This endpoint checks that the magic link `token` is valid, hasn’t
     * expired, or already been used – and can optionally require additional security settings, such as the IP address and
     * user agent matching the initial reset request.
     *
     * The provided password needs to meet our password strength requirements, which can be checked in advance with the
     * password strength endpoint. If the token and password are accepted, the password is securely stored for future
     * authentication and the user is authenticated.
     *
     * Note that a successful password reset by email will revoke all active sessions for the `user_id`.
     */
    public fun reset(
        data: ResetRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Reset the user’s password and authenticate them. This endpoint checks that the magic link `token` is valid, hasn’t
     * expired, or already been used – and can optionally require additional security settings, such as the IP address and
     * user agent matching the initial reset request.
     *
     * The provided password needs to meet our password strength requirements, which can be checked in advance with the
     * password strength endpoint. If the token and password are accepted, the password is securely stored for future
     * authentication and the user is authenticated.
     *
     * Note that a successful password reset by email will revoke all active sessions for the `user_id`.
     */
    public fun resetCompletable(data: ResetRequest): CompletableFuture>
}

internal class EmailImpl(
    private val httpClient: HttpClient,
    private val coroutineScope: CoroutineScope,
) : Email {
    private val moshi = Moshi.Builder().add(InstantAdapter()).build()

    override suspend fun resetStart(data: ResetStartRequest): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()

            val asJson = moshi.adapter(ResetStartRequest::class.java).toJson(data)
            httpClient.post("/v1/passwords/email/reset/start", asJson, headers)
        }

    override fun resetStart(
        data: ResetStartRequest,
        callback: (StytchResult) -> Unit,
    ) {
        coroutineScope.launch {
            callback(resetStart(data))
        }
    }

    override fun resetStartCompletable(data: ResetStartRequest): CompletableFuture> =
        coroutineScope.async {
            resetStart(data)
        }.asCompletableFuture()

    override suspend fun reset(data: ResetRequest): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()

            val asJson = moshi.adapter(ResetRequest::class.java).toJson(data)
            httpClient.post("/v1/passwords/email/reset", asJson, headers)
        }

    override fun reset(
        data: ResetRequest,
        callback: (StytchResult) -> Unit,
    ) {
        coroutineScope.launch {
            callback(reset(data))
        }
    }

    override fun resetCompletable(data: ResetRequest): CompletableFuture> =
        coroutineScope.async {
            reset(data)
        }.asCompletableFuture()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy