Create login route and protect routes #3

Merged
csimonis merged 1 commits from feature/auth into main 2024-12-18 09:27:44 +00:00
10 changed files with 56 additions and 60 deletions

View File

@ -1,2 +1,2 @@
<app-employee-list></app-employee-list>
<router-outlet></router-outlet>

View File

@ -1,29 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have the 'lf10StarterNew' title`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('lf10StarterNew');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, lf10StarterNew');
});
});

View File

@ -1,11 +1,10 @@
import {Component} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterOutlet} from '@angular/router';
import {EmployeeListComponent} from "./employee-list/employee-list.component";
@Component({
selector: 'app-root',
imports: [CommonModule, EmployeeListComponent],
imports: [CommonModule, RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})

View File

@ -1,3 +1,10 @@
import {Routes} from '@angular/router';
import {EmployeeListComponent} from "./employee-list/employee-list.component";
import {LoginComponent} from "./login/login.component";
import {AuthGuardService} from "./auth-guard.service";
export const routes: Routes = [];
export const routes: Routes = [
{path: 'login', component: LoginComponent},
{path: '', component: EmployeeListComponent, canActivate: [AuthGuardService]},
{path: '**', redirectTo: ''}
];

View File

@ -0,0 +1,20 @@
import {Injectable} from '@angular/core';
import {AuthService} from "./services/auth.service";
import {Router} from "@angular/router";
@Injectable({
providedIn: 'root'
})
export class AuthGuardService {
constructor(public auth: AuthService, public router: Router) {
}
canActivate(): boolean {
if (!this.auth.isAuthenticated()) {
this.router.navigate(['login']);
return false;
}
return true;
}
}

View File

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

View File

View File

@ -0,0 +1 @@
<p>login works!</p>

View File

@ -0,0 +1,11 @@
import { Component } from '@angular/core';
@Component({
selector: 'app-login',
imports: [],
templateUrl: './login.component.html',
styleUrl: './login.component.css'
})
export class LoginComponent {
}

View File

@ -0,0 +1,10 @@
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AuthService {
public isAuthenticated(): boolean {
return false;
}
}