-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pimmerks/feature/auth
Auth guard
- Loading branch information
Showing
20 changed files
with
193 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ jobs: | |
- uses: actions/[email protected] | ||
|
||
# Caches npm packages | ||
- uses: actions/cache@v1 | ||
- uses: actions/cache@v2 | ||
with: | ||
path: ~/.npm | ||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,31 @@ | ||
<clr-main-container> | ||
<clr-header> | ||
<app-header></app-header> | ||
<div class="branding"> | ||
<a [routerLink]="['/home']" class="nav-link"> | ||
<clr-icon shape="vm-bug"></clr-icon> | ||
<span class="title">clarity-bootstrap</span> | ||
</a> | ||
</div> | ||
<div class="header-nav" [clr-nav-level]="1"> | ||
<a [routerLink]="['/home']" routerLinkActive="active" class="nav-link"><span class="nav-text">Home</span></a> | ||
</div> | ||
|
||
<div class="header-actions" *ngIf="(isAuthenticated$ | async) === false"> | ||
<a [routerLink]="['/auth/login']" routerLinkActive="active" class="nav-link" aria-label="login"> | ||
Login | ||
</a> | ||
<a [routerLink]="['/auth/register']" routerLinkActive="active" class="nav-link" aria-label="register"> | ||
Register | ||
</a> | ||
</div> | ||
|
||
</clr-header> | ||
|
||
<div class="content-container"> | ||
<div class="content-area"> | ||
<router-outlet></router-outlet> | ||
</div> | ||
<nav class="sidenav"> | ||
<nav class="sidenav" *ngIf="isAuthenticated$ | async"> | ||
</nav> | ||
</div> | ||
</clr-main-container> | ||
</clr-main-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import { Component } from '@angular/core'; | ||
import { AuthenticationService } from '@services/authentication/authentication.service'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html' | ||
}) | ||
export class AppComponent { | ||
constructor( | ||
public readonly authService: AuthenticationService | ||
) { } | ||
|
||
public isAuthenticated$ = this.authService.isAuthenticated$; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,45 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, CanActivateChild, CanLoad, Route, UrlSegment, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router'; | ||
import { CanActivate, CanActivateChild, CanLoad, Route, UrlSegment, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; | ||
import { Observable } from 'rxjs'; | ||
import { AuthenticationService } from '@services/authentication/authentication.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad { | ||
constructor( | ||
private readonly authService: AuthenticationService, | ||
private readonly router: Router | ||
) { } | ||
|
||
public canActivate( | ||
next: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { | ||
return true; | ||
if (!this.authService.isAuthenticated) { | ||
const redirect = this.router.parseUrl('/auth/login'); | ||
redirect.queryParams = { returnUrl: state.url }; | ||
return redirect; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public canActivateChild( | ||
next: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { | ||
return true; | ||
if (!this.authService.isAuthenticated) { | ||
const redirect = this.router.parseUrl('/auth/login'); | ||
redirect.queryParams = { returnUrl: state.url }; | ||
return redirect; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public canLoad( | ||
route: Route, | ||
segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean { | ||
return true; | ||
return this.authService.isAuthenticated; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
<div class="card"> | ||
<div class="card-block"> | ||
|
||
<h2>Login</h2> | ||
|
||
<app-error-alert [error]="error"></app-error-alert> | ||
|
||
<form clrForm [formGroup]="loginForm" (ngSubmit)="onSubmit()" clrLayout="horizontal"> | ||
<clr-input-container> | ||
<label>Email</label> | ||
<input clrInput placeholder="[email protected]" type="text" formControlName="email" autocomplete="username" /> | ||
<clr-control-error>Please provide an email address.</clr-control-error> | ||
</clr-input-container> | ||
<clr-password-container> | ||
<label>Password</label> | ||
<input clrPassword placeholder="Password" formControlName="password" autocomplete="current-password" /> | ||
<clr-control-error>Please provide a password.</clr-control-error> | ||
</clr-password-container> | ||
<br /> | ||
<button [clrLoading]="loginBtnState" [disabled]="loginForm.invalid" class="btn btn-primary">Login</button> | ||
</form> | ||
<app-center-card> | ||
|
||
<div class="card"> | ||
<div class="card-block"> | ||
|
||
<h2>Login</h2> | ||
|
||
<app-error-alert [error]="error"></app-error-alert> | ||
|
||
<form clrForm [formGroup]="loginForm" (ngSubmit)="onSubmit()" clrLayout="horizontal"> | ||
<clr-input-container> | ||
<label>Email</label> | ||
<input clrInput placeholder="[email protected]" type="text" formControlName="email" autocomplete="username" /> | ||
<clr-control-error>Please provide an email address.</clr-control-error> | ||
</clr-input-container> | ||
<clr-password-container> | ||
<label>Password</label> | ||
<input clrPassword placeholder="Password" formControlName="password" autocomplete="current-password" /> | ||
<clr-control-error>Please provide a password.</clr-control-error> | ||
</clr-password-container> | ||
<br /> | ||
<button [clrLoading]="loginBtnState" [disabled]="loginForm.invalid" class="btn btn-primary">Login</button> | ||
</form> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
|
||
</app-center-card> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 26 additions & 24 deletions
50
src/app/pages/auth/components/register/register.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
<div class="card"> | ||
<div class="card-block"> | ||
<app-center-card> | ||
<div class="card"> | ||
<div class="card-block"> | ||
|
||
<h2>Register</h2> | ||
<h2>Register</h2> | ||
|
||
<app-error-alert [error]="error"></app-error-alert> | ||
<app-error-alert [error]="error"></app-error-alert> | ||
|
||
<form clrForm [formGroup]="registerForm" (ngSubmit)="onSubmit()" clrLayout="horizontal"> | ||
<clr-input-container> | ||
<label>Email</label> | ||
<input clrInput placeholder="[email protected]" type="text" formControlName="email" autocomplete="username" /> | ||
<clr-control-error>Please provide an email address.</clr-control-error> | ||
</clr-input-container> | ||
<form clrForm [formGroup]="registerForm" (ngSubmit)="onSubmit()" clrLayout="horizontal"> | ||
<clr-input-container> | ||
<label>Email</label> | ||
<input clrInput placeholder="[email protected]" type="text" formControlName="email" autocomplete="username" /> | ||
<clr-control-error>Please provide an email address.</clr-control-error> | ||
</clr-input-container> | ||
|
||
<clr-input-container> | ||
<label>Name</label> | ||
<input clrInput placeholder="John Doe" type="text" formControlName="name" autocomplete="name" /> | ||
<clr-control-error>Please provide a name.</clr-control-error> | ||
</clr-input-container> | ||
<clr-input-container> | ||
<label>Name</label> | ||
<input clrInput placeholder="John Doe" type="text" formControlName="name" autocomplete="name" /> | ||
<clr-control-error>Please provide a name.</clr-control-error> | ||
</clr-input-container> | ||
|
||
<clr-password-container> | ||
<label>Password</label> | ||
<input clrPassword placeholder="Password" formControlName="password" autocomplete="current-password" /> | ||
<clr-control-error>Please provide a password.</clr-control-error> | ||
</clr-password-container> | ||
<br /> | ||
<button [clrLoading]="registerBtnState" [disabled]="registerForm.invalid" class="btn btn-primary">register</button> | ||
</form> | ||
<clr-password-container> | ||
<label>Password</label> | ||
<input clrPassword placeholder="Password" formControlName="password" autocomplete="current-password" /> | ||
<clr-control-error>Please provide a password.</clr-control-error> | ||
</clr-password-container> | ||
<br /> | ||
<button [clrLoading]="registerBtnState" [disabled]="registerForm.invalid" class="btn btn-primary">register</button> | ||
</form> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
</app-center-card> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/app/shared/components/center-card/center-card.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div class="clr-row"> | ||
|
||
<div class="clr-col-xl-4 clr-offset-xl-4 | ||
clr-col-lg-6 clr-offset-lg-3 | ||
clr-col-md-8 clr-offset-md-2"> | ||
|
||
<ng-content></ng-content> | ||
</div> | ||
|
||
</div> | ||
|
||
|
||
|
25 changes: 25 additions & 0 deletions
25
src/app/shared/components/center-card/center-card.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { CenterCardComponent } from './center-card.component'; | ||
|
||
describe('CenterCardComponent', () => { | ||
let component: CenterCardComponent; | ||
let fixture: ComponentFixture<CenterCardComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ CenterCardComponent ] | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(CenterCardComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.