Created
December 28, 2020 18:40
-
-
Save lydemann/c216b449b3875ff77e8c0dcddba37f43 to your computer and use it in GitHub Desktop.
set-token.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable({ providedIn: 'root' }) | |
export class SetTokenInterceptor implements HttpInterceptor { | |
constructor(private userService: UserService) {} | |
intercept( | |
req: HttpRequest<any>, | |
next: HttpHandler | |
): Observable<HttpEvent<any>> { | |
if (!this.userService.currentUser$.value) { | |
return next.handle(req); | |
} | |
return this.userService.currentUser$.pipe( | |
first(), | |
switchMap(user => { | |
return from(user?.getIdToken() || ''); | |
}), | |
exhaustMap(token => { | |
const tenantId = auth().tenantId; | |
let headers = tenantId | |
? req.headers.append('Schoolid', tenantId) | |
: req.headers; | |
headers = headers.append('Authorization', token); | |
const authReq = req.clone({ headers }); | |
return next.handle(authReq); | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment