signInWithOtp method
Log in a user using magiclink or a one-time password (OTP).
If the {{ .ConfirmationURL }}
variable is specified in the email template, a magiclink will be sent.
If the {{ .Token }}
variable is specified in the email template, an OTP will be sent.
If you're using phone sign-ins, only an OTP will be sent. You won't be able to send a magiclink for phone sign-ins.
If shouldCreateUser
is set to false, this method will not create a new user. Defaults to true.
emailRedirectTo
can be used to specify the redirect URL embedded in the email link
data
can be used to set the user's metadata, which maps to the auth.users.user_metadata
column.
captchaToken
Verification token received when the user completes the captcha on the site.
channel
Messaging channel to use (e.g. whatsapp or sms)
Implementation
Future<void> signInWithOtp({
String? email,
String? phone,
String? emailRedirectTo,
bool? shouldCreateUser,
Map<String, dynamic>? data,
String? captchaToken,
OtpChannel channel = OtpChannel.sms,
}) async {
_removeSession();
if (email != null) {
String? codeChallenge;
if (_flowType == AuthFlowType.pkce) {
assert(_asyncStorage != null,
'You need to provide asyncStorage to perform pkce flow.');
final codeVerifier = generatePKCEVerifier();
await _asyncStorage!.setItem(
key: '${Constants.defaultStorageKey}-code-verifier',
value: codeVerifier);
codeChallenge = generatePKCEChallenge(codeVerifier);
}
await _fetch.request(
'$_url/otp',
RequestMethodType.post,
options: GotrueRequestOptions(
headers: _headers,
redirectTo: emailRedirectTo,
body: {
'email': email,
'data': data ?? {},
'create_user': shouldCreateUser ?? true,
'gotrue_meta_security': {'captcha_token': captchaToken},
'code_challenge': codeChallenge,
'code_challenge_method': codeChallenge != null ? 's256' : null,
},
),
);
return;
}
if (phone != null) {
final body = {
'phone': phone,
'data': data ?? {},
'create_user': shouldCreateUser ?? true,
'gotrue_meta_security': {'captcha_token': captchaToken},
'channel': channel.name,
};
final fetchOptions = GotrueRequestOptions(headers: _headers, body: body);
await _fetch.request(
'$_url/otp',
RequestMethodType.post,
options: fetchOptions,
);
return;
}
throw AuthException(
'You must provide either an email, phone number, a third-party provider or OpenID Connect.',
);
}