Skip to content

Commit e0130e1

Browse files
committed
4.29
1 parent 488d18b commit e0130e1

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

server/db-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,8 @@ export const LESSONS = {
878878
export const USERS = {
879879
1: {
880880
id: 1,
881-
email: 'test@angular-university.io',
882-
password: 'test',
881+
email: 'cognito@inc.com',
882+
password: 'localstorage',
883883
pictureUrl: 'https://lh3.googleusercontent.com/-1pUNnTB3vaA/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rdn4uEc0ti8YE4Uuw6_Kz04tVe2Mg.CMID/s32-c/photo.jpg'
884884
}
885885

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.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: 14 additions & 4 deletions
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,11 +16,12 @@ 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({
21-
email: ['test@angular-university.io', [Validators.required]],
22-
password: ['test', [Validators.required]]
23+
email: ['cognito@inc.com', [Validators.required]],
24+
password: ['localstorage', [Validators.required]]
2325
});
2426

2527
}
@@ -32,7 +34,15 @@ export class LoginComponent implements OnInit {
3234

3335
const val = this.form.value;
3436

35-
37+
this.auth.login(val.email, val.password)
38+
.subscribe(
39+
() => {
40+
this.router.navigateByUrl('/courses');
41+
},
42+
err => {
43+
alert("login has failed");
44+
}
45+
);
3646

3747
}
3848

src/app/services/auth.store.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Injectable } from "@angular/core";
33
import { BehaviorSubject, Observable } from "rxjs";
44
import { map, shareReplay, tap } from "rxjs/operators";
55
import { User } from "../model/user";
6+
import {Router} from '@angular/router';
7+
8+
9+
const AUTH_DATA = "auth_data";
610

711
@Injectable({
812
providedIn: 'root'
@@ -16,23 +20,32 @@ export class AuthStore {
1620
isLoggedIn$: Observable<boolean>;
1721
isLoggedOut$: Observable<boolean>;
1822

19-
constructor(private http: HttpClient) {
23+
constructor(private http: HttpClient,private router: Router) {
2024
this.isLoggedIn$ = this.user$.pipe(map(user => !!user));
2125
this.isLoggedOut$ = this.isLoggedIn$.pipe(map(loggedIn => !loggedIn));
26+
const user = localStorage.getItem(AUTH_DATA);
27+
if(user){
28+
this.subject.next(JSON.parse(user));
29+
}
2230
}
2331

2432
login(email: string, password: string): Observable<User> {
2533

2634
return this.http.post<User>("/api/login", { email, password })
2735
.pipe(
28-
tap(user => this.subject.next(user)),
36+
tap(user => {
37+
this.subject.next(user);
38+
localStorage.setItem(AUTH_DATA, JSON.stringify(user));
39+
}),
2940
shareReplay()
3041
);
3142

3243
}
3344

3445
logout() {
3546
this.subject.next(null);
47+
localStorage.removeItem(AUTH_DATA);
48+
this.router.navigateByUrl('/login');
3649
}
3750

3851
}

0 commit comments

Comments
 (0)