Skip to content

Your First Application Setup

This guide walks you through connecting your first application to Skycloak. We’ll create a simple web application that can authenticate users through your Keycloak cluster.

By the end of this tutorial, you’ll have:

  • A working Keycloak cluster
  • A configured application client
  • A simple web page that can log users in and out
  • Understanding of how authentication flows work
  • A Skycloak account (sign up for free at skycloak.io)
  • Basic knowledge of HTML and JavaScript
  • A text editor or IDE
  1. Log into Skycloak and go to your dashboard

  2. Click “Create Cluster” - you’ll see the cluster creation form

  3. Choose your settings:

    • Name: “My First Cluster” (or whatever you prefer)
    • Region: Choose the closest to your users
    • Size: Small (perfect for getting started)
    • Keycloak Version: Use the latest version (recommended)
  4. Click “Create Cluster” and wait 2-3 minutes for setup

Once your cluster is ready:

  1. Click on your cluster in the dashboard
  2. Click “Open Admin Console” - this opens the Keycloak admin interface
  3. You are signed in automatically with your Skycloak account via Admin Console SSO, there is no separate admin password to enter

What is the Keycloak Console? Think of it as the “control panel” for your authentication system. Here you’ll create realms (like separate “environments”), configure applications, and manage users.

A realm is like a separate workspace for your applications. It keeps different projects isolated from each other.

  1. Hover over “Master” dropdown (top-left corner)
  2. Click “Add realm”
  3. Name it: “my-app” (use lowercase, no spaces)
  4. Click “Create”

A client represents your application in Keycloak. It defines how your app will communicate with Keycloak.

  1. Go to “Clients” section (left sidebar)

  2. Click “Create”

  3. Fill in the details:

    • Client ID: “my-web-app”
    • Client Protocol: “openid-connect”
    • Root URL: “http://localhost:8080” (we’ll test locally)
  4. Click “Save”

After creating the client, you’ll see the settings page:

  1. Set “Valid Redirect URIs”: http://localhost:8080/*
  2. Set “Web Origins”: http://localhost:8080
  3. Access Type: “public” (for web apps without server-side secrets)
  4. Click “Save”

What do these settings mean?

  • Redirect URIs: Where Keycloak can send users after login
  • Web Origins: Which websites can make requests to Keycloak
  • Access Type: How secure your client is (public = browser-based)

Let’s create a user account to test with:

  1. Go to “Users” section (left sidebar)

  2. Click “Add user”

  3. Fill in:

    • Username: “testuser”
    • Email: “test@example.com”
    • First Name: “Test”
    • Last Name: “User”
    • Email Verified: ON
  4. Click “Save”

  1. Go to “Credentials” tab
  2. Set Password: “password123”
  3. Temporary: OFF (so user doesn’t need to change it)
  4. Click “Set Password”

Now let’s create a simple web page that uses Keycloak for authentication.

Create a file called index.html:

<!DOCTYPE html>
<html>
<head>
    <title>My First Skycloak App</title>
    <script src="https://cdn.jsdelivr.net/npm/keycloak-js@latest/dist/keycloak.min.js"></script>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        .container { max-width: 600px; }
        .user-info { background: #f0f0f0; padding: 20px; border-radius: 5px; }
        button { padding: 10px 20px; margin: 10px 0; }
        .login-button { background: #007bff; color: white; border: none; border-radius: 5px; }
        .logout-button { background: #dc3545; color: white; border: none; border-radius: 5px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Welcome to My App!</h1>
        
        <div id="not-authenticated" style="display: none;">
            <p>Please log in to continue:</p>
            <button class="login-button" onclick="login()">Login</button>
        </div>
        
        <div id="authenticated" style="display: none;">
            <div class="user-info">
                <h3>Hello, <span id="username"></span>!</h3>
                <p><strong>Email:</strong> <span id="email"></span></p>
                <p><strong>Full Name:</strong> <span id="fullName"></span></p>
            </div>
            <button class="logout-button" onclick="logout()">Logout</button>
        </div>
    </div>

    <script>
        // Replace these with your actual values from Skycloak
        const keycloak = new Keycloak({
            url: 'https://your-cluster-url.skycloak.io',  // Your cluster URL
            realm: 'my-app',                              // The realm you created
            clientId: 'my-web-app'                        // The client you created
        });

        // Initialize Keycloak
        keycloak.init({ onLoad: 'check-sso' }).then(function(authenticated) {
            if (authenticated) {
                showUserInfo();
            } else {
                showLoginButton();
            }
        }).catch(function(error) {
            console.error('Failed to initialize Keycloak:', error);
        });

        function login() {
            keycloak.login();
        }

        function logout() {
            keycloak.logout();
        }

        function showLoginButton() {
            document.getElementById('not-authenticated').style.display = 'block';
            document.getElementById('authenticated').style.display = 'none';
        }

        function showUserInfo() {
            document.getElementById('not-authenticated').style.display = 'none';
            document.getElementById('authenticated').style.display = 'block';
            
            // Get user information from the token
            const token = keycloak.tokenParsed;
            document.getElementById('username').textContent = token.preferred_username;
            document.getElementById('email').textContent = token.email || 'Not provided';
            document.getElementById('fullName').textContent = (token.given_name + ' ' + token.family_name) || 'Not provided';
        }
    </script>
</body>
</html>

Important: Replace your-cluster-url.skycloak.io with your actual cluster URL from the Skycloak dashboard.

You can’t just open the HTML file directly - you need a web server. Here are easy options:

Option 1: Python (if you have Python installed)

# Navigate to your folder with index.html
cd /path/to/your/file

# Python 3
python -m http.server 8080

# Python 2
python -m SimpleHTTPServer 8080

Option 2: Node.js (if you have Node.js installed)

# Install a simple server
npm install -g http-server

# Run it
http-server -p 8080

Option 3: PHP (if you have PHP installed)

php -S localhost:8080
  1. Open your browser and go to http://localhost:8080
  2. Click “Login” - you should be redirected to Keycloak
  3. Enter your test credentials:
    • Username: testuser
    • Password: password123
  4. You should be redirected back to your app and see your user information
  5. Click “Logout” to test the logout flow

When you clicked “Login”, here’s what happened:

  1. Your app redirected you to Keycloak’s login page
  2. Keycloak verified your username and password
  3. Keycloak created tokens containing your user information
  4. Keycloak redirected you back to your app with the tokens
  5. Your app verified the tokens and logged you in

Keycloak gives your app three types of tokens:

  • Access Token: Proves you’re authenticated (like a temporary ID card)
  • ID Token: Contains your basic profile information
  • Refresh Token: Lets your app get new tokens when they expire

Congratulations! You’ve successfully: ✅ Created a Keycloak cluster ✅ Configured a realm and client ✅ Built a working authentication flow ✅ Tested login and logout

  1. Add More Users: Create additional test accounts with different information
  2. Customize the Login Page: Learn about branding customization
  3. Add User Registration: Enable users to create their own accounts
  4. Protect API Endpoints: Learn how to secure backend APIs
  5. Add Social Login: Connect Google, GitHub, or other social providers

“Invalid redirect URI” error:

  • Check that your Redirect URIs in the client settings include http://localhost:8080/*

Login page doesn’t load:

  • Verify your cluster URL is correct
  • Make sure your cluster is running (check the Skycloak dashboard)

Can’t log in with test user:

  • Verify the username and password are correct
  • Check that the user is in the correct realm

CORS errors in browser console:

  • Make sure Web Origins includes http://localhost:8080
  • Check that you’re accessing the app via http://localhost:8080, not file://

Need more help? Contact our support team at support@skycloak.io!