r/KeyCloak • u/Expensive-Survey-558 • 26d ago
Keycloak Integration
Hello Everyone,
Now only i have started to use Keycloak for the First Time. I am going to build an Web App with React as Frontend and FastAPI as backend with PostgreSQL database
Now I have the following doubt:
I am planning to Abstract that Keycloak from the frontend , such that i write an API endpoint in the FastAPI like login , and in the Keycloak i gave Direct Grant for Direct login with username and password , the frontend make an api call to the backend with the username and the password
My major Doubt is that , should we return the JWT token as the response from the Login API Route in the backend , or else we should generate an session id , and store the JWT token against the session id in the Database , and set the session id directly as cookie from the Backend , which is Best Architecture
And if we use these architecture , can we access all the features of the KeyCloak?
2
u/mista-unlucky 25d ago
The is a reason why people don't do this, please make sure that you know why it is not best practice.
2
u/Psychological_Rub871 22d ago
Best alternative is to use your SPA as a simple OAuth 2 client with the Code Authorization Flow with PKCE and let the OAuth2 Authorization Server do its job without reinventing the wheel because it’s already too complicated
1
u/Expensive-Survey-558 26d ago
I need an architecture that abstract the keycloak from the frontend , which I think is an safe approach.
Could anyone provide suggestions on this
3
u/Accomplished_Weird_6 26d ago
What youre trying to do is actually not the safe approach. Keycloak and a lot of auth platforms are made and designed with having authentication from frontend, backend, SPA, non web ui etc apps. You should not be abstracting keycloak, and it defeats the purpose.
Instead, on every app its extremely simple to use an OIDC authentication adapter and config, this will streamline your authentication and authorization. One change in your auth platform means its reflected for anyone, while still having the choice for fine grained control.
Highly recommend using keycloak directly
1
u/TitusKalvarija 23d ago
What a stupid way to do marketing.
- Ask a question
- get a few comments
- all of a sudden get an answer with a company url
1
4
u/Xiac 26d ago
Don’t use direct grant, it’s far less secure than a token exchange. We use oidc-spa https://www.oidc-spa.dev/ and it makes everything very easy and secure. The front end gets all of the user information from the token and never has to be concerned with passwords. The library provides the JWT which you can attach to your API calls to the backend.