74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
|
import { Injectable } from '@angular/core';
|
||
|
import { BehaviorSubject, Observable } from 'rxjs';
|
||
|
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
|
||
|
|
||
|
import { ConfigData } from './configdata';
|
||
|
|
||
|
var Buffer = require('buffer/').Buffer;
|
||
|
|
||
|
@Injectable({
|
||
|
providedIn: 'root'
|
||
|
})
|
||
|
export class XeroService {
|
||
|
beUrl = ConfigData.Be_URL;
|
||
|
clientId: string = '';
|
||
|
clientSecret: string = '';
|
||
|
xeroToken: any = {
|
||
|
accessToken: '',
|
||
|
refreshToken: '',
|
||
|
expiresIn: 0,
|
||
|
scope: '',
|
||
|
tokenType: ''
|
||
|
};
|
||
|
private _clientIdUpdated: BehaviorSubject<string> = new BehaviorSubject(this.clientId);
|
||
|
private _clientSecretUpdated: BehaviorSubject<string> = new BehaviorSubject(this.clientSecret);
|
||
|
private _tokenUpdated: BehaviorSubject<any> = new BehaviorSubject(this.xeroToken);
|
||
|
public readonly clientIdUpdate: Observable<string> = this._clientIdUpdated.asObservable();
|
||
|
public readonly clientSecretUpdate: Observable<string> = this._clientSecretUpdated.asObservable();
|
||
|
public readonly tokenUpdate: Observable<any> = this._tokenUpdated.asObservable();
|
||
|
private reqHeaders: HttpHeaders;
|
||
|
|
||
|
constructor(
|
||
|
private http: HttpClient
|
||
|
) {
|
||
|
var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64');
|
||
|
this.reqHeaders = new HttpHeaders().set('Authorization', auth);
|
||
|
this.getXeroConfig();
|
||
|
}
|
||
|
|
||
|
getXeroConfig(){
|
||
|
let obs = this.http.get<{message: string, xeroConfig: any}>(this.beUrl+'api/xero', { headers:this.reqHeaders, observe: 'response'});
|
||
|
|
||
|
obs.subscribe(xeroDataResponse => {
|
||
|
if (xeroDataResponse.status == 200) {
|
||
|
this.clientId = xeroDataResponse.body!.xeroConfig.clientId;
|
||
|
this.clientSecret = xeroDataResponse.body!.xeroConfig.clientSecret;
|
||
|
this._clientIdUpdated.next(Object.assign({}, this).clientId);
|
||
|
this._clientSecretUpdated.next(Object.assign({}, this).clientSecret);
|
||
|
} else {
|
||
|
console.log('No config in DB!');
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return obs;
|
||
|
}
|
||
|
|
||
|
getXeroAccessToken(code: string){
|
||
|
this.getXeroConfig().subscribe(xeroDataResponse => {
|
||
|
console.log('XAT: '+this.clientId);
|
||
|
var xeroAuth = 'Basic ' + Buffer.from(`${this.clientId}:${this.clientSecret}`).toString('base64');
|
||
|
var xeroHeaders = new HttpHeaders().set('Authorization', xeroAuth).append('Content-Type', 'application/x-www-form-urlencoded');
|
||
|
let obs = this.http.post('https://identity.xero.com/connect/token', `grant_type=authorization_code&code=${code}&redirect_uri=http://localhost:4200/test` , {headers: xeroHeaders, observe: 'response'})
|
||
|
obs.subscribe(tokenData => {
|
||
|
if (tokenData.status == 200) {
|
||
|
console.log(tokenData.body!);
|
||
|
this.xeroToken = tokenData.body!;
|
||
|
this._tokenUpdated.next(Object.assign({}, this).xeroToken);
|
||
|
} else {
|
||
|
console.log('Error: '+tokenData.status);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|