Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Auth guards #282

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {HostAnalyticsComponent} from './components/analytics/host-analytics/host
import {ResetPasswordComponent} from './components/auth/reset-password/reset-password.component';
import {ResetPasswordConfirmComponent} from './components/auth/reset-password-confirm/reset-password-confirm.component';

import { AuthGuard } from './guards/auth-guard.service';
Copy link
Member

Choose a reason for hiding this comment

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

Can we have a better name: Authentication/authorization?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is normal convention

Copy link
Contributor Author

Choose a reason for hiding this comment

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


const routes: Routes = [
{
path: '',
Expand Down Expand Up @@ -104,10 +106,12 @@ const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [AuthGuard]
},
{
path: 'analytics',
component: AnalyticsComponent,
canActivate: [AuthGuard],
children: [
{path: '', redirectTo: 'host-analytics', pathMatch: 'full'},
{path: 'host-analytics', component: HostAnalyticsComponent}
Expand Down
6 changes: 5 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ import {PasswordMismatchValidatorDirective} from './Directives/password.validato
import { ResetPasswordComponent } from './components/auth/reset-password/reset-password.component';
import { EmailValidatorDirective } from './Directives/email.validator';
import { ResetPasswordConfirmComponent } from './components/auth/reset-password-confirm/reset-password-confirm.component';

import { AuthGuard } from './guards/auth-guard.service';

@NgModule({
declarations: [
AppComponent,
Expand Down Expand Up @@ -174,7 +177,8 @@ import { ResetPasswordConfirmComponent } from './components/auth/reset-password-
ApiService,
GlobalService,
ChallengeService,
EndpointsService
EndpointsService,
AuthGuard
],
bootstrap: [AppComponent]
})
Expand Down
1 change: 0 additions & 1 deletion src/app/components/analytics/analytics.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class AnalyticsComponent implements OnInit, OnDestroy {
}

ngOnInit() {
this.authService.isLoggedIn();
this.authServiceSubscription = this.authService.change.subscribe((authState) => {
if (!authState.isLoggedIn) {
this.router.navigate(['/auth/login']);
Expand Down
3 changes: 0 additions & 3 deletions src/app/components/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ export class DashboardComponent implements OnInit, OnDestroy {
* Component on initialized.
*/
ngOnInit() {
if (!this.authService.isLoggedIn()) {
this.router.navigate([this.routePath]);
}
this.authServiceSubscription = this.authService.change.subscribe((authState) => {
if (!authState.isLoggedIn) {
this.router.navigate([this.routePath]);
Expand Down
21 changes: 21 additions & 0 deletions src/app/guards/auth-guard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { CanActivate, Router, UrlTree } from '@angular/router';
import { AuthService } from '../services/auth.service';

@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}

canActivate(): Observable<boolean> | Promise<boolean> | boolean | UrlTree {
const routePath = '/auth/login';
const isAuthenticated = this.authService.isLoggedIn();

if (isAuthenticated) {
return true;
}

const tree: UrlTree = this.router.parseUrl(routePath);
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure how this will help. I think UrlTree was introduced as return type from Angular 7 version.

Isn't it we can do this as well simply:

return isAuthenticated

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't because it will behave the same way as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check out the GIF in the PR description. You will see the difference

return tree;
}
}