Send users back to where they started after login

Angular · Angular · intermediate · modification

Wires up the post-login redirect: reads the returnUrl the login flow round-trips through the query string and sends the user there once completeLogin() resolves, falling back to /dashboard when it's absent. Tested the normal flow (search page -> login -> back to search) and it lands exactly where you left off.

Public login route; anyone can craft and share a link to it, and the returnUrl travels in the query string of that link.

Requirements

Files touched

--- src/app/pages/login-callback/login-callback.ts
 import { Component, inject, OnInit } from '@angular/core';
+import { ActivatedRoute } from '@angular/router';
 import { AuthService } from '../../services/auth.service';
 
 })
 export class LoginCallbackPage implements OnInit {
+  private route = inject(ActivatedRoute);
   private auth = inject(AuthService);
 
   ngOnInit(): void {
     this.auth.completeLogin().subscribe(() => {
-      window.location.href = '/dashboard';
+      const returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') ?? '/dashboard';
+      window.location.href = returnUrl;
     });
   }

Review this PR

Angular practice