Last active
August 11, 2017 13:43
-
-
Save brettwold/711e23cf26c18cbb0054aa3a5985223f to your computer and use it in GitHub Desktop.
Using HttpAuthInterceptor example Ionic2
This file contains hidden or 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
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