Auth

Plugin to interact with the Supabase Auth API

To use it you need to install it to the SupabaseClient:

val supabase = createSupabaseClient(supabaseUrl, supabaseKey) {
install(Auth)
}

then you can use it like this:

val result = supabase.auth.signUpWith(Email) {
email = "example@email.com"
password = "password"
}

Types

Link copied to clipboard

Properties

Link copied to clipboard
abstract val admin: AdminApi

Access to the auth admin api where you can manage users. Service role access token is required. Import it via importAuthToken. Never share it publicly

Link copied to clipboard
abstract val apiVersion: Int
Link copied to clipboard

The cache for the code verifier. This is used for PKCE authentication. Can be customized via AuthConfig.codeVerifierCache

Link copied to clipboard
abstract val config: AuthConfig
Link copied to clipboard

Whether the sessionStatus session is getting refreshed automatically

Link copied to clipboard
abstract val mfa: MfaApi

Access to the mfa api where you can manage multi-factor authentication for the current user.

Link copied to clipboard
abstract val pluginKey: String
Link copied to clipboard
Link copied to clipboard

Returns the session manager instance

Link copied to clipboard
abstract val sessionStatus: StateFlow<SessionStatus>

Returns the current session status

Link copied to clipboard

Functions

Link copied to clipboard
abstract suspend fun awaitInitialization()

Blocks the current coroutine until the plugin is initialized.

Link copied to clipboard
abstract suspend fun clearSession()

Deletes the current session from storage and sets sessionStatus to SessionStatus.NotAuthenticated

Link copied to clipboard
open suspend fun close()
Link copied to clipboard

Returns the current access token, or null if no session is available

Link copied to clipboard

Returns the connected identities to the current user or null

Link copied to clipboard

Returns the current session or null

Link copied to clipboard

Returns the current user or null

Link copied to clipboard
abstract suspend fun exchangeCodeForSession(code: String, saveSession: Boolean = true): UserSession

Exchanges a code for a session. Used when using the FlowType.PKCE flow

Link copied to clipboard
abstract fun getOAuthUrl(provider: OAuthProvider, redirectUrl: String? = defaultRedirectUrl(), url: String = "authorize", additionalConfig: ExternalAuthConfigDefaults.() -> Unit = {}): String

Returns the url to use for oAuth

Link copied to clipboard
open suspend fun importAuthToken(accessToken: String, refreshToken: String = "", retrieveUser: Boolean = false, autoRefresh: Boolean = if(refreshToken.isNotBlank()) config.alwaysAutoRefresh else false)

Imports the jwt token and retrieves the user profile. Be aware auto-refreshing is not available when importing only a jwt token.

Link copied to clipboard
abstract suspend fun importSession(session: UserSession, autoRefresh: Boolean = config.alwaysAutoRefresh, source: SessionSource = SessionSource.Unknown)

Imports a user session and starts auto-refreshing if autoRefresh is true

Link copied to clipboard
open fun init()
Link copied to clipboard
abstract suspend fun linkIdentity(provider: OAuthProvider, redirectUrl: String? = defaultRedirectUrl(), config: ExternalAuthConfigDefaults.() -> Unit = {}): String?

Links an OAuth Identity to an existing user.

Link copied to clipboard
abstract suspend fun loadFromStorage(autoRefresh: Boolean = config.alwaysAutoRefresh): Boolean

Retrieves the latest session from storage and starts auto-refreshing if autoRefresh is true or AuthConfig.alwaysAutoRefresh as the default parameter

Link copied to clipboard
abstract suspend fun parseErrorResponse(response: HttpResponse): RestException
Link copied to clipboard
fun Auth.parseFragmentAndImportSession(fragment: String, onSessionSuccess: (UserSession) -> Unit = {})
Link copied to clipboard

Parses a session from a fragment.

Link copied to clipboard

Parses a session from an url.

Link copied to clipboard
abstract suspend fun reauthenticate()

