How baezzys/google-oauth2-login-spring-react-demo works
Main Components
• Frontend Client (React):
• Located under the “react-oauth” directory.
• Handles obtaining the Google ID token and displaying user information once authenticated.
• Backend Server (Spring Boot):
• Found under the “spring-oauth” directory.
• Contains the main Java application (OauthApplication.java), configuration classes (JWTRequestFilter, SecurityConfig, WebConfig), controllers (AccountController, LoginController), DTOs, repositories (AccountRepository), and service handling (AccountService).
• Manages validation of the Google ID token, account creation, JWT generation, cookie management, and securing API endpoints.
• External Google OAuth Service:
• Represents Google’s OAuth service which issues the ID token needed for authentication.
• Database (Embedded/H2):
• Although not explicitly shown as a separate directory, usage of Spring Data JPA and H2 (via configuration in application.yml) indicates that an H2 database is used for account persistence.
Relationships and Interactions
• The frontend sends the Google-provided ID token to the backend (through postLoginToken API) upon user login.
• The backend validates the ID token, creates (or verifies) the user account, and generates a custom JWT.
• The JWT is sent back as a cookie to the frontend.
• For subsequent processes (like user information retrieval), the frontend includes this JWT cookie along with API calls (via getUserInfo API) to the backend.
• Internal interactions in the backend include filtering requests (JWTRequestFilter) and controlling access with the SecurityConfig.
Architectural Patterns & Technologies
• Use of OAuth 2.0 for authentication.
• Use of JWT for stateless authentication and session management.
• MVC pattern in the Spring Boot part (controllers, services, repositories, DTOs).
• Separation of concerns between the frontend (React components, pages, API integrations) and backend (REST controllers, business logic, security filters, data access).
• Configuration management via application.yml (Spring Boot) and .env (React).
Additional Detail Inclusions
• Clearly label configuration elements like environment variables (.env in React and application.yml in Spring Boot) as they are integral to the OAuth2 and JWT configurations.
• Security flow where the JWT filter (JWTRequestFilter) intercepts API requests to validate tokens.
• Dependencies, such as Spring Boot’s dependency on gradle for builds, and React’s npm management.