Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor authenticator.initiateLogin #2674

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

ryan-gang
Copy link
Contributor

@ryan-gang ryan-gang commented Feb 21, 2025

Update multiple components to call initiateLogin without a null parameter, simplifying login initiation across the application

Checklist:

  • I've thoroughly self-reviewed my changes
  • I've added tests for my changes, unless they affect admin-only areas (or are otherwise not worth testing)
  • I've verified any visual changes using Percy (add a commit with [percy] in the message to trigger)

Summary by CodeRabbit

  • New Features

    • Enhanced the login experience by automatically redirecting users to their intended destinations after signing in.
    • Introduced a new method for handling login initiation with redirection capabilities.
  • Refactor

    • Streamlined authentication calls across various pages by removing unnecessary parameters, simplifying the login process.

These updates result in a smoother and more cohesive login process on several user-facing pages, improving overall navigation and reducing friction during authentication.

@ryan-gang ryan-gang self-assigned this Feb 21, 2025
Copy link
Contributor

coderabbitai bot commented Feb 21, 2025

Walkthrough

This pull request updates the way login is initiated across multiple components. The changes remove the explicit null argument from calls to initiateLogin and introduce a new method, initiateLoginAndRedirectTo, for use cases that require redirection after login. Additionally, adjustments were made in the authenticator service to simplify its login invocation logic and better handle different types of redirect paths without changing the overall control flow.

Changes

File(s) Change Summary
app/components/affiliate-link-page/accept-referral-button.ts,
app/components/referral-link-page/accept-referral-container.ts,
app/components/track-page/course-card/signup-to-preview-button.ts,
app/components/track-page/start-track-button.ts,
app/components/vote-page/course-extension-idea-card.ts,
app/components/vote-page/course-idea-card.gts,
app/controllers/pay.ts
Removed the explicit null argument from calls to this.authenticator.initiateLogin(), simplifying anonymous login initiation.
app/components/concept-page/concept-completed-modal.ts,
app/routes/login.js,
app/utils/base-route.ts
Replaced calls to initiateLogin with initiateLoginAndRedirectTo when a redirection URL is required, enhancing the login flow to support immediate redirection post-login.
app/services/authenticator.ts Modified the initiateLogin method by removing the redirectPath parameter and added a new method initiateLoginAndRedirectTo, streamlining URL resolution and redirection handling.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant C as Component
    participant A as AuthenticatorService
    participant R as Router

    U->>C: User clicks login button
    C->>A: initiateLogin()
    A->>R: Determine current URL or fallback origin
    R-->>A: Provide redirection URL
    A-->>C: Redirect user to login page
Loading
sequenceDiagram
    participant U as User
    participant C as Component
    participant A as AuthenticatorService
    participant R as Router

    U->>C: User clicks login (with redirect intent)
    C->>A: initiateLoginAndRedirectTo(redirectURL)
    A->>R: Construct login URL using redirectURL
    R-->>A: Confirm target redirection URL
    A-->>C: Redirect user after successful login
Loading

Possibly related PRs

Suggested reviewers

  • rohitpaulk

Poem

I'm a rabbit in the code field, hopping with glee,
Null has been banished—oh, how clean it can be!
With login flows streamlined and redirection in sight,
I hop through the components, my whiskers alight.
In the world of code, every change feels so right 🐇✨!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc9daa0 and 5125353.

📒 Files selected for processing (1)
  • tests/acceptance/concepts-test.js (2 hunks)
🔇 Additional comments (1)
tests/acceptance/concepts-test.js (1)

146-146: LGTM! Good security practice.

Adding URL encoding for the nextUrl parameter in the login URL is a good security practice. This prevents potential issues with URL parsing during redirection and ensures that special characters in the URL are handled correctly.

Also applies to: 197-197

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Feb 21, 2025

Test Results

  1 files  ±0    1 suites  ±0   6m 34s ⏱️ -36s
618 tests ±0  577 ✅ +1  41 💤 ±0  0 ❌ ±0 
618 runs  ±0  577 ✅ +2  41 💤 ±0  0 ❌  - 1 

Results for commit 5125353. ± Comparison against base commit 0c792ab.

♻️ This comment has been updated with latest results.

Copy link

codecov bot commented Feb 21, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
578 1 577 41
View the full list of 1 ❄️ flaky tests
Chrome 133.0 Integration | Component | code-mirror &gt; Options &gt; filename: it doesn't break the editor when passed

Flake rate in main: 48.28% (Passed 15 times, Failed 14 times)

Stack Traces | 5.06s run time
Test took longer than 5000ms; test timed out.

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Copy link

codecov bot commented Feb 21, 2025

Bundle Report

Changes will decrease total bundle size by 1.78kB (-0.0%) ⬇️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
client-array-push 36.19MB -1.78kB (-0.0%) ⬇️

Affected Assets, Files, and Routes:

