Skip to content

Commit 447e25d

Browse files
Merge pull request #4 from HackTzi/Login-screen
Login, Home and routes screens
2 parents e6c7090 + 11c28d4 commit 447e25d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+615
-21
lines changed

src/app/app-routing.module.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
import { NgModule } from '@angular/core';
2-
import { Routes, RouterModule } from '@angular/router';
2+
import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
33
import { PageNotFoundComponent } from './shared/components';
44

5+
// Routes
56
import { HomeRoutingModule } from './home/home-routing.module';
67

8+
// Guards
9+
import { AuthGuard } from './guards/auth.guard';
10+
11+
// Components
12+
import { AuthComponent } from './auth/auth.component';
13+
import { MenuComponent } from './menu/menu.component';
14+
715
const routes: Routes = [
16+
{ path: 'auth', component: AuthComponent },
817
{
9-
path: '',
10-
redirectTo: 'home',
11-
pathMatch: 'full'
18+
path: 'app', canActivate: [AuthGuard], component: MenuComponent,
19+
loadChildren: () => import('./home/home.module').then(m => m.HomeModule)
1220
},
13-
{
14-
path: '**',
15-
component: PageNotFoundComponent
16-
}
21+
{ path: '', redirectTo: '/app/home', pathMatch: 'full' },
22+
{ path: '**', component: PageNotFoundComponent, } // Wildcard route for a 404 page
1723
];
1824

1925
@NgModule({
2026
imports: [
21-
RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy' }),
27+
RouterModule.forRoot(routes, {
28+
relativeLinkResolution: 'legacy',
29+
preloadingStrategy: PreloadAllModules
30+
}),
2231
HomeRoutingModule,
2332
],
2433
exports: [RouterModule]

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
2323
}
2424

2525
import { AngularMaterialModule } from './angular-material.module';
26+
import { AuthComponent } from './auth/auth.component';
27+
import { MenuComponent } from './menu/menu.component';
2628

2729
@NgModule({
28-
declarations: [AppComponent],
30+
declarations: [AppComponent, AuthComponent, MenuComponent],
2931
imports: [
3032
BrowserModule,
3133
FormsModule,

src/app/auth/auth.component.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<form [formGroup]="authForm">
2+
<mat-card>
3+
<mat-card-header>
4+
<img mat-card-image src="" alt="Photo of platzi logo">
5+
<mat-card-title>{{'PAGES.AUTH.TITLE' | translate}}</mat-card-title>
6+
</mat-card-header>
7+
<mat-card-content style="margin:auto;">
8+
<mat-form-field>
9+
<input formControlName="username" matInput placeholder="{{'PAGES.AUTH.EMAIL'|translate}}" type="email">
10+
</mat-form-field>
11+
<mat-form-field>
12+
<input formControlName="password" matInput placeholder="{{'PAGES.AUTH.PASSWORD'|translate}}" type="password">
13+
</mat-form-field>
14+
<button [disabled]="!authForm.valid" color="accent" mat-raised-button (click)="login()">{{'PAGES.AUTH.START'|translate}}</button>
15+
</mat-card-content>
16+
</mat-card>
17+
</form>
18+

src/app/auth/auth.component.scss

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { AuthComponent } from './auth.component';
4+
5+
describe('AuthComponent', () => {
6+
let component: AuthComponent;
7+
let fixture: ComponentFixture<AuthComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [ AuthComponent ]
12+
})
13+
.compileComponents();
14+
});
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(AuthComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});

src/app/auth/auth.component.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable @typescript-eslint/unbound-method */
2+
import { Component, Inject, OnInit } from '@angular/core';
3+
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
4+
import { Router } from '@angular/router';
5+
6+
@Component({
7+
selector: 'app-auth',
8+
templateUrl: './auth.component.html',
9+
styleUrls: ['./auth.component.scss']
10+
})
11+
export class AuthComponent implements OnInit {
12+
constructor(
13+
@Inject(FormBuilder) fb: FormBuilder,
14+
private router: Router
15+
) {
16+
this.authForm = fb.group({
17+
username: new FormControl({ value: '', disabled: false }, [Validators.required, Validators.email]),
18+
password: new FormControl({ value: '', disabled: false }, [Validators.required, Validators.pattern(this.passPattern)])
19+
});
20+
}
21+
authForm: FormGroup;
22+
passPattern: RegExp;
23+
loading = false;
24+
25+
ngOnInit(): void { }
26+
27+
// TODO: Add login call to auth.service
28+
login() {
29+
this.loading = true;
30+
localStorage.setItem('token', 'TOKEN FOR TEST SET IN AUTH COMPONENT');
31+
32+
this.router.navigate(['/app/home']);
33+
setTimeout(() => this.loading = false, 2000); // Quit loading timeout for test
34+
}
35+
36+
}

src/app/guards/auth.guard.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { AuthGuard } from './auth.guard';
4+
5+
describe('AuthGuard', () => {
6+
let guard: AuthGuard;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
guard = TestBed.inject(AuthGuard);
11+
});
12+
13+
it('should be created', () => {
14+
expect(guard).toBeTruthy();
15+
});
16+
});

src/app/guards/auth.guard.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
3+
import { Observable } from 'rxjs';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class AuthGuard implements CanActivate {
9+
constructor(private router: Router) { }
10+
canActivate(
11+
route: ActivatedRouteSnapshot,
12+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
13+
if (localStorage.getItem('token')) {
14+
return true;
15+
} else {
16+
this.router.navigate(['/auth']);
17+
return false;
18+
}
19+
}
20+
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>agenda works!</p>

src/app/home/agenda/agenda.component.scss

Whitespace-only changes.

0 commit comments

Comments
 (0)