Skip to content

Commit 261ea7e

Browse files
author
Your Name
committed
Reactive Angular Course
1 parent 69bd6b4 commit 261ea7e

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
<span>Register</span>
2323
</a>
2424

25-
<a mat-list-item routerLink="login">
25+
<a mat-list-item routerLink="login" *ngIf="auth.isLoggedOut$ | async">
2626
<mat-icon>account_circle</mat-icon>
2727
<span>Login</span>
2828
</a>
2929

30-
<a mat-list-item (click)="logout()">
30+
<a mat-list-item (click)="logout()" *ngIf="auth.isLoggedIn$ | async">
3131
<mat-icon>exit_to_app</mat-icon>
3232
<span>Logout</span>
3333
</a>

src/app/app.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Component, OnInit} from '@angular/core';
22
import {LoadingService} from './loading/loading.service';
33
import {MessagesService} from './messages/messages.service';
4+
import {AuthStore} from './services/auth.store';
45

56

67

@@ -11,7 +12,7 @@ import {MessagesService} from './messages/messages.service';
1112
})
1213
export class AppComponent implements OnInit {
1314

14-
constructor() {
15+
constructor(public auth: AuthStore) {
1516

1617
}
1718

@@ -21,6 +22,7 @@ export class AppComponent implements OnInit {
2122
}
2223

2324
logout() {
25+
this.auth.logout();
2426

2527
}
2628

src/app/login/login.component.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {FormBuilder, FormGroup, Validators} from '@angular/forms';
33

44

55
import {Router} from '@angular/router';
6+
import {AuthStore} from '../services/auth.store';
67

78
@Component({
89
selector: 'login',
@@ -15,7 +16,8 @@ export class LoginComponent implements OnInit {
1516

1617
constructor(
1718
private fb: FormBuilder,
18-
private router: Router) {
19+
private router: Router,
20+
private auth: AuthStore) {
1921

2022
this.form = fb.group({
2123
email: ['test@angular-university.io', [Validators.required]],
@@ -32,6 +34,16 @@ export class LoginComponent implements OnInit {
3234

3335
const val = this.form.value;
3436

37+
this.auth.login(val.email, val.password)
38+
.subscribe(
39+
() => {
40+
this.router.navigateByUrl("/courses")
41+
},
42+
err => {
43+
alert("Login failed!");
44+
}
45+
);
46+
3547

3648

3749
}

0 commit comments

Comments
 (0)