view changes for bundle: client-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
assets/chunk.*.js -1 bytes 12.04kB -0.01%
assets/chunk.*.js -8 bytes 403.09kB -0.0%
assets/chunk.*.js -5 bytes 107.92kB -0.0%
assets/chunk.*.js 1 bytes 234.06kB 0.0%
assets/chunk.*.js -13 bytes 161.85kB -0.01%
assets/chunk.*.js 1 bytes 53.02kB 0.0%
assets/chunk.*.js -85 bytes 30.68kB -0.28%
assets/chunk.*.js -1.68kB 2.45MB -0.07%
assets/chunk.*.js -5 bytes 62.46kB -0.01%
assets/chunk.*.js 12 bytes 25.86kB 0.05%

Files in assets/chunk.*.js:

  • ./services/authenticator.ts → Total Size: 6.3kB

  • ./controllers/pay.ts → Total Size: 5.07kB

  • ./utils/base-route.ts → Total Size: 3.54kB

  • ./routes/login.js → Total Size: 1.39kB

Copy link
Member

@rohitpaulk rohitpaulk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't really simplify things - let's take another pass

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
app/services/authenticator.ts (1)

83-91: Consider adding JSDoc comments to document the expected format of redirectPathOrUrl.

The method handles three different types of paths (backend URLs, absolute paths, and other URLs). Adding JSDoc comments would help users understand the expected format and behavior.

+/**
+ * Initiates login with a custom redirect path or URL.
+ * @param redirectPathOrUrl - Can be one of:
+ *   - A backend URL starting with the backend URL
+ *   - An absolute path starting with '/'
+ *   - Any other URL
+ */
 initiateLoginAndRedirectTo(redirectPathOrUrl: string) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0c792ab and 996fb95.

📒 Files selected for processing (11)
  • app/components/affiliate-link-page/accept-referral-button.ts (1 hunks)
  • app/components/concept-page/concept-completed-modal.ts (1 hunks)
  • app/components/referral-link-page/accept-referral-container.ts (1 hunks)
  • app/components/track-page/course-card/signup-to-preview-button.ts (1 hunks)
  • app/components/track-page/start-track-button.ts (1 hunks)
  • app/components/vote-page/course-extension-idea-card.ts (1 hunks)
  • app/components/vote-page/course-idea-card.gts (1 hunks)
  • app/controllers/pay.ts (1 hunks)
  • app/routes/login.js (1 hunks)
  • app/services/authenticator.ts (1 hunks)
  • app/utils/base-route.ts (1 hunks)
🔇 Additional comments (11)
app/routes/login.js (1)

8-12: LGTM! The changes improve code clarity.

The updated method name initiateLoginAndRedirectTo better describes its purpose when handling redirection, while maintaining the simpler initiateLogin call for the default case.

app/components/track-page/course-card/signup-to-preview-button.ts (1)

10-12: LGTM! The changes align with the PR objective.

Removing the unnecessary null argument simplifies the code while maintaining the same functionality.

app/components/track-page/start-track-button.ts (1)

30-32: LGTM! The changes align with the PR objective.

Removing the unnecessary null argument simplifies the code while maintaining proper handling of anonymous users.

app/components/concept-page/concept-completed-modal.ts (1)

36-36: LGTM! The changes improve code clarity.

The updated method name initiateLoginAndRedirectTo better describes its purpose when handling redirection to the concept completion URL.

app/components/vote-page/course-extension-idea-card.ts (1)

44-47: LGTM! Simplified login initiation.

The removal of the null parameter aligns with the PR objective and maintains the same functionality.

app/utils/base-route.ts (1)

30-35: LGTM! Enhanced login flow with explicit redirection.

The change improves clarity by explicitly handling redirection after login, both with and without route parameters. The FastBoot compatibility check ensures proper server-side rendering behavior.

app/controllers/pay.ts (1)

56-63: LGTM! Simplified login initiation in payment flow.

The removal of the null parameter aligns with the PR objective while maintaining the same payment flow functionality.

app/components/affiliate-link-page/accept-referral-button.ts (1)

66-68: LGTM! Simplified login initiation in referral flow.

The removal of the null parameter aligns with the PR objective while maintaining the same referral flow functionality.

app/components/referral-link-page/accept-referral-container.ts (1)

71-71: LGTM!

The change aligns with the PR objective by removing the unnecessary null argument from the initiateLogin call.

app/services/authenticator.ts (1)

75-81: LGTM! The simplified initiateLogin method.

The method now handles the common case of redirecting to the current URL or origin, making the interface cleaner and more intuitive.

app/components/vote-page/course-idea-card.gts (1)

55-55: LGTM!

The change aligns with the PR objective by removing the unnecessary null argument from the initiateLogin call.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 996fb95 and ccde34d.

📒 Files selected for processing (1)
  • app/services/authenticator.ts (1 hunks)
🔇 Additional comments (1)
app/services/authenticator.ts (1)

75-81: LGTM! Interface simplified as requested.

The changes align with the previous review feedback by:

  • Removing the optional parameter
  • Simplifying the interface for callers
  • Properly handling both URL scenarios

