Project-Stage-Academy / UA1244_beta

1 stars 0 forks source link

OAuth Authorization Integration #93

Open mehalyna opened 4 days ago

mehalyna commented 4 days ago

Title:

As a user, I want to log in to the WebAPI application using my social accounts (e.g., Google, GitHub), so that I can access the platform more easily without the need to create a separate account.


Description:

This feature introduces OAuth2-based login for users via popular social platforms like Google or GitHub. The user will have the option to either register and log in with an email and password (using JWT) or authenticate via OAuth. This will improve the user experience by providing an alternative, streamlined login flow.


User Flow:

  1. Select Social Login Provider:

    • On the login page, the user can select "Login with Google", "Login with GitHub", or other available providers.
  2. OAuth Provider Authorization:

    • The user will be redirected to the selected OAuth provider’s login page (e.g., Google).
    • After successful login, the OAuth provider will redirect back to the application with an authorization code or access token.
  3. Backend OAuth Handling:

    • The backend receives the authorization code/token, exchanges it for user data, and creates or updates the user profile in the database.
    • If the user logs in for the first time, a new user account is created and associated with the provider. For returning users, the profile is updated.
  4. JWT Token Assignment:

    • After OAuth authentication, the backend issues a JWT token to the user for further API interactions.
  5. Celery Tasks for Welcome Emails:

    • After a successful login or registration, a welcome email is sent asynchronously using Celery.

Acceptance Criteria:


Technical Implementation Notes:

  1. Dependencies:

    • Use django-allauth or authlib for OAuth2 integration.
    • Djoser for JWT token management.
    • Celery for background task handling (e.g., sending welcome emails).
  2. Settings Configuration:

    • Update INSTALLED_APPS to include OAuth libraries (django-allauth or authlib).
    • Add OAuth provider credentials (e.g., Google Client ID/Secret) in environment variables.
  3. Djoser and OAuth Integration:

    • Configure custom views to handle OAuth-based login and issue JWT tokens after successful OAuth authentication.
  4. Backend OAuth Flow:

    • Exchange the authorization code from the provider with an access token.
    • Retrieve the user’s profile from the OAuth provider.
    • Create or update the user in the database.
  5. Frontend Changes:

    • Add "Login with Google" and "Login with GitHub" buttons to the login page.
    • Handle OAuth redirection and display error messages if OAuth fails.

Example API Flow:

  1. Frontend:
    User clicks "Login with Google" -> Redirect to https://accounts.google.com/o/oauth2/auth.

  2. Backend:
    OAuth provider redirects back with authorization code ->
    Exchange code for access token -> Retrieve user profile -> Create/Update user -> Issue JWT token.


Tasks:

  1. Backend Setup:

    • Install and configure django-allauth or authlib.
    • Configure OAuth providers in Django settings.
  2. JWT Integration:

    • Extend Djoser views to support OAuth-based authentication.
  3. Frontend Integration:

    • Add social login buttons and handle OAuth redirection.
  4. Testing:

    • Write unit and integration tests for OAuth login and JWT issuance.
  5. Documentation:

    • Update project documentation with instructions for OAuth setup.