Skip to content

Instantly share code, notes, and snippets.

View danielcrisp's full-sized avatar

Daniel Crisp danielcrisp

View GitHub Profile
<script>
parent.alert("Hello");
</script>
@danielcrisp
danielcrisp / auth.service.ts
Last active August 8, 2018 14:01
AuthService - Async HTTP Interceptors with Angular 4
import { Injectable } from '@angular/core';
import { UserManager, User } from 'oidc-client';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
@Injectable()
export class AuthService {
private userManager: UserManager;
@danielcrisp
danielcrisp / token.interceptor.ts
Last active April 5, 2021 20:15
TokenInterceptor - Async HTTP Interceptors with Angular 4
import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/mergeMap';
import { AuthService } from './auth.service';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
@danielcrisp
danielcrisp / token.interceptor.ts
Created July 25, 2017 13:39
TokenInterceptor - Async HTTP Interceptors with Angular 4
intercept (request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return this.auth.getUser()
.mergeMap((user: User) => {
if (user) {
// clone and modify the request
request = request.clone({
setHeaders: {
Authorization: `Bearer ${user.access_token}`
}
@danielcrisp
danielcrisp / token.interceptor.ts
Created July 25, 2017 13:38
TokenInterceptor - Async HTTP Interceptors with Angular 4
intercept (request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return this.auth.getUser()
.mergeMap((user: User) => {
// ... modify the request here ...
return next.handle(request);
});
}
@danielcrisp
danielcrisp / token.interceptor.ts
Created July 25, 2017 13:37
TokenInterceptor imports - Async HTTP Interceptors with Angular 4
import 'rxjs/add/operator/mergeMap';
@danielcrisp
danielcrisp / auth.service.ts
Created July 25, 2017 13:36
AuthService.getUser - Async HTTP Interceptors with Angular 4
getUser (): Observable<User> {
return Observable.fromPromise(this.userManager.getUser());
}
@danielcrisp
danielcrisp / auth.service.ts
Created July 25, 2017 13:35
AuthService imports - Async HTTP Interceptors with Angular 4
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
@danielcrisp
danielcrisp / token.interceptor.ts
Created July 25, 2017 13:31
TokenInterceptor.intercept - Async HTTP Interceptors with Angular 4
// THIS DOESN'T WORK!
intercept (request, next) {
this.auth.getUser().then(user => {
// ... modify the request here ...
next.handle(request);
});
}
@danielcrisp
danielcrisp / token.interceptor.ts
Created July 25, 2017 13:30
TokenInterceptor.intercept - Async HTTP Interceptors with Angular 4
intercept (request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request);
}