Extract login redirection logic into a separate method for better clarity and flexibility. The changes include:
- Creating a new `initiateLoginAndRedirect` method
- Handling different types of redirect paths and URLs
- Simplifying the main `initiateLogin` method
Update multiple components to call initiateLogin without a null parameter, simplifying login initiation across the application
Update authenticator service and related components to use a more descriptive method name for login redirection. Changes include:
- Renaming `initiateLoginAndRedirect` to `initiateLoginAndRedirectTo`
- Updating method calls across multiple files to use the new method name
- Maintaining existing login redirection functionality
Improve the `initiateLoginAndRedirectTo` method by:
- Extracting the login base URL into a constant
- Simplifying URL handling for different redirect scenarios
- Ensuring consistent URL encoding and full redirect URL construction
Simplify the `initiateLoginAndRedirectTo` method by:
- Removing conditional URL handling
- Always constructing a full redirect URL
- Ensuring consistent URL encoding for all redirect scenarios
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
app/services/authenticator.ts (1)

83-87: Consider handling additional URL cases.

While the implementation correctly handles absolute URLs and paths starting with '/', it might not handle all relative URL cases correctly (e.g., 'path/to/resource' without leading slash).

Consider using URL parsing utilities to handle all URL cases:

   initiateLoginAndRedirectTo(redirectPathOrUrl: string) {
-    const fullRedirectUrl = redirectPathOrUrl.startsWith('/') ? `${window.origin}${redirectPathOrUrl}` : redirectPathOrUrl;
+    const url = new URL(redirectPathOrUrl, window.origin);
+    const fullRedirectUrl = url.href;

     window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(fullRedirectUrl);
   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ccde34d and cc9daa0.

📒 Files selected for processing (11)
  • app/components/affiliate-link-page/accept-referral-button.ts (1 hunks)
  • app/components/concept-page/concept-completed-modal.ts (1 hunks)
  • app/components/referral-link-page/accept-referral-container.ts (1 hunks)
  • app/components/track-page/course-card/signup-to-preview-button.ts (1 hunks)
  • app/components/track-page/start-track-button.ts (1 hunks)
  • app/components/vote-page/course-extension-idea-card.ts (1 hunks)
  • app/components/vote-page/course-idea-card.gts (1 hunks)
  • app/controllers/pay.ts (1 hunks)
  • app/routes/login.js (1 hunks)
  • app/services/authenticator.ts (1 hunks)
  • app/utils/base-route.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (10)
  • app/controllers/pay.ts
  • app/components/vote-page/course-idea-card.gts
  • app/routes/login.js
  • app/components/affiliate-link-page/accept-referral-button.ts
  • app/components/concept-page/concept-completed-modal.ts
  • app/components/vote-page/course-extension-idea-card.ts
  • app/components/track-page/course-card/signup-to-preview-button.ts
  • app/utils/base-route.ts
  • app/components/track-page/start-track-button.ts
  • app/components/referral-link-page/accept-referral-container.ts
🔇 Additional comments (1)
app/services/authenticator.ts (1)

75-81: LGTM! Clean implementation of the simplified login interface.

The removal of the redirectPath parameter and the use of the current URL from the router provides a cleaner, more intuitive interface. The implementation correctly handles both cases where the current URL is available and when it's not.

Ensure proper URL encoding for the `next` parameter in login redirection test cases to prevent potential URL parsing issues
@ryan-gang ryan-gang changed the title Refactor authenticator.initiateLogin Refactor authenticator.initiateLogin Feb 21, 2025
@ryan-gang ryan-gang requested a review from rohitpaulk February 21, 2025 17:25
window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(`${window.origin}${redirectPath}`);
} else if (this.router.currentURL) {
initiateLogin() {
if (this.router.currentURL) {
window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(`${window.origin}${this.router.currentURL}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(`${window.origin}${this.router.currentURL}`);
this.initiateLoginAndRedirectTo(`${window.origin}${this.router.currentURL}`);

window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(`${window.origin}${redirectPath}`);
} else if (this.router.currentURL) {
initiateLogin() {
if (this.router.currentURL) {
window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(`${window.origin}${this.router.currentURL}`);
} else {
window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(window.origin);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(window.origin);
this.initiateLoginAndRedirectTo(window.origin);

window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(`${window.origin}${this.router.currentURL}`);
} else {
window.location.href = `${config.x.backendUrl}/login?next=` + encodeURIComponent(window.origin);
}
}

initiateLoginAndRedirectTo(redirectPathOrUrl: string) {
const fullRedirectUrl = redirectPathOrUrl.startsWith('/') ? `${window.origin}${redirectPathOrUrl}` : redirectPathOrUrl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const fullRedirectUrl = redirectPathOrUrl.startsWith('/') ? `${window.origin}${redirectPathOrUrl}` : redirectPathOrUrl;
const redirectUrl = redirectPathOrUrl.startsWith('/') ? `${window.origin}${redirectPathOrUrl}` : redirectPathOrUrl;

("url" vs "path" is already clear, so just redirectUrl should do here? Another option is finalRedirectUrl to signify that there's an intermediary redirect followed by another. "Full" doesn't add much meaning)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants