mohamed-abdo / spring-boot-3-keycloak-integration

sample seeding project spring boot 3 with keycloak integration
Apache License 2.0
0 stars 0 forks source link

Sweep: add a new screen with header contains logout button using react js to consume the api /ouath/auth and show login in case no user exists in the security context #1

Open mohamed-abdo opened 1 year ago

mohamed-abdo commented 1 year ago
Checklist - [X] `src/main/frontend/package.json` > * Initialize a new React.js application using the create-react-app command. > • Add the necessary dependencies for the application, such as react, react-dom, and axios for making HTTP requests. - [X] `src/main/frontend/src/components/Header.js` > * Create a new Header component. > • Add a logout button to the Header component. > • Implement the logic for the logout button to clear the user's authentication status and redirect them to the login screen. - [X] `src/main/frontend/src/components/Login.js` > * Create a new Login component. > • Implement the logic to consume the /oauth/auth API and authenticate the user. > • Redirect the user to the main screen of the application upon successful authentication. - [X] `src/main/java/com/keycloak/demo/config/SecurityConfiguration.java` > * Add a resource handler to serve the static files of the React.js application. > • Set the location of the static files to the build folder of the React.js application. - [X] `src/main/java/com/keycloak/demo/OAuth2ClientController.java` > * Modify the /oauth/auth endpoint to return the user's authentication status instead of the entire security context. > • This will allow the frontend to easily determine whether to display the login screen or the main screen of the application.
sweep-ai[bot] commented 1 year ago

Here's the PR! https://github.com/mohamed-abdo/spring-boot-3-keycloak-integration/pull/6.

⚡ Sweep Free Trial: I used GPT-4 to create this ticket. You have 5 GPT-4 tickets left for the month and 2 for the day. For more GPT-4 tickets, visit our payment portal.To get Sweep to recreate this ticket, leave a comment prefixed with "sweep:" or edit the issue.


