Skip to content

Commit

Permalink
Add login redirect (#1256)
Browse files Browse the repository at this point in the history
Previously, upon login, users are brought to the
default landing page of the phase.

We redirect to the intended landing page,
with appropriate next route checking.
  • Loading branch information
nknguyenhc authored Apr 15, 2024
1 parent 05fe528 commit 9690676
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/auth/confirm-login/confirm-login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class ConfirmLoginComponent implements OnInit {
*/
handleAuthSuccess() {
this.authService.setTitleWithPhaseDetail();
this.router.navigateByUrl(this.phaseService.currentPhase);
this.authService.changeAuthState(AuthState.Authenticated);
this.authService.navigateToLandingPage();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export class AuthGuard implements CanActivate, CanLoad {

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.auth.isAuthenticated()) {
this.auth.clearNext();
return true;
} else {
this.auth.storeNext(state);
this.router.navigate(['']);
return false;
}
Expand Down
37 changes: 36 additions & 1 deletion src/app/core/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { NgZone } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { Router, RouterStateSnapshot } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { AppConfig } from '../../../environments/environment';
import { generateSessionId } from '../../shared/lib/session';
Expand Down Expand Up @@ -30,6 +30,8 @@ export enum AuthState {
* updating the application state with regards to authentication.
*/
export class AuthService {
private static readonly SESSION_NEXT_KEY = 'next';

authStateSource = new BehaviorSubject(AuthState.NotAuthenticated);
currentAuthState = this.authStateSource.asObservable();
accessToken = new BehaviorSubject(undefined);
Expand All @@ -50,6 +52,27 @@ export class AuthService {
private logger: LoggingService
) {}

/**
* Stores the data about the next route in the session storage.
*/
storeNext(next: RouterStateSnapshot) {
sessionStorage.setItem(AuthService.SESSION_NEXT_KEY, next.url);
}

/**
* Returns the next route
*/
private getNext(): string {
return sessionStorage.getItem(AuthService.SESSION_NEXT_KEY);
}

/**
* Clears the next route from the session storage.
*/
clearNext() {
sessionStorage.removeItem(AuthService.SESSION_NEXT_KEY);
}

/**
* Will store the OAuth token.
*/
Expand Down Expand Up @@ -144,4 +167,16 @@ export class AuthService {
}
window.location.href = url;
}

/**
* Navigates to next if there is, or default landing page.
*/
navigateToLandingPage() {
const nextRoute = this.getNext();
if (!nextRoute || !this.phaseService.isValidRoute(nextRoute)) {
this.router.navigateByUrl(this.phaseService.currentPhase);
} else {
this.router.navigateByUrl(nextRoute);
}
}
}
7 changes: 7 additions & 0 deletions src/app/core/services/phase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ export class PhaseService {
return this.orgName.concat('/').concat(this.repoName);
}

/**
* Checks whether the given route is allowed in this phase.
*/
isValidRoute(route: string): boolean {
return route.startsWith('/' + this.currentPhase);
}

reset() {
this.currentPhase = null;
}
Expand Down
10 changes: 8 additions & 2 deletions src/app/shared/view-issue/view-issue.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { Issue } from '../../core/models/issue.model';
import { UserRole } from '../../core/models/user.model';
Expand Down Expand Up @@ -53,7 +54,8 @@ export class ViewIssueComponent implements OnInit, OnDestroy, OnChanges {
public permissions: PermissionService,
public userService: UserService,
public issueService: IssueService,
private phaseService: PhaseService
private phaseService: PhaseService,
private router: Router
) {}

ngOnInit() {
Expand Down Expand Up @@ -126,7 +128,11 @@ export class ViewIssueComponent implements OnInit, OnDestroy, OnChanges {
this.issue = issue;
this.pollIssue(id);
},
(err) => this.errorHandlingService.handleError(err)
(err) => {
this.router.navigateByUrl(this.phaseService.currentPhase).then(() => {
this.errorHandlingService.handleError(new Error('Invalid URL provided!'));
});
}
);
}

Expand Down

0 comments on commit 9690676

Please sign in to comment.