我正在尝试使用以下教程在我们的应用程序中实现刷新令牌:
https://codinglatte.com/posts/angular/refreshing-authorization-tokens-angular-6/
我遇到以下错误:
TypeError: Cannot read property 'pipe' of null
at HttpAuthInterceptor.push../src/app/Interceptors/newauth.interceptor.ts.HttpAuthInterceptor.intercept (newauth.interceptor.ts:38)
at HttpInterceptorHandler.handle (http.js:1279)
at LoaderInterceptor.intercept (loader.interceptor.ts:55)
at HttpInterceptorHandler.handle (http.js:1279)
at HttpXsrfInterceptor.intercept (http.js:1888)
at HttpInterceptorHandler.handle (http.js:1279)
at HttpInterceptingHandler.handle (http.js:1932)
at MergeMapSubscriber.project (http.js:1099)
at MergeMapSubscriber._tryNext (mergeMap.js:61)
at MergeMapSubscriber._next (mergeMap.js:51)
我的代码是:
import { HttpEvent,HttpHandler,HttpInterceptor,HttpRequest } from '@angular/common/http';
import { Injectable, Injector } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import { catchError, switchMap } from 'rxjs/operators';
import { AuthService } from '../Services/Auth.service';
@Injectable()
export class HttpAuthInterceptor implements HttpInterceptor {
constructor(private injector: Injector, private Route: Router) { }
inflightAuthRequest = null;
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
// exempt some paths from authentication
if (req.headers.get('authExempt') === 'true') {
return next.handle(req);
}
const authService = this.injector.get(AuthService);
if (!this.inflightAuthRequest) {
this.inflightAuthRequest = authService.getToken();
}
return this.inflightAuthRequest.pipe(
switchMap((newToken: string) => {
// unset request inflight
this.inflightAuthRequest = null;
// use the newly returned token
const authReq = req.clone({
headers: req.headers.set('token', newToken ? newToken : '')
});
// resend the request
return next.handle(authReq);
}),
catchError(error => {
if (error.status === 401) {
// check if the response is from the token refresh end point
const isFromRefreshTokenEndpoint = !!error.headers.get('unableToRefreshToken');
if (isFromRefreshTokenEndpoint) {
localStorage.clear();
this.Route.navigate(['/login']);
return throwError(error);
}
if (!this.inflightAuthRequest) {
this.inflightAuthRequest = authService.refreshToken();
if (!this.inflightAuthRequest) {
// remove existing tokens
localStorage.clear();
this.Route.navigate(['/login']);
return throwError(error);
}
}
return this.inflightAuthRequest.pipe(
switchMap((newToken: string) => {
// unset inflight request
this.inflightAuthRequest = null;
// clone the original request
const authReqRepeat = req.clone({
headers: req.headers.set('token', newToken)
});
// resend the request
return next.handle(authReqRepeat);
})
);
} else {
return throwError(error);
}
})
);
}
}
有人可以告诉我我做错了什么吗?错误中的第38行引用了return this.inflightAuthRequest.pipe(
。我不确定我需要做什么,真的会感谢您的帮助。
答案 0 :(得分:0)
您缺少此部分:
public function login(ServerRequestInterface $request)
{
if (!auth()->attempt([
'email' => $request->getParsedBody()['email'],
'password' => $request->getParsedBody()['password']
])) {
return response()->json('failed attempt...');
}
auth()->login(User::where('id', Auth::user()->id)->first());
.
.
// I can access auth()->user() here just fine ..
}
之后:
public function logout()
{
//I can't access the authenticated user here
return auth()->user();
//return response()->json('Logged out successfully', 200);
}
我一定忘了将该部分包含在文章中,尽管该部分在回购中。抱歉。