Skip to content

Commit da2a7ed

Browse files
signup and login done,dashboard work remain
1 parent 64c2689 commit da2a7ed

12 files changed

+122
-38
lines changed

src/app/app-routing.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NgModule } from '@angular/core';
22
import { RouterModule, Routes } from '@angular/router';
33
import { AppComponent } from './app.component';
4+
import { AuthGuardGuard } from './auth-guard.guard';
45
import { DashboardComponent } from './dashboard/dashboard.component';
56
import { HomeComponent } from './home/home.component';
67
import { LoginComponent } from './login/login.component';
@@ -10,7 +11,7 @@ import { RegisterComponent } from './register/register.component';
1011
const routes: Routes = [
1112
{ path: "login", component: LoginComponent, pathMatch: "full" },
1213
{ path: "register", component: RegisterComponent, pathMatch: "full" },
13-
{ path: "dashboard", component: DashboardComponent, pathMatch: "full" },
14+
{ path: "dashboard", component: DashboardComponent, pathMatch: "full", canActivate: [AuthGuardGuard] },
1415
{ path: "", component: HomeComponent, pathMatch: "full" },
1516
{ path: "app", component: AppComponent, pathMatch: "full" }
1617
];

src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import { Component } from '@angular/core';
77
})
88
export class AppComponent {
99
title = 'Demo';
10-
displayDashboard = "true";
10+
displayDashboard = "false";
1111
}

src/app/auth-guard.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 { AuthGuardGuard } from './auth-guard.guard';
4+
5+
describe('AuthGuardGuard', () => {
6+
let guard: AuthGuardGuard;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
guard = TestBed.inject(AuthGuardGuard);
11+
});
12+
13+
it('should be created', () => {
14+
expect(guard).toBeTruthy();
15+
});
16+
});

src/app/auth-guard.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 { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
3+
import { Observable } from 'rxjs';
4+
import { LoginServiceService } from './login-service.service';
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class AuthGuardGuard implements CanActivate {
10+
constructor(private loginService: LoginServiceService, private router: Router) { }
11+
canActivate(
12+
route: ActivatedRouteSnapshot,
13+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
14+
if (this.loginService.isLogin()) {
15+
return true;
16+
}
17+
this.router.navigate(['/login']);
18+
return false;
19+
}
20+
21+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
.spacer {
2-
flex: 1 1 auto;
3-
}
1+
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
<!-- <mat-toolbar style="background-color: brown;">
2-
<span><a style="text-decoration: none; color: rgb(253, 253, 251);"routerLink="/">Global Bank</a></span>
3-
<div class="spacer"> </div>
4-
<!-- <a mat-button routerLink="/">Home</a>&nbsp; -->
5-
<!-- <a mat-raised-button color="yellow" routerLink="mycard">Mycard</a>&nbsp; -->
6-
<!-- <a mat-raised-button color="yellow" routerLink="login">Logout</a>&nbsp; -->
7-
<!-- <a mat-raised-button color="accent" routerLink="register">Register</a> -->
8-
<!-- </mat-toolbar> -->
9-
10-
11-
<p>Dashbord Works</p>
1+
<div class="container">
2+
<p>Dashbord Works</p>
3+
</div>
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 { LoginServiceService } from './login-service.service';
4+
5+
describe('LoginServiceService', () => {
6+
let service: LoginServiceService;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(LoginServiceService);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});

src/app/login-service.service.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable } from '@angular/core';
2+
import { User1 } from './user1';
3+
import { Observable } from 'rxjs';
4+
import { HttpClient } from '@angular/common/http';
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class LoginServiceService {
10+
11+
constructor(private _http: HttpClient) { }
12+
13+
public doLogin(user: User1): Observable<any> {
14+
return this._http.post<any>("https://global-rest-api.herokuapp.com/userAuth/login", user);
15+
}
16+
17+
isLogin() {
18+
return true;
19+
}
20+
21+
}
22+
23+

src/app/login/login.component.html

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
<mat-tab-group mat-stretch-tabs class="example-stretched-tabs">
55
<mat-tab label="Login">
66
<mat-card-content>
7-
<form>
7+
<form (ngSubmit)="loginNow()">
8+
<small class="text-success ml-5">{{msg}}</small>
89
<mat-form-field>
910
<input
1011
matInput
1112
placeholder="Email"
1213
required
1314
name="email"
15+
[(ngModel)]="user1.email"
1416
ngModel
1517
#email="ngModel">
1618
<mat-error *ngIf="email.invalid">Email is required</mat-error>
@@ -24,34 +26,35 @@
2426
required
2527
minlength="6"
2628
name="password"
29+
[(ngModel)]="user1.password"
2730
ngModel
2831
#password="ngModel">
2932
<mat-error
3033
*ngIf="password.errors && password.errors.required">
3134
Password is required
3235
</mat-error>
33-
<mat-error
36+
<!-- <mat-error
3437
*ngIf="password.errors && password.errors.minlength">
3538
Password must be of length 6
36-
</mat-error>
39+
</mat-error> -->
3740
<!-- <mat-icon matSuffix>lock</mat-icon> -->
3841
</mat-form-field>
3942
<!-- <mat-checkbox>Remember Me!</mat-checkbox> -->
4043
<button
4144
mat-raised-button
4245
color="primary"
4346
class="login-button"
44-
routerLink="/dashboard">
47+
type="submit">
4548
Login
4649
</button>
4750

48-
<button
51+
<!-- <button
4952
mat-raised-button
5053
color="accent"
5154
class="login-button">
5255
Login with Google
53-
</button>
54-
<p class="login-button">Do not have account ! please <a routerLink="register" >Register</a></p>
56+
</button> -->
57+
<p class="login-button">Do not have account ! please <a routerLink="/register" >Register</a></p>
5558
</form>
5659
</mat-card-content>
5760
</mat-tab>

src/app/register/register.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<mat-tab label="Sign Up">
66
<mat-card-content>
77
<form (ngSubmit)="registerNow()">
8+
<small class="text-success ml-5" >{{msg}}</small>
89
<mat-form-field>
910
<input
1011
matInput
@@ -64,6 +65,7 @@
6465
required
6566
minlength="6"
6667
name="repassword"
68+
ngModel="user.repassword"
6769
ngModel
6870
#repassword="ngModel">
6971
<mat-error

0 commit comments

Comments
 (0)