DEV Community

Cover image for React Authentication Made Easy with Keycloak
Mateus Aleixo
Mateus Aleixo

Posted on

React Authentication Made Easy with Keycloak

πŸ” A Simple and Modern Approach to Securing Your React Apps Using Keycloak

A simple and modern approach to securing your React apps using Keycloak and an open-source helper library.

⚑ Introduction

Securing modern web applications is a top priority. Keycloak is a powerful open-source identity and access management solution, but integrating it with React can be frustrating β€” especially for developers who want fast, reliable, and flexible authentication.

To solve this pain point, I created an open-source library called GJC Keycloak Manager. It simplifies the integration process so that any React developer can secure their app in minutes.

πŸš€ Features

  • πŸ” Seamless login/logout with Keycloak
  • βš›οΈ React Context integration
  • πŸ§‘β€πŸ’» Built with TypeScript
  • πŸ“¦ Lightweight and tree-shakable
  • 🌍 Easy setup, with full customization options

πŸ› οΈ How It Works

1. Install the library:

npm install keycloak-ease
Enter fullscreen mode Exit fullscreen mode

2. Create a Keycloak config file:

export const keycloakConfig = {
  url: "http://localhost:8080/auth",
  realm: "your-realm-name",
  clientId: "your-client-id",
};
Enter fullscreen mode Exit fullscreen mode

3. Initialize Keycloak in your application:

import { initKeycloak } from "keycloak-ease";
import useSessionStore from "keycloak-ease/dist/stores/SessionStore";
import { keycloakConfig } from "./keycloak-config";

// Initialize Keycloak
const keycloak = initKeycloak(keycloakConfig);

// Initialize the session store
useSessionStore.getState().initialize(
  { onLoad: "login-required" }, // or 'check-sso'
  keycloakConfig
);
Enter fullscreen mode Exit fullscreen mode

4. Use the session store in your components:

import useSessionStore from "keycloak-ease/dist/stores/SessionStore";

function MyComponent() {
  const { authenticated, userInfo, login, logout } = useSessionStore();

  if (!authenticated) {
    return <button onClick={login}>Login</button>;
  }

  return (
    <div>
      <p>Welcome, {userInfo?.name || userInfo?.email}</p>
      <button onClick={logout}>Logout</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

πŸ“š Full Setup Guide

If you’re new to Keycloak or want to see how to configure everything, check out the step-by-step guide here:

πŸ‘‰ GitHub Tutorial

πŸ’‘ Why I Built This

As a full-stack developer working with microservices and secure applications, I needed a quick and reusable way to implement Keycloak authentication without repeating boilerplate code across projects.

This library was born from real-world needs β€” and now it’s available for others.

πŸ“ˆ Want to Contribute?

I’d love to hear your feedback!

Feel free to:

  • ⭐ Star the repository
  • πŸ“₯ Open issues
  • πŸ§ͺ Suggest features
  • πŸ”§ Submit pull requests

πŸ‘‰ GitHub Repo

πŸ™Œ About the Author

I’m Mateus Alves, a Brazilian full-stack developer passionate about clean architecture, security, and scalable solutions.

Connect with me on GitHub or LinkedIn.


✨ Let’s Make Authentication Easier, Together.

Top comments (0)