Skip to content

Instantly share code, notes, and snippets.

@Kasun002
Created January 30, 2024 06:21
Show Gist options
  • Save Kasun002/31ab9fe8e3d8709d6790aaa1357305b9 to your computer and use it in GitHub Desktop.
Save Kasun002/31ab9fe8e3d8709d6790aaa1357305b9 to your computer and use it in GitHub Desktop.
Configure Keycloak with PG SQL for role based authentication
version: '3.4'
volumes:
postgres_data:
driver: local
services:
postgres:
image: postgres:14.1
container_name: keycloak-nodejs-example-postgres
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: root
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
keycloak:
image: jboss/keycloak:15.0.2
container_name: keycloak-nodejs-example-keycloak
environment:
DB_VENDOR: POSTGRES
DB_ADDR: postgres
DB_DATABASE: keycloak
DB_USER: keycloak
DB_PASSWORD: root
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin
command:
- "-Dkeycloak.profile.feature.upload_scripts=enabled"
- "-Dkeycloak.migration.action=import"
- "-Dkeycloak.migration.provider=dir"
- "-Dkeycloak.migration.dir=/opt/jboss/keycloak/import"
- "-Dkeycloak.migration.strategy=OVERWRITE_EXISTING"
ports:
- 8080:8080
volumes:
- "./docker/import_realm_users:/opt/jboss/keycloak/import"
depends_on:
- postgres
@Kasun002
Copy link
Author

Kasun002 commented Jan 30, 2024

      - "-Dkeycloak.migration.action=import" // This command will allow to import of data from specified sources from to keycloak
      - "-Dkeycloak.migration.provider=dir" // Import data from the directory 
      - "-Dkeycloak.migration.dir=/opt/jboss/keycloak/import" // Specify directory to import data
      - "-Dkeycloak.migration.strategy=OVERWRITE_EXISTING" // If data is conflicting with existing data it will override existing data when this command enables 

@Kasun002
Copy link
Author

Role-Based Authentication System

This project implements a role-based authentication system using Node.js, Keycloak, and PostgreSQL.

Steps to Implement

1. Setup PostgreSQL Database

  • Install PostgreSQL
  • Create a new database
  • Create users and roles tables
  • Insert users and roles data

2. Setup Keycloak

  • Install Keycloak
  • Create a new realm
  • Create roles (admin, manager, editor)
  • Create users and assign them roles

3. Setup Node.js Application

  • Initialize a new Node.js project
  • Install necessary packages (express, keycloak-connect, pg)

4. Integrate Keycloak with Node.js

  • Configure keycloak-connect middleware in your Node.js application
  • Protect your routes with Keycloak

5. Implement Role-Based Access Control (RBAC)

  • Create middleware to check the user's role
  • Use this middleware in your routes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment