I have a method that creates a new user and sets a custom password, everything works fine to the point where I try to log in with the new user and get an error that the login details are incorrect. Even the login history for that user does not show any entry stating that the password was incorrect, etc.
I have verified that the username and password both are correct, and it works when I do a subsequent setPassword() using the console.
Example:
User newUser = New User();
//...set user fields
insert newUser;
String password = getPassword(8);
system.setPassword(newUser.Id, password);
//...email user the password
When I run this in the console the same problem occurs:
User u = new User();
u.username = '[email protected]';
u.email = '[email protected]';
u.lastName = 'test';
u.firstName = 'test';
u.alias = 'test1236';
u.languagelocalekey = 'en_US';
u.localesidkey = UserInfo.getLocale();
u.emailEncodingKey = 'UTF-8';
u.timeZoneSidKey = 'America/Los_Angeles';
u.profileId = '00eG0000000FgyJ';
insert u;
system.setPassword(u.id, 'testpassword1');
*UPDATE
I can login after 5 minutes or so after the user is created, I have tried on two different orgs.
User u = new User(); u.username = '[email protected]'; u.email = '[email protected]'; u.lastName = 'test'; u.firstName = 'test'; u.alias = 'test1236'; u.languagelocalekey = 'en_US'; u.localesidkey = UserInfo.getLocale(); u.emailEncodingKey = 'UTF-8'; u.timeZoneSidKey = 'America/Los_Angeles'; u.profileId = '00eG0000000FgyJ'; insert u; system.setPassword(u.id, 'testtest1');