signUp method
Creates a new user.
Be aware that if a user account exists in the system you may get back an error message that attempts to hide this information from the user. This method has support for PKCE via email signups. The PKCE flow cannot be used when autoconfirm is enabled.
Returns a logged-in session if the server has "autoconfirm" ON, but only a user if the server has "autoconfirm" OFF
email
is the user's email address
phone
is the user's phone number WITH international prefix
password
is the password of the user
data
sets User.userMetadata without an extra call to updateUser
channel
Messaging channel to use (e.g. whatsapp or sms)
Implementation
Future<AuthResponse> signUp({
String? email,
String? phone,
required String password,
String? emailRedirectTo,
Map<String, dynamic>? data,
String? captchaToken,
OtpChannel channel = OtpChannel.sms,
}) async {
assert((email != null && phone == null) || (email == null && phone != null),
'You must provide either an email or phone number');
_removeSession();
late final Map<String, dynamic> response;
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);
}
response = await _fetch.request(
'$_url/signup',
RequestMethodType.post,
options: GotrueRequestOptions(
headers: _headers,
redirectTo: emailRedirectTo,
body: {
'email': email,
'password': password,
'data': data,
'gotrue_meta_security': {'captcha_token': captchaToken},
'code_challenge': codeChallenge,
'code_challenge_method': codeChallenge != null ? 's256' : null,
},
),
);
} else if (phone != null) {
final body = {
'phone': phone,
'password': password,
'data': data,
'gotrue_meta_security': {'captcha_token': captchaToken},
'channel': channel.name,
};
final fetchOptions = GotrueRequestOptions(headers: _headers, body: body);
response = await _fetch.request('$_url/signup', RequestMethodType.post,
options: fetchOptions) as Map<String, dynamic>;
} else {
throw AuthException(
'You must provide either an email or phone number and a password');
}
final authResponse = AuthResponse.fromJson(response);
final session = authResponse.session;
if (session != null) {
_saveSession(session);
notifyAllSubscribers(AuthChangeEvent.signedIn);
}
return authResponse;
}