Skip to content

Commit

Permalink
Merge pull request #367 from supabase-community/rename-recovery
Browse files Browse the repository at this point in the history
Rename sendRecoveryEmail method and add PKCE support
  • Loading branch information
jan-tennert authored Dec 3, 2023
2 parents d08b24b + 6fb8077 commit 8a01d3b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ sealed interface Auth : MainPlugin<AuthConfig>, CustomSerializationPlugin {
/**
* Sends a password reset email to the user with the specified [email]
* @param email The email to send the password reset email to
* @param redirectUrl The redirect url to use. If you don't specify this, the platform specific will be use, like deeplinks on android.
* @param redirectUrl The redirect url to use. If you don't specify this, the platform specific will be used, like deeplinks on android.
* @throws RestException or one of its subclasses if receiving an error response
* @throws HttpRequestTimeoutException if the request timed out
* @throws HttpRequestException on network related issues
*/
suspend fun sendRecoveryEmail(email: String, redirectUrl: String? = null, captchaToken: String? = null)
suspend fun resetPasswordForEmail(email: String, redirectUrl: String? = null, captchaToken: String? = null)

/**
* Sends a nonce to the user's email (preferred) or phone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,32 @@ internal class AuthImpl(
}
}

override suspend fun sendRecoveryEmail(
override suspend fun resetPasswordForEmail(
email: String,
redirectUrl: String?,
captchaToken: String?
) {
require(email.isNotBlank()) {
"Email must not be blank"
}
val finalRedirectUrl = generateRedirectUrl(redirectUrl)
var codeChallenge: String? = null
if (this.config.flowType == FlowType.PKCE) {
val codeVerifier = generateCodeVerifier()
codeVerifierCache.saveCodeVerifier(codeVerifier)
codeChallenge = generateCodeChallenge(codeVerifier)
}
val body = buildJsonObject {
put("email", email)
captchaToken?.let {
putJsonObject("gotrue_meta_security") {
put("captcha_token", captchaToken)
}
}
codeChallenge?.let {
put("code_challenge", it)
put("code_challenge_method", "s256")
}
}.toString()
api.postJson("recover", body) {
finalRedirectUrl?.let { url.encodedParameters.append("redirect_to", it) }
Expand Down
2 changes: 1 addition & 1 deletion GoTrue/src/commonTest/kotlin/GoTrueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class GoTrueTest {
fun test_recovery() {
val client = createSupabaseClient()
runTest(dispatcher) {
client.auth.sendRecoveryEmail("[email protected]")
client.auth.resetPasswordForEmail("[email protected]")
client.close()
}
}
Expand Down

0 comments on commit 8a01d3b

Please sign in to comment.