Skip to content

Commit 7e5bb3f

Browse files
login authentication
1 parent d9acaa6 commit 7e5bb3f

File tree

8 files changed

+103
-6
lines changed

8 files changed

+103
-6
lines changed

src/app/app.module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33
import { ReactiveFormsModule } from '@angular/forms';
44
import { RouterModule, Routes } from '@angular/router';
5+
import { HttpClientModule } from '@angular/common/http';
56

67
import { AppComponent } from './app.component';
78
import { LoginComponent } from './login/login.component';
89
import { DashboardComponent } from './dashboard/dashboard.component';
910
import { HeaderComponent } from './header/header.component';
1011
import { FooterComponent } from './footer/footer.component';
1112

13+
import { AuthService } from './services/auth.service';
1214

1315
const routes: Routes = [
1416
{
@@ -22,7 +24,8 @@ const routes: Routes = [
2224
},
2325
{
2426
path: 'dashboard',
25-
component: DashboardComponent
27+
component: DashboardComponent,
28+
canActivate: [ AuthService ]
2629
}
2730
];
2831

@@ -36,10 +39,11 @@ const routes: Routes = [
3639
],
3740
imports: [
3841
BrowserModule,
42+
HttpClientModule,
3943
ReactiveFormsModule,
4044
RouterModule.forRoot(routes)
4145
],
42-
providers: [],
46+
providers: [AuthService],
4347
bootstrap: [AppComponent]
4448
})
4549
export class AppModule { }

src/app/header/header.component.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
</ul>
2525
<ul class="navbar-nav ml-auto">
2626
<li class="nav-item">
27-
<a class="nav-link" data-toggle="modal" data-target="#exampleModal">
28-
<i class="fa fa-fw fa-sign-out"></i>Logout</a>
27+
<a class="nav-link" [routerLink]="['/login']">
28+
<i class="fa fa-fw fa-sign-out"></i>Logout
29+
</a>
2930
</li>
3031
</ul>
3132
</div>

src/app/login/login.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<div class="card card-login mx-auto mt-5">
33
<div class="card-header">Login</div>
44
<div class="card-body">
5+
<div class="alert alert-danger" [hidden]="!error">
6+
<strong>Error!</strong> Invalid Credentials
7+
</div>
58
<form (ngSubmit)="onSubmit()" [formGroup]="loginForm">
69
<div class="form-group" [ngClass]="{'has-error': loginForm.controls['email'].errors && !loginForm.controls['email'].pristine}">
710
<label for="email">Email address</label>

src/app/login/login.component.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { Router } from '@angular/router';
2+
import { Router, ActivatedRoute } from '@angular/router';
33
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
44

5+
import { AuthService } from '../services/auth.service';
6+
57
@Component({
68
selector: 'app-login',
79
templateUrl: './login.component.html',
@@ -14,21 +16,36 @@ export class LoginComponent implements OnInit {
1416
password: ''
1517
};
1618

19+
error = false;
20+
returnUrl: string;
1721
loginForm: FormGroup;
1822

1923
constructor(
2024
private router: Router,
25+
private route: ActivatedRoute,
26+
private authService: AuthService,
2127
private formBuilder: FormBuilder
2228
) { }
2329

2430
ngOnInit() {
31+
32+
// logout current user
33+
this.authService.logout();
34+
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/dashboard';
35+
36+
// login form builder
2537
this.loginForm = this.formBuilder.group({
2638
email: [null, [Validators.required, Validators.email]],
2739
password: [null, Validators.required]
2840
});
2941
}
3042

3143
onSubmit() {
32-
this.router.navigate(['/dashboard']);
44+
this.error = false;
45+
if ( this.authService.login(this.loginForm.value) ) {
46+
this.router.navigate([this.returnUrl]);
47+
} else {
48+
this.error = true;
49+
}
3350
}
3451
}

src/app/services/auth.service.spec.ts

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

src/app/services/auth.service.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
3+
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
4+
5+
@Injectable()
6+
export class AuthService implements CanActivate {
7+
8+
constructor(
9+
private router: Router,
10+
private http: HttpClient
11+
) {}
12+
13+
login(data): boolean {
14+
window.localStorage.setItem('user', JSON.stringify(data));
15+
return true;
16+
}
17+
18+
logout(): void {
19+
window.localStorage.removeItem('user');
20+
}
21+
22+
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
23+
24+
if (localStorage.getItem('user')) {
25+
return true;
26+
}
27+
28+
this.router.navigate(['/login'], {
29+
queryParams: { returnUrl: state.url }
30+
});
31+
32+
return false;
33+
}
34+
}

src/app/services/user.service.spec.ts

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

src/app/services/user.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable()
4+
export class UserService {
5+
6+
constructor() { }
7+
8+
}

0 commit comments

Comments
 (0)