Skip to content
This repository was archived by the owner on Oct 14, 2019. It is now read-only.

Commit cc0aa80

Browse files
author
Your Name
committed
Angular and Firebase App
1 parent 6e2b6ba commit cc0aa80

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
4+
export class AuthInfo {
5+
6+
constructor(
7+
public $uid:string
8+
) {
9+
10+
}
11+
12+
13+
isLoggedIn() {
14+
return !!this.$uid;
15+
}
16+
17+
}

src/app/shared/security/auth.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { Injectable } from '@angular/core';
2-
import {Observable, Subject} from "rxjs/Rx";
2+
import {Observable, Subject, BehaviorSubject} from "rxjs/Rx";
33
import {FirebaseAuth, FirebaseAuthState} from "angularfire2/index";
4+
import {AuthInfo} from "./auth-info";
45

56
@Injectable()
67
export class AuthService {
78

89

10+
static UNKNOWN_USER = new AuthInfo(null);
11+
12+
authInfo$: BehaviorSubject<AuthInfo> = new BehaviorSubject<AuthInfo>(AuthService.UNKNOWN_USER);
13+
14+
915
constructor(private auth: FirebaseAuth) {
1016

1117
}
@@ -26,10 +32,13 @@ export class AuthService {
2632

2733
promise
2834
.then(res => {
35+
const authInfo = new AuthInfo(this.auth.getAuth().uid);
36+
this.authInfo$.next(authInfo);
2937
subject.next(res);
3038
subject.complete();
3139
},
3240
err => {
41+
this.authInfo$.error(err);
3342
subject.error(err);
3443
subject.complete();
3544
});

src/app/top-menu/top-menu.component.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
routerLinkActive="menu-active">Courses</a>
1313
</li>
1414
<li>
15-
<a routerLinkActive="menu-active" routerLink="login">Login</a>
15+
<a routerLinkActive="menu-active" routerLink="login"
16+
*ngIf="!authInfo?.isLoggedIn()">Login</a>
1617
</li>
1718
<li>
18-
<a routerLinkActive="menu-active" routerLink="register">Register</a>
19+
<a routerLinkActive="menu-active" routerLink="register"
20+
*ngIf="!authInfo?.isLoggedIn()">Register</a>
1921
</li>
2022
<li>
21-
<a>Logout</a>
23+
<a *ngIf="authInfo?.isLoggedIn()">Logout</a>
2224
</li>
2325
</ul>
2426

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
2+
import {AuthService} from "../shared/security/auth.service";
3+
import {AuthInfo} from "../shared/security/auth-info";
24

35
@Component({
46
selector: 'top-menu',
@@ -7,9 +9,20 @@ import { Component, OnInit } from '@angular/core';
79
})
810
export class TopMenuComponent implements OnInit {
911

10-
constructor() { }
12+
authInfo: AuthInfo;
13+
14+
constructor(private authService:AuthService) {
15+
16+
17+
18+
}
1119

1220
ngOnInit() {
21+
22+
23+
this.authService.authInfo$.subscribe(authInfo => this.authInfo = authInfo);
24+
25+
1326
}
1427

1528
}

0 commit comments

Comments
 (0)