Sends a nonce to the user's email (preferred) or phone

Link copied to clipboard
abstract suspend fun refreshCurrentSession()

Refreshes the current session

Link copied to clipboard
abstract suspend fun refreshSession(refreshToken: String): UserSession

Refreshes a session using the refresh token

Link copied to clipboard
abstract suspend fun resendEmail(type: OtpType.Email, email: String, captchaToken: String? = null)

Resends an existing signup confirmation email, email change email

Link copied to clipboard
abstract suspend fun resendPhone(type: OtpType.Phone, phone: String, captchaToken: String? = null)

Resends an existing SMS OTP or phone change OTP.

Link copied to clipboard
abstract suspend fun resetPasswordForEmail(email: String, redirectUrl: String? = defaultRedirectUrl(), captchaToken: String? = null)

Sends a password reset email to the user with the specified email

Link copied to clipboard
open fun resolveUrl(path: String): String
Link copied to clipboard
abstract suspend fun retrieveSSOUrl(redirectUrl: String? = defaultRedirectUrl(), config: SSO.Config.() -> Unit): SSO.Result

Retrieves the sso url for the given config

Link copied to clipboard
abstract suspend fun retrieveUser(jwt: String): UserInfo

Retrieves the user attached to the specified jwt

Link copied to clipboard
abstract suspend fun retrieveUserForCurrentSession(updateSession: Boolean = false): UserInfo

Retrieves the current user with the current session

Link copied to clipboard
expect fun Auth.setupPlatform()
actual fun Auth.setupPlatform()
actual fun Auth.setupPlatform()
actual fun Auth.setupPlatform()
actual fun Auth.setupPlatform()
actual fun Auth.setupPlatform()
Link copied to clipboard
abstract suspend fun signInAnonymously(data: JsonObject? = null, captchaToken: String? = null)

Signs in the user without any credentials. This will create a new user session with a new access token.

Link copied to clipboard
inline suspend fun <T : Any> Auth.signInAnonymously(data: T, captchaToken: String? = null)

Signs in the user without any credentials. This will create a new user session with a new access token.

Link copied to clipboard
abstract suspend fun <C, R, Provider : AuthProvider<C, R>> signInWith(provider: Provider, redirectUrl: String? = defaultRedirectUrl(), config: C.() -> Unit? = null)

Signs in the user with the specified provider

Link copied to clipboard
abstract suspend fun signOut(scope: SignOutScope = SignOutScope.LOCAL)

Signs out the current user, which means sessionStatus will be SessionStatus.NotAuthenticated and the access token will be revoked

Link copied to clipboard
abstract suspend fun <C, R, Provider : AuthProvider<C, R>> signUpWith(provider: Provider, redirectUrl: String? = defaultRedirectUrl(), config: C.() -> Unit? = null): R?

Signs up a new user with the specified provider

Link copied to clipboard
abstract suspend fun startAutoRefreshForCurrentSession()

Starts auto refreshing the current session

Link copied to clipboard

Stops auto-refreshing the current session

Link copied to clipboard
abstract suspend fun unlinkIdentity(identityId: String, updateLocalUser: Boolean = true)

Unlinks an OAuth Identity from an existing user.

Link copied to clipboard
abstract suspend fun updateUser(updateCurrentUser: Boolean = true, redirectUrl: String? = defaultRedirectUrl(), config: UserUpdateBuilder.() -> Unit): UserInfo

Modifies the current user

Link copied to clipboard
abstract suspend fun verifyEmailOtp(type: OtpType.Email, tokenHash: String, captchaToken: String? = null)

Verifies a email otp token hash received via email

abstract suspend fun verifyEmailOtp(type: OtpType.Email, email: String, token: String, captchaToken: String? = null)

Verifies a email otp

Link copied to clipboard
abstract suspend fun verifyPhoneOtp(type: OtpType.Phone, phone: String, token: String, captchaToken: String? = null)

Verifies a phone/sms otp