Skip to content

Instantly share code, notes, and snippets.

@brettwold
Last active August 11, 2017 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettwold/711e23cf26c18cbb0054aa3a5985223f to your computer and use it in GitHub Desktop.
Save brettwold/711e23cf26c18cbb0054aa3a5985223f to your computer and use it in GitHub Desktop.
Using HttpAuthInterceptor example Ionic2
import { Response, RequestOptions, ConnectionBackend } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Storage } from '@ionic/storage';
import { HttpAuthInterceptor, InterceptorConfig } from './http-auth-interceptor';
export class HttpAuth extends HttpAuthInterceptor {
// In production code do not put your API keys here make sure they are obtained some other way.
// perhaps a env variables.
const API_ACCESS_KEY = '...';
const API_ACCESS_SECRET = '...';
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private storage: Storage) {
super(backend, defaultOptions, new InterceptorConfig({ noTokenError: true }));
}
protected getToken(): Promise<string> {
return this.storage.get('id_token').then((token) => {
if(!token) {
return null;
} else {
return token;
}
});
}
protected saveToken(token: string): Promise<string> {
return this.storage.set('id_token', token);
}
protected refreshToken(): Observable<Response> {
return super.post('http://www.data.com/api/authenticate', {
access_key_id: API_ACCESS_KEY,
access_key_secret: API_ACCESS_SECRET
}, null, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment