处理 UI 中的授权错误
Handle authorization error in UI
我已经尝试在我的 Angular 中实施授权几个小时了:example。我已经构建了一个 HTTP 拦截器来捕获错误,但我不确定如何在我的登录视图中显示它。我试图传递一个变量,但我无法让它工作
这些是我的 AuthService 的几个方法:
login(email: string, password: string) {
this.logout();
return this.http
.post("http://localhost:3000/user/login", {
email,
password
})
.do(res => {
this.setSession(res);
})
.shareReplay();
}
private setSession(authResult) {
const expiresAt = moment().add(authResult.data.expiresIn, "second");
localStorage.setItem("id_token", authResult.data.idToken);
localStorage.setItem("expires_at", JSON.stringify(expiresAt.valueOf()));
}
这是我的 Http Inceptor:
@Injectable()
导出 class AuthInterceptor 实现 HttpInterceptor {
constructor(private router: Router, private _data: AuthdataService) { }
intercept(req: HttpRequest<any>,
next: HttpHandler): Observable<HttpEvent<any>> {
const idToken = localStorage.getItem("id_token");
if (idToken) {
const cloned = req.clone({
headers: req.headers.set("Authorization",
"Bearer " + idToken)
});
return next.handle(cloned);
}
else {
return next.handle(req).do(
succ => console.log(succ),
err => {
if (err.status === 401) {
console.error('AUTHENTICATION FAILED');
this.router.navigateByUrl('/login');
//HOW CAN I SEND THIS TO THE UI?
}
}
);
}
}
}
最后是我的登录组件:
loginUser() {
if (this.loginForm.valid) {
const email = this.loginForm.value.email;
const password = this.loginForm.value.password;
//set loading to true and then false if error
this.loading = false;
this._data.login(email, password).subscribe(() => {
if (this._data.isLoggedIn()) {
this.router.navigateByUrl("/");
} else {
//never reaches here
}
});
}
}
我尝试从我的服务调用 isLoggedIn(),它检查本地存储中的令牌,但是永远不会命中 else 语句,因为如果数据服务抛出 401 错误则不会执行该方法..
总结一下:如何显示“我的登录 HTML 组件的电子邮件或密码不正确?
之类的消息
感谢您的帮助。
您可以从拦截器中抛出错误并在登录组件中捕获它。
应该是这样的:
return Observable.throw(err);
在登录组件中捕获它:
this._data.login(email, password).subscribe(() => {
if (this._data.isLoggedIn()) {
this.router.navigateByUrl("/");
} else {
//never reaches here
}
}, err => {
/* Write your logic here*/
});
我已经尝试在我的 Angular 中实施授权几个小时了:example。我已经构建了一个 HTTP 拦截器来捕获错误,但我不确定如何在我的登录视图中显示它。我试图传递一个变量,但我无法让它工作
这些是我的 AuthService 的几个方法:
login(email: string, password: string) {
this.logout();
return this.http
.post("http://localhost:3000/user/login", {
email,
password
})
.do(res => {
this.setSession(res);
})
.shareReplay();
}
private setSession(authResult) {
const expiresAt = moment().add(authResult.data.expiresIn, "second");
localStorage.setItem("id_token", authResult.data.idToken);
localStorage.setItem("expires_at", JSON.stringify(expiresAt.valueOf()));
}
这是我的 Http Inceptor:
@Injectable() 导出 class AuthInterceptor 实现 HttpInterceptor {
constructor(private router: Router, private _data: AuthdataService) { }
intercept(req: HttpRequest<any>,
next: HttpHandler): Observable<HttpEvent<any>> {
const idToken = localStorage.getItem("id_token");
if (idToken) {
const cloned = req.clone({
headers: req.headers.set("Authorization",
"Bearer " + idToken)
});
return next.handle(cloned);
}
else {
return next.handle(req).do(
succ => console.log(succ),
err => {
if (err.status === 401) {
console.error('AUTHENTICATION FAILED');
this.router.navigateByUrl('/login');
//HOW CAN I SEND THIS TO THE UI?
}
}
);
}
}
}
最后是我的登录组件:
loginUser() {
if (this.loginForm.valid) {
const email = this.loginForm.value.email;
const password = this.loginForm.value.password;
//set loading to true and then false if error
this.loading = false;
this._data.login(email, password).subscribe(() => {
if (this._data.isLoggedIn()) {
this.router.navigateByUrl("/");
} else {
//never reaches here
}
});
}
}
我尝试从我的服务调用 isLoggedIn(),它检查本地存储中的令牌,但是永远不会命中 else 语句,因为如果数据服务抛出 401 错误则不会执行该方法..
总结一下:如何显示“我的登录 HTML 组件的电子邮件或密码不正确?
之类的消息感谢您的帮助。
您可以从拦截器中抛出错误并在登录组件中捕获它。
应该是这样的:
return Observable.throw(err);
在登录组件中捕获它:
this._data.login(email, password).subscribe(() => {
if (this._data.isLoggedIn()) {
this.router.navigateByUrl("/");
} else {
//never reaches here
}
}, err => {
/* Write your logic here*/
});