signUp method
Creates a user from an email
and password
.
If Nhost is configured to not automatically activate new users, the
returned AuthResponse
will not contain a session. The user must first
activate their account by clicking an activation link sent to their email.
Throws an NhostException
if registration fails.
Implementation
@override
Future<AuthResponse> signUp({
required String email,
required String password,
String? locale,
String? defaultRole,
Map<String, Object?>? metadata,
List<String>? roles,
String? displayName,
String? redirectTo,
}) async {
log.finer('Attempting user registration');
final includeRoleOptions =
defaultRole != null || (roles != null && roles.isNotEmpty);
final options = {
if (metadata != null) 'metadata': metadata,
if (locale != null) 'locale': locale,
if (includeRoleOptions) 'defaultRole': defaultRole,
if (includeRoleOptions) 'allowedRoles': roles,
if (displayName != null) 'displayName': displayName,
if (redirectTo != null) 'redirectTo': redirectTo,
};
try {
final res = await _apiClient.post(
'/signup/email-password',
jsonBody: {
'email': email,
'password': password,
if (options.isNotEmpty) 'options': options,
},
responseDeserializer: AuthResponse.fromJson,
);
log.finer('Registration successful');
if (res.session?.accessToken != null) {
await setSession(res.session!);
return res;
} else {
// if AUTO_ACTIVATE_NEW_USERS is false
return AuthResponse(session: null);
}
} catch (e) {
log.finer('Registration failed');
rethrow;
}
}