Created
September 5, 2018 19:22
-
-
Save whisher/d129487ed476035fd44bf1369b5f54b2 to your computer and use it in GitHub Desktop.
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
| login(credentials): Observable<AuthenticationToken> { | |
| const data = 'username=' + encodeURIComponent(credentials.username) + '&password=' + | |
| encodeURIComponent(credentials.password) + '&grant_type=password&scope=read%20write&' + | |
| 'client_secret=' + this.clientSecret + '&client_id=' + this.clientId; | |
| const headers = new HttpHeaders() | |
| .set('Content-Type', 'application/x-www-form-urlencoded') | |
| .set('Accept', 'application/json') | |
| .set('Authorization', 'Basic ' + this.base64('myapp' + ':' + 'mySecretOAuthSecret')); | |
| return this.http.post(this.urlLogin, data, { | |
| headers | |
| }) | |
| .pipe( | |
| map(this.authSuccess.bind(this)), | |
| catchError((error: HttpErrorResponse) => { | |
| const errorMessage = `Server returned code: ${error.status}, error message is: ${error.message}`; | |
| return ErrorObservable.create(errorMessage); | |
| }) | |
| ); | |
| } | |
| private authSuccess(data): AuthenticationToken { | |
| const expiredAt = new Date(); | |
| expiredAt.setSeconds(expiredAt.getSeconds() + data.expires_in); | |
| const authenticationToken: AuthenticationToken = { | |
| token: data.access_token, | |
| expiresIn: data.expires_in, | |
| expiresAt: expiredAt.getTime() | |
| }; | |
| return authenticationToken | |
| } | |
| private base64(str): string { | |
| return btoa(str); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment