0
내가 Angular2에서 AuthService 온을 buiding있어 모든 값을 방출하고, 인증-변경 내용을 방송 몇 가지 문제가없는AngularFire.auth는 ... <code>this.af.auth.subscribe</code> 아무것도 방출하지 않는 것 같다</p> <p>...
import {Injectable, OnInit, OnDestroy} from '@angular/core';
import {Http} from "@angular/http";
import {Router} from "@angular/router";
import {JwtHelper} from "angular2-jwt/angular2-jwt";
import {AngularFire, AngularFireAuth, AuthProviders, AuthMethods} from "angularfire2";
import {environment} from "../environments/environment";
import {Subject, Subscription} from "rxjs";
import {Output} from "@angular/core/src/metadata/directives";
@Injectable()
export class AuthService implements OnInit, OnDestroy{
@Output() public loginStatus$ = new Subject<LoginStatus>();
private auth$:Subscription;
ngOnInit(): void {
this.auth$ = this.af.auth.subscribe(user => {
console.log(user);
if (user)
this.loginStatus$.next(new LoginStatus(true,"Login successful", this.getDecodedJwt()));
else if (!user && this.getDecodedJwt())
this.loginStatus$.next(new LoginStatus(false,"Could not login to real-time database!"));
else
this.loginStatus$.next(new LoginStatus(false,"Logged out."));
},
error => console.log(error))
}
constructor(
private http:Http,
private router:Router,
private jwtHelper: JwtHelper,
private af: AngularFire,
private auth: AngularFireAuth,
) {}
ngOnDestroy(): void {
this.auth$.unsubscribe();
this.loginStatus$.complete();
}
login(login:LoginBody) {
this.http.post(environment.baseUri + "auth/login_check", login)
.subscribe(
response => {
localStorage.setItem("auth_token",response.json().token);
this.auth.login(response.json().token, {
provider: AuthProviders.Custom,
method: AuthMethods.CustomToken
});},
error => {
console.log(error);
switch (error.status){
case 403:this.loginStatus$.next(new LoginStatus(false,"Password does not match the supplied email address.", error)); break;
case 404:this.loginStatus$.next(new LoginStatus(false,"Unknown username or password.", error));break;
default: this.loginStatus$.next(new LoginStatus(false,"Server error. We are working on a solution, please try again later.", error));break;
}
});
return this.loginStatus$;
}
logout() {
localStorage.removeItem("auth_token");
this.af.auth.logout();
this.router.navigateByUrl("");
}
hasRole(role:string){
if (!this.isLoggedIn())
return false;
const jwt = this.getDecodedJwt();
for (let i = 0; i < jwt.claims.roles.length;i++)
{
if (role.toUpperCase() === jwt.claims.roles[i].toUpperCase())
return true;
}
return false;
}
private getDecodedJwt() {
return this.jwtHelper.decodeToken(localStorage.getItem("auth_token"));
}
isLoggedIn() {
return !!localStorage.getItem("auth_token");
}
}
export class LoginBody {
constructor(
public _username: string,
public _password: string
){}
}
export class LoginStatus {
constructor(public status: boolean, public message: string, public token: any = []) {}
}
AuthService은 내 LoginComponent에 사용됩니다
모든 요청이 작동login(form) {
this.loginStatus$ = this.authService.login(form.value)
.subscribe(status => console.log(status));
}
... 사용자는 내 서버에서 JWT 토큰을 얻을하지 않습니다, 그는 네트워크에서 (중포 기지에 로그인됩니까 탭).