Browse Source

refactor building formData params

pull/16468/head
Barış Can Yılmaz 3 years ago
parent
commit
edfae5bf6b
  1. 18
      templates/app/react-native/src/api/AccountAPI.js

18
templates/app/react-native/src/api/AccountAPI.js

@ -2,10 +2,11 @@ import api from './API';
import { getEnvVars } from '../../Environment'; import { getEnvVars } from '../../Environment';
const { oAuthConfig } = getEnvVars(); const { oAuthConfig } = getEnvVars();
getLoginData = (username, password) => { getLoginData = (username, password) => {
const formData ={ const formData = {
grant_type:'password', grant_type: 'password',
scope: oAuthConfig.scope, scope: oAuthConfig.scope,
username: username, username: username,
password: password, password: password,
@ -13,11 +14,11 @@ getLoginData = (username, password) => {
}; };
if (oAuthConfig.clientSecret) if (oAuthConfig.clientSecret)
formData['client_secret']=oAuthConfig.clientSecret; formData['client_secret'] = oAuthConfig.clientSecret;
return Array.from(Object.entries(formData)) return Object.entries(formData)
.reduce((prev, [key, value]) => prev+=`&${key}=${value}`, '') .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.slice(1); .join('&');
} }
export const login = ({ username, password }) => export const login = ({ username, password }) =>
@ -25,10 +26,11 @@ export const login = ({ username, password }) =>
method: 'POST', method: 'POST',
url: '/connect/token', url: '/connect/token',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: getLoginData(username,password), data: getLoginData(username, password),
baseURL: oAuthConfig.issuer, baseURL: oAuthConfig.issuer
}).then(({ data }) => data); }).then(({ data }) => data);
export const Logout = () => export const Logout = () =>
api({ api({
method: 'GET', method: 'GET',

Loading…
Cancel
Save