Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios, {AxiosInstance} from 'axios';
  2.  
  3. const apiVersionPrefix = 'v1';
  4. const oauthGrantType = 'password';
  5. const oauthClientId = '1';
  6. const oauthClientSecret = 'rcvMGaXE080elgdfolWsp8QNOPwG5FYiAllzFYXH';
  7. const oauthScope = '';
  8.  
  9. const API: AxiosInstance = axios.create({
  10.     baseURL: `http://api.shop.local/` + apiVersionPrefix
  11. });
  12.  
  13. export interface IApiOauthTokenData {
  14.     email: string,
  15.     password: string,
  16. }
  17.  
  18. export interface IApiOauthTokenRequestData {
  19.     grant_type: string,
  20.     client_id: string,
  21.     client_secret: string,
  22.     scope: string,
  23.     username: string,
  24.     password: string,
  25. }
  26.  
  27. export interface IApiOauthTokenResponseData {
  28.     token_type: string,
  29.     expires_in: number,
  30.     access_token: string,
  31.     refresh_token: string,
  32. }
  33.  
  34. export async function CallApiOauthToken(data: IApiOauthTokenData): Promise<IApiOauthTokenResponseData | {}> {
  35.     const requestData: IApiOauthTokenRequestData = {
  36.         grant_type: oauthGrantType,
  37.         client_id: oauthClientId,
  38.         client_secret: oauthClientSecret,
  39.         scope: oauthScope,
  40.         username: data.email,
  41.         password: data.password,
  42.     };
  43.  
  44.     return await API.post(`/oauth/token`, requestData).then(result => {
  45.         const responseData: IApiOauthTokenResponseData = {
  46.             token_type: result.data.token_type,
  47.             expires_in: result.data.expires_in,
  48.             access_token: result.data.access_token,
  49.             refresh_token: result.data.refresh_token,
  50.         };
  51.  
  52.         return responseData;
  53.     }).catch(error => {
  54.         console.log(error); // todo error show
  55.  
  56.         return {};
  57.     });
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement