Skip to content

Commit

Permalink
feat: use the post api to get nih auth url
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong committed Nov 18, 2024
1 parent 9a411b1 commit fd74078
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
11 changes: 7 additions & 4 deletions src/components/ERACommons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default function ERACommons(props) {
// TODO Testing code to replace old functionality with:
try {
const ecmResponse = AuthenticateNIH.getECMeRACommonsStatus();
console.log(ecmResponse);
console.log('ecmResponse', ecmResponse);
} catch (err) {
console.log(err);
}
Expand All @@ -76,9 +76,12 @@ export default function ERACommons(props) {
const redirectToNihLogin = async () => {
// TODO Testing code to replace old functionality with:
try {
const redirectURI = window.location.origin;
let redirectURI = window.location.origin;
if (!origin.endsWith('/')) {
redirectURI = redirectURI + '/';
}
const authUrl = await AuthenticateNIH.getECMeRACommonsAuthUrl(redirectURI, destination);
console.log(authUrl);
console.log('authUrl', authUrl);
} catch (err) {
console.log(err);
}
Expand All @@ -101,7 +104,7 @@ export default function ERACommons(props) {
const eraAuthState = extractEraAuthenticationState(response.researcherProperties);
setAuthorized(eraAuthState.isAuthorized);
setExpirationCount(eraAuthState.expirationCount);
setEraCommonsId(researcherProfile.eraCommonsId);
setEraCommonsId(undefined);
onNihStatusUpdate(eraAuthState.nihValid);
setSearch('');
} else {
Expand Down
36 changes: 8 additions & 28 deletions src/libs/ajax/AuthenticateNIH.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Config } from '../config';
import {getApiUrl, fetchOk, getECMUrl, reportError} from '../ajax';
import axios from 'axios';
import {mergeAll, getOr, isNil} from 'lodash/fp';
import {get, isNil, merge} from 'lodash';


axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
// Default to a 502 when we can't get a real response object.
const status = getOr(502)('response.status')(error);
const reportUrl = getOr(null)('response.config.url')(error);
const status = get(error, 'response.status', 502);
const reportUrl = get(error, 'response.config.url', null);
if (!isNil(reportUrl) && status >= 500) {
reportError(reportUrl, status);
}
Expand All @@ -19,13 +19,13 @@ axios.interceptors.response.use(function (response) {
export const AuthenticateNIH = {
saveNihUsr: async (decodedData) => {
const url = `${await getApiUrl()}/api/nih`;
const res = await fetchOk(url, mergeAll([Config.authOpts(), Config.jsonBody(decodedData), { method: 'POST' }]));
const res = await fetchOk(url, merge([Config.authOpts(), Config.jsonBody(decodedData), { method: 'POST' }]));
return await res.json();
},

deleteAccountLinkage: async () => {
const url = `${await getApiUrl()}/api/nih`;
const res = await fetchOk(url, mergeAll([Config.authOpts(), { method: 'DELETE' }]));
const res = await fetchOk(url, merge([Config.authOpts(), { method: 'DELETE' }]));
return await res;
},

Expand All @@ -39,33 +39,13 @@ export const AuthenticateNIH = {
},

getECMeRACommonsAuthUrl: async (redirectUri, redirectTo) => {
const url = `${await getECMUrl()}/api/oauth/v1/era-commons/authorization-url`;
const parameterBlock = {
params: {
scopes: ['openid', 'email', 'profile'],
redirectUri: redirectUri,
redirectTo: redirectTo
},
paramsSerializer: {
indexes: null,
}
};
const config = Object.assign({}, Config.authOpts(), parameterBlock);
const res = await axios.get(url, config);
const url = `${await getECMUrl()}/api/oauth/v1/era-commons/authorization-url?redirectUri=${redirectUri}`;
console.log('url', url);
const res = await axios.post(url, {redirectTo: redirectTo}, Config.authOpts());
if (res.status === 200) {
return res.data;
}
return undefined;
},

postECMeRACommonsOauthcode: async (state, oauthcode) => {
const url = `${await getECMUrl()}/api/oauth/v1/era-commons/oauthcode`;
const data = {
state: state,
oauthcode: oauthcode
};
const res = await axios.post(url, data, Config.authOpts());
return res.data;
}

};

0 comments on commit fd74078

Please sign in to comment.