This is a demo for useing the keycloak authentication framework in an Angular project including a PostgreSQL database. Keycloak itself, as well as the database run on seperate docker container. Beware: This is a Linux/Ubuntu 20.04.6 LTS test!
The installation of the following prerequisites is not explained further. The respective version numbers only show with which this demo was performed and considered to be functional.
- Ubuntu (20.04.6)
- Docker (24.0.2)
- VSCode (1.78.2)
To store login information such as credentials, it is necessary to use a separate database. When Keycloak is running in dev mode, all data is stored only in the internal database of the container and is therefore lost when the container is stopped.
docker pull ubuntu/postgres
This pulls the least postgres container. (FYI: It is also possible to use a different version!)
To use the keycloak service to retrieve data in our Angular project we need to install it via npm.
npm install keycloak-angular keycloak-js
Angular is used for this demo project. However, any other language can be used to create a frontend.
- Install Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
- Update NPM
sudo npm install npm@latest -g
- Install the Angular CLI
sudo npm install -g @angular/cli
- Create a workspace and initial Angular app
ng new my-app
- Run the application
cd my-app
ng serve --open
There are 2 ways to use this project:
- a) Keycloak can be started directly as a container (data will be lost after stopping)
docker run -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=password quay.io/keycloak/keycloak start-dev
- b) Or via docker-compose (data will be stored via PostgreSQL). Make sure to cd into the directory of the docker-compose.yml file. The app.sh will start the compose and return a message if everything is up and running.
chmod +x ./app.sh
./app.sh
After both containers are up keycloak should be reachable under following url:
http://localhost:8080/
You can then login (with the credentials used in the .env) via the admin console and configure free at will. For further information please use the offical documentation:
https://www.keycloak.org/documentation
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.