diff --git a/templates/app/react-native/src/api/AccountAPI.js b/templates/app/react-native/src/api/AccountAPI.js index af303b626a..76a4b7f914 100644 --- a/templates/app/react-native/src/api/AccountAPI.js +++ b/templates/app/react-native/src/api/AccountAPI.js @@ -3,15 +3,34 @@ import { getEnvVars } from '../../Environment'; const { oAuthConfig } = getEnvVars(); +getLoginData = (username, password) => { + + const formData = { + grant_type: 'password', + scope: oAuthConfig.scope, + username: username, + password: password, + client_id: oAuthConfig.clientId + }; + + if (oAuthConfig.clientSecret) + formData['client_secret'] = oAuthConfig.clientSecret; + + return Object.entries(formData) + .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) + .join('&'); +} + export const login = ({ username, password }) => api({ method: 'POST', url: '/connect/token', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - data: `grant_type=password&scope=${oAuthConfig.scope}&username=${username}&password=${password}&client_id=${oAuthConfig.clientId}&client_secret=${oAuthConfig.clientSecret}`, - baseURL: oAuthConfig.issuer, + data: getLoginData(username, password), + baseURL: oAuthConfig.issuer }).then(({ data }) => data); + export const Logout = () => api({ method: 'GET',