Account Linking
This guide makes sure that signing in with Google and signing in with the local account lead to the same Keycloak user. Without it, the first Google login creates a second user, and every application sees two different people.
Prerequisites
- The
homelabrealm, groups, and local users exist (see Realm, Groups and Users). - The
googleIdentity Provider is added (see Google Identity Provider). - Each local user has the same email as their Google account, Email verified is
ON, and the user has a local password (set under Credentials).
How linking works
Keycloak runs a first broker login flow the first time someone signs in through an Identity Provider. By default this flow creates a new user when it cannot confirm the person. The custom flow in this guide changes that:
- Google confirms who the person is and sends the email.
- Keycloak looks for a local user with the same email.
- If found, Keycloak asks for that user's local password once, as proof of ownership, then links the Google account to the user.
- If not found, the login is rejected. No user is created.
After the first time, the person signs in with Google only and is never asked for the local password again.
Step 4 matters. With the default flow, any Google account that reaches the login page can get a new user in the realm, even when User registration is OFF. The custom flow closes that gap: only users the admin created can sign in.
Create the first login flow
Duplicate the default flow
- Go to Authentication → Flows.
- Open the menu (⋮) of
first broker loginand click Duplicate. - Set the name to
first-broker-login-homelaband click Duplicate.
Do not edit the built-in flow. Keep it as a reference.
Add the "Detect existing broker user" step
In the default flow, Create User If Unique is also the step that notices an existing user with the same email. This guide disables that step, so a separate step has to do the detection.
- In the User creation or linking sub-flow, click the + icon and choose Add step (shown as Add execution in some versions).
- Select Detect existing broker user and click Add.
- Drag the new step above Handle Existing Account. The order matters, because the steps below need the result of this one.
Authenticator names can differ slightly between Keycloak versions. If Detect existing broker user is missing from the list, check the version of your Keycloak and its release notes before using another step.
Adjust the steps
Set the requirements in first-broker-login-homelab as follows. Indented rows are inside the sub-flow above them.
| Step | Requirement | Purpose |
|---|---|---|
| Review Profile | Disabled | No manual profile page that could lead to a new user. |
| User creation or linking (sub-flow) | Required | Keep as is. |
| Detect existing broker user | Required | Finds the local user with the same email. |
| Create User If Unique | Disabled | Never create a user automatically. |
| Handle Existing Account (sub-flow) | Required | Runs the confirmation and the password check. |
| Confirm link existing account | Required | Shows the "Account already exists" page. |
| Account verification options (sub-flow) | Required | Keep as is. |
| Verify existing account by Email | Disabled | Needs SMTP, and would skip the password check. |
| Verify Existing Account by Re-authentication (sub-flow) | Required | Asks for the local password once. |
| First Broker Login - Conditional Organization (sub-flow) | Leave as is | Only used when Organizations are enabled. |
Leave the steps inside Verify Existing Account by Re-authentication as they are: the username and password form (Required) and the conditional OTP form. If the user has OTP configured, Keycloak asks for the code during linking too.
Changes to a requirement are saved as soon as you pick a value in the dropdown. There is no separate save button.
Finished flow:

Do not add Automatically Set Existing User. It links silently without any proof of ownership, which defeats the purpose of the password check. Also skip Confirm Override Existing Link, which is only used when a user is already linked to a different Google account.
Use the flow in the Identity Provider
- Go to Identity providers → google → Advanced settings.
- Set First login flow override to
first-broker-login-homelab. - Click Save.
Also confirm that Trust Email is ON on the same page. It was set in Google Identity Provider.
Onboard a user
Linking needs the admin to prepare the user first. For each person:
Admin, once per user
- Create the local user with the exact Google email and Email verified
ON(see Realm, Groups and Users). - Set a local password under Credentials → Set password with Temporary
OFF. - Join the user to the right groups.
- Give the person their username and password.
User, once
- Open
https://auth.rizwan.my.id/realms/homelab/account(or sign in to any application through SSO). - Click Google and sign in.
- On the Account already exists page, confirm and enter the local password.
After that the person signs in with Google only. The local password stays as a backup login. If the person does not need it, set a long random password.
Test
First login with Google
Demo: first login with Google, confirming the link with the local password.
- Open a private browser window and go to
https://auth.rizwan.my.id/realms/homelab/account. - Click Google and sign in with the Google account that matches the local user's email.
- Keycloak shows Account already exists. Click Add to existing account.
- Enter the local password (the username is usually pre-filled), then click Sign in.
- You land in the account console, signed in as the local user.
This step asks for the local password set by the admin, not the Google password. If no local password exists yet, or a different one is typed, Keycloak shows Invalid username or password.
Second login with Google
Sign out, click Google again. This time you go straight in without a password.
Login with the local account
Sign out and sign in with the local username and password. You must land on the same user.
A Google account without a local user
Sign in with a Google account that has no matching local user. Keycloak shows an error page and does not create a user. This is the expected result.
Verify
- Open Users in the
homelabrealm and search by email. There must be exactly one user. - Open the user, then the Identity provider links tab. There is one row for
google. - Check that the groups and the Enabled and Email verified switches are unchanged.
Link manually (fallback)
Use this only when the Google email cannot be made the same as the local email.
- Sign in with the local account at
https://auth.rizwan.my.id/realms/homelab/account. - Open Account security → Linked accounts.
- Click Link account next to
googleand sign in with Google.
Fixing the email on the local user is usually the cleaner solution, since manual linking has to be repeated for every person.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
Invalid username or password on the link page | The local password is not set, or the Google password was typed. | Set the password under the user's Credentials tab with Temporary OFF, and enter that one. |
| Google login creates a new user | First login flow override on google is still the default. | Set it to first-broker-login-homelab, delete the duplicate user, and log in again. |
| Error after choosing Google, no password page | No local user has the same email, or Detect existing broker user is missing. | Check the email on the local user (it must match exactly) and the steps in the flow table. |
| Error that the Google account is already linked | An earlier test linked it to another user. | Open that user, Identity provider links, and unlink google. Then try again. |
| The person forgot the local password | The password is only used for the first link. | Reset it under the user's Credentials tab with Temporary OFF. |
| The Google button works but the person lands on the wrong user | A duplicate user from earlier tests exists. | Search by email in Users, delete the extra user, and unlink and relink. |
What's next
The accounts are linked, but applications still cannot see group membership. Continue with the groups client scope and mapper, then create a client for each application.