Step 1: 🔍 Code Search

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I looked at (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/mohamed-abdo/spring-boot-3-keycloak-integration/blob/ff1e9ed8af26d174a7ecef66a4b9b5ca90d8cbf4/src/main/java/com/keycloak/demo/config/SecurityConfiguration.java#L1-L66 https://github.com/mohamed-abdo/spring-boot-3-keycloak-integration/blob/ff1e9ed8af26d174a7ecef66a4b9b5ca90d8cbf4/src/main/java/com/keycloak/demo/custom/CustomClientRegistrationRepository.java#L1-L49 https://github.com/mohamed-abdo/spring-boot-3-keycloak-integration/blob/ff1e9ed8af26d174a7ecef66a4b9b5ca90d8cbf4/src/main/java/com/keycloak/demo/filter/CustomAuthenticationFailureHandler.java#L1-L35 https://github.com/mohamed-abdo/spring-boot-3-keycloak-integration/blob/ff1e9ed8af26d174a7ecef66a4b9b5ca90d8cbf4/src/main/java/com/keycloak/demo/custom/CustomReactiveAuthenticationManager.java#L1-L47 https://github.com/mohamed-abdo/spring-boot-3-keycloak-integration/blob/ff1e9ed8af26d174a7ecef66a4b9b5ca90d8cbf4/src/main/java/com/keycloak/demo/OAuth2ClientController.java#L1-L40

I also found some related docs:

Summary of related docs from https://react.dev/:

The relevant documentation snippets for solving the problem are:

  1. From https://react.dev/reference/react-dom/client:

    • The createRoot function allows you to create a root to display React components inside a browser DOM node.
    • The hydrateRoot function allows you to display React components inside a browser DOM node whose HTML content was previously generated by react-dom/server.
  2. From https://react.dev/reference/react/useEffect:

    • The useEffect hook is used to connect a component to an external system, such as a network or browser API.
    • Effects only run on the client and don't run during server rendering.
  3. From https://react.dev/reference/react/useSyncExternalStore:

    • The useSyncExternalStore hook is used to subscribe to an external store or value that changes over time.
    • It is recommended to use built-in React state with useState and useReducer instead of useSyncExternalStore, unless you need to integrate with existing non-React code.

Based on these snippets, you can use the createRoot function to create a root element in the DOM, and then use the useEffect hook to connect to the /oauth/auth API and handle the logic for displaying the login screen or the logout button based on the user's existence in the security context.

Sources:


Step 2: 🧐 Snippet Analysis

From looking through the relevant snippets, I decided to make the following modifications:

File Path Proposed Changes
src/main/frontend/package.json Create src/main/frontend/package.json with contents:
* Initialize a new React.js application using the create-react-app command.
• Add the necessary dependencies for the application, such as react, react-dom, and axios for making HTTP requests.
src/main/frontend/src/components/Header.js Create src/main/frontend/src/components/Header.js with contents:
* Create a new Header component.
• Add a logout button to the Header component.
• Implement the logic for the logout button to clear the user's authentication status and redirect them to the login screen.
src/main/frontend/src/components/Login.js Create src/main/frontend/src/components/Login.js with contents:
* Create a new Login component.
• Implement the logic to consume the /oauth/auth API and authenticate the user.
• Redirect the user to the main screen of the application upon successful authentication.
src/main/java/com/keycloak/demo/config/SecurityConfiguration.java Modify src/main/java/com/keycloak/demo/config/SecurityConfiguration.java with contents:
* Add a resource handler to serve the static files of the React.js application.
• Set the location of the static files to the build folder of the React.js application.
src/main/java/com/keycloak/demo/OAuth2ClientController.java Modify src/main/java/com/keycloak/demo/OAuth2ClientController.java with contents:
* Modify the /oauth/auth endpoint to return the user's authentication status instead of the entire security context.
• This will allow the frontend to easily determine whether to display the login screen or the main screen of the application.

Step 3: 📝 Planning

I have created a plan for writing the pull request. I am now working my plan and coding the required changes to address this issue. Here is the planned pull request:

Add new screen with header and logout button using React.js sweep/add-react-frontend

Description

This PR adds a new screen with a header and logout button using React.js to consume the /oauth/auth API. The screen will display a login form if no user exists in the security context. The Spring Boot backend has been modified to serve the static files of the React.js application.

Summary of Changes

  • Added a new React.js application using create-react-app.
  • Created a Header component with a logout button.
  • Created a Login component to handle user authentication.
  • Modified the Spring Boot application to serve the static files of the React.js application.
  • Modified the /oauth/auth endpoint to return the user's authentication status.

Please review and merge this PR. Thank you!


Step 4: ⌨️ Coding

File Instructions Progress
src/main/frontend/package.json Create src/main/frontend/package.json with contents:
* Initialize a new React.js application using the create-react-app command.
• Add the necessary dependencies for the application, such as react, react-dom, and axios for making HTTP requests.
✅ Commit bb9c433
src/main/frontend/src/components/Header.js Create src/main/frontend/src/components/Header.js with contents:
* Create a new Header component.
• Add a logout button to the Header component.
• Implement the logic for the logout button to clear the user's authentication status and redirect them to the login screen.
✅ Commit 5c19bee
src/main/frontend/src/components/Login.js Create src/main/frontend/src/components/Login.js with contents:
* Create a new Login component.
• Implement the logic to consume the /oauth/auth API and authenticate the user.
• Redirect the user to the main screen of the application upon successful authentication.
✅ Commit 5c19bee
src/main/java/com/keycloak/demo/config/SecurityConfiguration.java Modify src/main/java/com/keycloak/demo/config/SecurityConfiguration.java with contents:
* Add a resource handler to serve the static files of the React.js application.
• Set the location of the static files to the build folder of the React.js application.
✅ Commit 3020741
src/main/java/com/keycloak/demo/OAuth2ClientController.java Modify src/main/java/com/keycloak/demo/OAuth2ClientController.java with contents:
* Modify the /oauth/auth endpoint to return the user's authentication status instead of the entire security context.
• This will allow the frontend to easily determine whether to display the login screen or the main screen of the application.
✅ Commit 15703c0 I have finished coding the issue. I am now reviewing it for completeness.

Step 5: 🔁 Code Review

Here are my self-reviews of my changes at sweep/add-react-frontend.

Here is the 1st review

Good job on the changes made so far. However, there are a few areas that need to be addressed:

  • In the file src/main/frontend/src/components/Header.js, the logout logic is currently a placeholder and should be replaced with the actual logic for handling user logout. Please implement this logic to ensure the logout functionality works as expected.

  • In the file src/main/frontend/package.json, while the basic structure and dependencies are correctly set up, the actual implementation of the frontend (the React application) is not included in this pull request. Please ensure that the necessary React components and logic are implemented and included in the pull request.

Keep up the good work!

I finished incorporating these changes.


To recreate the pull request, or edit the issue title or description. Join Our Discord