Here is my not finished source that will do some "checks" then addUser if not exist, Currently I'm testing simply adding new user here is my writings
public void authenticate(AuthenticationFlowContext context) {
KeycloakSession session = context.getSession();
RealmModel realm = context.getRealm();
String username = "hello";
UserProvider users = session.userLocalStorage();
UserModel user = users.getUserByUsername(realm,username);
if (user == null) {
logger.infof("addingUser");
user = session.users().addUser(realm,username);
logger.infof("isEnabled()");
user.setEnabled(true);
user.setEmail("mail@mail.com");
user.setEmailVerified(true);
}
context.setUser(user);
context.success();
}
with this source when I try to login for the first time, it gets me error but user is created. Then if I try to login for the second time, it logs in successfully (shows me personal page)
06:05:12,633 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-8)
Unique index or primary
key violation: "UK_RU8TT6T700S9V50BU18WS5HA6_INDEX_B ON PUBLIC.USER_ENTITY(REALM_ID,
USERNAME) VALUES ('Customer', 'hello', 5)"; SQL statement:
2021-11-12T06:05:12.634069100Z insert into USER_ENTITY (CREATED_TIMESTAMP, EMAIL,
EMAIL_CONSTRAINT, EMAIL_VERIFIED, ENABLED, FEDERATION_LINK,
FIRST_NAME, LAST_NAME, NOT_BEFORE, REALM_ID, SERVICE_ACCOUNT_CLIENT_LINK, USERNAME,
ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23505-197]
2021-11-12T06:05:12.647493900Z
Additional information logs show me output twice for example
2021-11-12T06:05:12.610917800Z 06:05:12,610 INFO [class name] (default task-6) addingUser
2021-11-12T06:05:12.615587600Z 06:05:12,615 INFO [class name] (default task-8) addingUser
task 6 reaches log output logger.infof("isEnabled()");
task 8 not