Début d'ajout pour la gestion du login

This commit is contained in:
Sébastien André
2020-03-11 11:22:35 +01:00
parent bf695b431c
commit 8a29fd7de9
19 changed files with 296 additions and 39 deletions

View File

@@ -17,9 +17,9 @@ import { NewGearComponent } from './new-gear/new-gear.component';
import { NewDropZoneComponent } from './new-drop-zone/new-drop-zone.component';
import { NewJumpTypeComponent } from './new-jump-type/new-jump-type.component';
import { DefaultComponent } from './default/default.component';
import { LoginComponent } from './login/login.component';
import { DateService } from '../services/date.service';
import { AircraftService } from '../services/aircraft.service';
import { DropzoneService } from '../services/dropzone.service';
import { GearService } from '../services/gear.service';
@@ -27,6 +27,8 @@ import { JumpService } from '../services/jump.service';
import { JumpTypeService } from '../services/jump-type.service';
import { StatsService } from '../services/stats.service';
import { ServiceComm } from '../services/service-comm.service';
import { RequestCache } from '../services/request-cache.service';
import { AuthGuardService } from '../services/auth-guard.service';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@@ -43,19 +45,23 @@ import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSelectModule } from '@angular/material/select';
import { MatTableModule } from '@angular/material/table';
import { RequestCache } from '../services/request-cache.service';
import { CachingInterceptor } from '../services/caching-interceptor.service';
import { CachingInterceptor } from '../interceptor/caching.interceptor';
import { BasicAuthInterceptor } from '../interceptor/basic-auth.interceptor';
import { ErrorInterceptor } from '../interceptor/error.interceptor';
const appRoutes: Routes = [
{ path: '', component: DefaultComponent },
{ path: '', component: DefaultComponent, canActivate: [AuthGuardService] },
{ path: 'summary', component: SummaryComponent },
{ path: 'jumps', component: ListOfJumpsComponent },
{ path: 'dzs', component: ListOfDzsComponent },
{ path: 'newjump', component: NewJumpComponent },
{ path: 'aircrafts', component: ListOfAircraftsComponent },
{ path: 'jumpTypes', component: ListOfJumpTypesComponent },
{ path: 'gears', component: ListOfGearsComponent },
{ path: 'summary', component: SummaryComponent, canActivate: [AuthGuardService] },
{ path: 'jumps', component: ListOfJumpsComponent, canActivate: [AuthGuardService] },
{ path: 'dzs', component: ListOfDzsComponent, canActivate: [AuthGuardService] },
{ path: 'newjump', component: NewJumpComponent, canActivate: [AuthGuardService] },
{ path: 'aircrafts', component: ListOfAircraftsComponent, canActivate: [AuthGuardService] },
{ path: 'jumpTypes', component: ListOfJumpTypesComponent, canActivate: [AuthGuardService] },
{ path: 'gears', component: ListOfGearsComponent, canActivate: [AuthGuardService] },
{ path: 'login', component: LoginComponent },
{ path: '**', redirectTo: '' }
];
@@ -74,7 +80,8 @@ const appRoutes: Routes = [
NewGearComponent,
NewDropZoneComponent,
NewJumpTypeComponent,
DefaultComponent
DefaultComponent,
LoginComponent
],
imports: [
RouterModule.forRoot(
@@ -111,6 +118,8 @@ const appRoutes: Routes = [
DateService,
RequestCache,
// { provide: HTTP_INTERCEPTORS, useClass: CachingInterceptor, multi: true }
{ provide: HTTP_INTERCEPTORS, useClass: BasicAuthInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
],
bootstrap: [AppComponent]
})

View File

@@ -0,0 +1,34 @@
<div class="col-md-6 offset-md-3 mt-5">
<div class="alert alert-info">
Username: test<br />
Password: test
</div>
<div class="card">
<h4 class="card-header">Angular 8 Basic Auth Login Example</h4>
<div class="card-body">
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="username">Username</label>
<input type="text" formControlName="username" class="form-control"
[ngClass]="{ 'is-invalid': submitted && f.username.errors }" />
<div *ngIf="submitted && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">Username is required</div>
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" formControlName="password" class="form-control"
[ngClass]="{ 'is-invalid': submitted && f.password.errors }" />
<div *ngIf="submitted && f.password.errors" class="invalid-feedback">
<div *ngIf="f.password.errors.required">Password is required</div>
</div>
</div>
<button [disabled]="loading" class="btn btn-primary">
<span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span>
Login
</button>
<div *ngIf="error" class="alert alert-danger mt-3 mb-0">{{error}}</div>
</form>
</div>
</div>
</div>

View File

@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LoginComponent } from './login.component';
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,67 @@
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { first } from 'rxjs/operators';
import { AuthenticationService } from '../../services/authentication.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;
loading = false;
submitted = false;
returnUrl: string;
error = '';
constructor(
private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private authenticationService: AuthenticationService
) {
// redirect to home if already logged in
if (this.authenticationService.currentUserValue) {
this.router.navigate(['/']);
}
}
ngOnInit() {
this.loginForm = this.formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
// get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
// convenience getter for easy access to form fields
get f() { return this.loginForm.controls; }
onSubmit() {
this.submitted = true;
// stop here if form is invalid
if (this.loginForm.invalid) {
return;
}
this.loading = true;
this.authenticationService.login(this.f.username.value, this.f.password.value)
.pipe(first())
.subscribe(
data => {
this.router.navigate([this.returnUrl]);
},
error => {
this.error = error;
this.loading = false;
});
}
}