diff --git a/client/package-lock.json b/client/package-lock.json index 516961ab3f..6a147b7b3a 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -7392,15 +7392,6 @@ "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz", "integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ==" }, - "bindings": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", - "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", - "optional": true, - "requires": { - "file-uri-to-path": "1.0.0" - } - }, "bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", @@ -11337,12 +11328,6 @@ "token-types": "^2.0.0" } }, - "file-uri-to-path": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", - "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", - "optional": true - }, "filesize": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/filesize/-/filesize-6.1.0.tgz", @@ -16713,12 +16698,6 @@ "resolved": "https://registry.npmjs.org/name-all-modules-plugin/-/name-all-modules-plugin-1.0.1.tgz", "integrity": "sha1-Cr+2rYNXGLn7Te8GdOBmV6lUN1w=" }, - "nan": { - "version": "2.14.2", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz", - "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", - "optional": true - }, "nanoid": { "version": "3.1.23", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz", @@ -22746,11 +22725,7 @@ "version": "1.2.13", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", - "optional": true, - "requires": { - "bindings": "^1.5.0", - "nan": "^2.12.1" - } + "optional": true }, "glob-parent": { "version": "3.1.0", diff --git a/client/package.json b/client/package.json index 02be9876e4..3cae697d97 100644 --- a/client/package.json +++ b/client/package.json @@ -55,6 +55,7 @@ "@reach/router": "1.3.4", "algoliasearch": "4.9.3", "assert": "2.0.0", + "axios": "0.21.1", "babel-plugin-preval": "5.0.0", "babel-plugin-prismjs": "2.0.1", "bezier-easing": "2.1.0", diff --git a/client/src/redux/accept-terms-saga.js b/client/src/redux/accept-terms-saga.js index b5027e2e3f..af09e9c904 100644 --- a/client/src/redux/accept-terms-saga.js +++ b/client/src/redux/accept-terms-saga.js @@ -8,7 +8,7 @@ import { putUserAcceptsTerms } from '../utils/ajax'; function* acceptTermsSaga({ payload: quincyEmails }) { try { - const response = yield call(putUserAcceptsTerms, quincyEmails); + const { data: response } = yield call(putUserAcceptsTerms, quincyEmails); yield put(acceptTermsComplete(quincyEmails)); yield put(createFlashMessage(response)); diff --git a/client/src/redux/fetch-user-saga.js b/client/src/redux/fetch-user-saga.js index e2147413b4..abed96dc3f 100644 --- a/client/src/redux/fetch-user-saga.js +++ b/client/src/redux/fetch-user-saga.js @@ -16,9 +16,7 @@ function* fetchSessionUser() { } try { const { - user = {}, - result = '', - sessionMeta = {} + data: { user = {}, result = '', sessionMeta = {} } } = yield call(getSessionUser); const appUser = user[result] || {}; yield put( @@ -32,11 +30,9 @@ function* fetchSessionUser() { function* fetchOtherUser({ payload: maybeUser = '' }) { try { const maybeUserLC = maybeUser.toLowerCase(); + const { data } = yield call(getUserProfile, maybeUserLC); - const { entities: { user = {} } = {}, result = '' } = yield call( - getUserProfile, - maybeUserLC - ); + const { entities: { user = {} } = {}, result = '' } = data; const otherUser = user[result] || {}; yield put( fetchProfileForUserComplete({ user: otherUser, username: result }) diff --git a/client/src/redux/report-user-saga.js b/client/src/redux/report-user-saga.js index 75243fba68..6df1cbcca1 100644 --- a/client/src/redux/report-user-saga.js +++ b/client/src/redux/report-user-saga.js @@ -8,7 +8,7 @@ import { postReportUser } from '../utils/ajax'; function* reportUserSaga({ payload }) { try { - const response = yield call(postReportUser, payload); + const { data: response } = yield call(postReportUser, payload); yield put(reportUserComplete()); yield put(createFlashMessage(response)); diff --git a/client/src/redux/settings/settings-sagas.js b/client/src/redux/settings/settings-sagas.js index 1472f4ec1c..407d2673db 100644 --- a/client/src/redux/settings/settings-sagas.js +++ b/client/src/redux/settings/settings-sagas.js @@ -26,7 +26,7 @@ import { createFlashMessage } from '../../components/Flash/redux'; function* submitNewAboutSaga({ payload }) { try { - const response = yield call(putUpdateMyAbout, payload); + const { data: response } = yield call(putUpdateMyAbout, payload); yield put(submitNewAboutComplete({ ...response, payload })); yield put(createFlashMessage(response)); } catch (e) { @@ -36,7 +36,7 @@ function* submitNewAboutSaga({ payload }) { function* submitNewUsernameSaga({ payload: username }) { try { - const response = yield call(putUpdateMyUsername, username); + const { data: response } = yield call(putUpdateMyUsername, username); yield put(submitNewUsernameComplete({ ...response, username })); yield put(createFlashMessage(response)); } catch (e) { @@ -46,7 +46,7 @@ function* submitNewUsernameSaga({ payload: username }) { function* submitProfileUISaga({ payload }) { try { - const response = yield call(putUpdateMyProfileUI, payload); + const { data: response } = yield call(putUpdateMyProfileUI, payload); yield put(submitProfileUIComplete({ ...response, payload })); yield put(createFlashMessage(response)); } catch (e) { @@ -56,7 +56,7 @@ function* submitProfileUISaga({ payload }) { function* updateUserFlagSaga({ payload: update }) { try { - const response = yield call(putUpdateUserFlag, update); + const { data: response } = yield call(putUpdateUserFlag, update); yield put(updateUserFlagComplete({ ...response, payload: update })); yield put(createFlashMessage(response)); } catch (e) { @@ -67,7 +67,9 @@ function* updateUserFlagSaga({ payload: update }) { function* validateUsernameSaga({ payload }) { try { yield delay(500); - const { exists } = yield call(getUsernameExists, payload); + const { + data: { exists } + } = yield call(getUsernameExists, payload); yield put(validateUsernameComplete(exists)); } catch (e) { yield put(validateUsernameError(e)); @@ -76,10 +78,9 @@ function* validateUsernameSaga({ payload }) { function* verifyCertificationSaga({ payload }) { try { - const { response, isCertMap, completedChallenges } = yield call( - putVerifyCert, - payload - ); + const { + data: { response, isCertMap, completedChallenges } + } = yield call(putVerifyCert, payload); yield put( verifyCertComplete({ ...response, diff --git a/client/src/redux/settings/update-email-saga.js b/client/src/redux/settings/update-email-saga.js index d848e46761..2befb67172 100644 --- a/client/src/redux/settings/update-email-saga.js +++ b/client/src/redux/settings/update-email-saga.js @@ -13,7 +13,7 @@ function* updateMyEmailSaga({ payload: email = '' }) { return; } try { - const response = yield call(putUserUpdateEmail, email); + const { data: response } = yield call(putUserUpdateEmail, email); yield put( updateMyEmailComplete({ ...response, diff --git a/client/src/redux/show-cert-saga.js b/client/src/redux/show-cert-saga.js index e352fab778..4358ce5c7a 100644 --- a/client/src/redux/show-cert-saga.js +++ b/client/src/redux/show-cert-saga.js @@ -7,7 +7,7 @@ import { showCertComplete, showCertError } from '.'; function* getShowCertSaga({ payload: { username, certSlug } }) { try { - const response = yield call(getShowCert, username, certSlug); + const { data: response } = yield call(getShowCert, username, certSlug); const { messages } = response; if (messages && messages.length) { for (let i = 0; i < messages.length; i++) { diff --git a/client/src/utils/ajax.js b/client/src/utils/ajax.js index 0845ba536a..79c87517bd 100644 --- a/client/src/utils/ajax.js +++ b/client/src/utils/ajax.js @@ -1,52 +1,41 @@ import envData from '../../../config/env.json'; +import axios from 'axios'; import Tokens from 'csrf'; import cookies from 'browser-cookies'; -const { apiLocation, environment } = envData; +const { apiLocation } = envData; const base = apiLocation; const tokens = new Tokens(); -// TODO: test on staging. Do we need 'include' everywhere? -const defaultOptions = { - credentials: environment === 'development' ? 'include' : 'same-site' -}; +axios.defaults.withCredentials = true; // _csrf is passed to the client as a cookie. Tokens are sent back to the server // via headers: -function getCSRFToken() { +function setCSRFTokens() { const _csrf = typeof window !== 'undefined' && cookies.get('_csrf'); - if (!_csrf) { - return ''; - } else { - return tokens.create(_csrf); - } + if (!_csrf) return; + axios.defaults.headers.post['CSRF-Token'] = tokens.create(_csrf); + axios.defaults.headers.put['CSRF-Token'] = tokens.create(_csrf); } function get(path) { - return fetch(`${base}${path}`, defaultOptions).then(res => res.json()); + return axios.get(`${base}${path}`); } export function post(path, body) { - return request('POST', path, body); + setCSRFTokens(); + return axios.post(`${base}${path}`, body); } function put(path, body) { - return request('PUT', path, body); + setCSRFTokens(); + return axios.put(`${base}${path}`, body); } -function request(method, path, body) { - const options = { - ...defaultOptions, - method, - headers: { - 'CSRF-Token': getCSRFToken(), - 'Content-Type': 'application/json' - }, - body: JSON.stringify(body) - }; - return fetch(`${base}${path}`, options).then(res => res.json()); -} +// function del(path) { +// return axios.delete(`${base}${path}`); +// } /** GET **/ @@ -117,3 +106,5 @@ export function putUserUpdateEmail(email) { export function putVerifyCert(certSlug) { return put('/certificate/verify', { certSlug }); } + +/** DELETE **/