diff --git a/src/adapters/authAdapter.ts b/src/adapters/authAdapter.ts deleted file mode 100644 index fa6cd33..0000000 --- a/src/adapters/authAdapter.ts +++ /dev/null @@ -1,64 +0,0 @@ -import BaseAdapter from './baseAdapter.'; - -type LoginAuthType = { - email: string; - password: string; -}; - -/* eslint-disable camelcase */ -export const commonParams = { - client_id: process.env.REACT_APP_API_CLIENT_ID, - client_secret: process.env.REACT_APP_API_CLIENT_SECRET, -}; -/* eslint-enable camelcase */ -class AuthAdapter extends BaseAdapter { - static loginWithEmailPassword(authParams: LoginAuthType) { - /* eslint-disable camelcase */ - const requestParams = { - ...commonParams, - ...authParams, - grant_type: 'password', - }; - /* eslint-enable camelcase */ - - return this.prototype.postRequest('oauth/token', { data: requestParams }); - } - - static loginWithRefreshToken(refreshToken: string) { - /* eslint-disable camelcase */ - const requestParams = { - ...commonParams, - refresh_token: refreshToken, - grant_type: 'refresh_token', - }; - /* eslint-enable camelcase */ - - return this.prototype.postRequest('oauth/token', { data: requestParams }); - } - - static logout(accessToken: string) { - /* eslint-disable camelcase */ - const requestParams = { - ...commonParams, - token: accessToken, - }; - /* eslint-enable camelcase */ - - return this.prototype.postRequest('oauth/revoke', { data: requestParams }); - } - - static resetPassword(email: string) { - const requestParams = { - ...commonParams, - user: { email: email }, - }; - - return this.prototype.postRequest('passwords', { data: requestParams }); - } - - static getUser() { - return this.prototype.getRequest('me', {}); - } -} - -export default AuthAdapter; diff --git a/src/adapters/authAdapter.test.ts b/src/adapters/surveyAdapter.test.ts similarity index 87% rename from src/adapters/authAdapter.test.ts rename to src/adapters/surveyAdapter.test.ts index 728ce1d..66dab9d 100644 --- a/src/adapters/authAdapter.test.ts +++ b/src/adapters/surveyAdapter.test.ts @@ -1,6 +1,6 @@ import nock from 'nock'; -import AuthAdapter, { commonParams } from './authAdapter'; +import SurveyAdapter, { commonParams } from './surveyAdapter'; /* eslint-disable camelcase */ const mockLoginCredentials = { @@ -20,7 +20,7 @@ const commonUserProfileResponse = { }, }; -describe('AuthAdapter', () => { +describe('SurveyAdapter', () => { afterAll(() => { nock.cleanAll(); nock.restore(); @@ -41,7 +41,7 @@ describe('AuthAdapter', () => { .reply(200); expect(scope.isDone()).toBe(false); - await AuthAdapter.loginWithEmailPassword({ ...mockLoginCredentials }); + await SurveyAdapter.loginWithEmailPassword({ ...mockLoginCredentials }); expect(scope.isDone()).toBe(true); }); }); @@ -61,7 +61,7 @@ describe('AuthAdapter', () => { .reply(200); expect(scope.isDone()).toBe(false); - await AuthAdapter.loginWithRefreshToken('refresh_token'); + await SurveyAdapter.loginWithRefreshToken('refresh_token'); expect(scope.isDone()).toBe(true); }); }); @@ -77,7 +77,7 @@ describe('AuthAdapter', () => { .reply(200, { ...commonUserProfileResponse }); expect(scope.isDone()).toBe(false); - expect(await AuthAdapter.getUser()).toEqual({ ...commonUserProfileResponse }); + expect(await SurveyAdapter.getUser()).toEqual({ ...commonUserProfileResponse }); expect(scope.isDone()).toBe(true); }); }); diff --git a/src/adapters/surveyAdapter.ts b/src/adapters/surveyAdapter.ts index 654ee00..d98001d 100644 --- a/src/adapters/surveyAdapter.ts +++ b/src/adapters/surveyAdapter.ts @@ -1,20 +1,67 @@ -import { getItem } from 'helpers/localStorage'; - import BaseAdapter from './baseAdapter.'; +type LoginAuthType = { + email: string; + password: string; +}; + /* eslint-disable camelcase */ export const commonParams = { - access_token: getItem('UserProfile')?.auth?.access_token, + client_id: process.env.REACT_APP_API_CLIENT_ID, + client_secret: process.env.REACT_APP_API_CLIENT_SECRET, }; /* eslint-enable camelcase */ - class SurveyAdapter extends BaseAdapter { - static list() { + static loginWithEmailPassword(authParams: LoginAuthType) { + /* eslint-disable camelcase */ + const requestParams = { + ...commonParams, + ...authParams, + grant_type: 'password', + }; + /* eslint-enable camelcase */ + + return this.prototype.postRequest('oauth/token', { data: requestParams }); + } + + static loginWithRefreshToken(refreshToken: string) { + /* eslint-disable camelcase */ + const requestParams = { + ...commonParams, + refresh_token: refreshToken, + grant_type: 'refresh_token', + }; + /* eslint-enable camelcase */ + + return this.prototype.postRequest('oauth/token', { data: requestParams }); + } + + static logout(accessToken: string) { + /* eslint-disable camelcase */ + const requestParams = { + ...commonParams, + token: accessToken, + }; + /* eslint-enable camelcase */ + + return this.prototype.postRequest('oauth/revoke', { data: requestParams }); + } + + static resetPassword(email: string) { const requestParams = { ...commonParams, + user: { email: email }, }; - return this.prototype.getRequest('surveys', { data: requestParams }); + return this.prototype.postRequest('passwords', { data: requestParams }); + } + + static getUser() { + return this.prototype.getRequest('me', {}); + } + + static list() { + return this.prototype.getRequest('surveys', {}); } } diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index d14ff81..8f3df94 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -1,6 +1,6 @@ import { useNavigate } from 'react-router-dom'; -import AuthAdapter from 'adapters/authAdapter'; +import SurveyAdapter from 'adapters/surveyAdapter'; import { getItem, clearItem } from 'helpers/localStorage'; import { User } from 'types/User'; @@ -18,7 +18,7 @@ function Sidebar({ user }: SidebarProps) { e.stopPropagation(); const accessToken = getItem('UserProfile')?.auth.access_token; - await AuthAdapter.logout(accessToken); + await SurveyAdapter.logout(accessToken); clearItem('UserProfile'); navigate(LOGIN_URL); }; diff --git a/src/lib/requestManager.ts b/src/lib/requestManager.ts index 3f8de0e..10552fd 100644 --- a/src/lib/requestManager.ts +++ b/src/lib/requestManager.ts @@ -1,6 +1,6 @@ import axios, { Method as HTTPMethod, ResponseType, AxiosRequestConfig, AxiosResponse } from 'axios'; -import AuthAdapter from 'adapters/authAdapter'; +import SurveyAdapter from 'adapters/surveyAdapter'; import { setItem, getItem, clearItem } from 'helpers/localStorage'; import { LOGIN_URL } from '../constants'; @@ -59,7 +59,7 @@ const requestManager = ( if (userProfile?.auth?.refresh_token) { try { - const response = await AuthAdapter.loginWithRefreshToken(userProfile.auth.refresh_token); + const response = await SurveyAdapter.loginWithRefreshToken(userProfile.auth.refresh_token); const { attributes: authInfo } = await response.data; diff --git a/src/screens/Login/index.test.tsx b/src/screens/Login/index.test.tsx index 49113bb..17cae5c 100644 --- a/src/screens/Login/index.test.tsx +++ b/src/screens/Login/index.test.tsx @@ -7,7 +7,7 @@ import userEvent from '@testing-library/user-event'; import nock from 'nock'; import LoginScreen from '.'; -import AuthAdapter from '../../adapters/authAdapter'; +import SurveyAdapter from '../../adapters/surveyAdapter'; /* eslint-disable camelcase */ const commonLoginParams = { @@ -77,7 +77,7 @@ describe('LoginScreen', () => { }); test('given an empty email and password in the login form, displays both errors', async () => { - const mockLogin = jest.spyOn(AuthAdapter, 'loginWithEmailPassword'); + const mockLogin = jest.spyOn(SurveyAdapter, 'loginWithEmailPassword'); render(, { wrapper: BrowserRouter }); @@ -140,7 +140,7 @@ describe('LoginScreen', () => { }); test('given INCORRECT credentials, displays the error from the API response', async () => { - const mockLogin = jest.spyOn(AuthAdapter, 'loginWithEmailPassword'); + const mockLogin = jest.spyOn(SurveyAdapter, 'loginWithEmailPassword'); render(, { wrapper: BrowserRouter }); diff --git a/src/screens/Login/index.tsx b/src/screens/Login/index.tsx index 1cbd43e..a559e9f 100644 --- a/src/screens/Login/index.tsx +++ b/src/screens/Login/index.tsx @@ -4,7 +4,7 @@ import { useNavigate, Link } from 'react-router-dom'; import { AxiosError } from 'axios'; -import AuthAdapter from 'adapters/authAdapter'; +import SurveyAdapter from 'adapters/surveyAdapter'; import logo from 'assets/images/logo.svg'; import Button from 'components/Button'; import Input from 'components/Input'; @@ -32,13 +32,13 @@ function LoginScreen() { const performLogin = async () => { try { - const loginResponse = await AuthAdapter.loginWithEmailPassword({ email: email, password: password }); + const loginResponse = await SurveyAdapter.loginWithEmailPassword({ email: email, password: password }); const { attributes: authInfo } = await loginResponse.data; setItem('UserProfile', { auth: authInfo }); - const getUserResponse = await AuthAdapter.getUser(); + const getUserResponse = await SurveyAdapter.getUser(); const { attributes: userInfo } = await getUserResponse.data; diff --git a/src/screens/ResetPassword/index.tsx b/src/screens/ResetPassword/index.tsx index c6deb33..b0a6232 100644 --- a/src/screens/ResetPassword/index.tsx +++ b/src/screens/ResetPassword/index.tsx @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'; import { AxiosError } from 'axios'; -import AuthAdapter from 'adapters/authAdapter'; +import SurveyAdapter from 'adapters/surveyAdapter'; import bellNotification from 'assets/images/bell-notification.png'; import Button from 'components/Button'; import Input from 'components/Input'; @@ -23,7 +23,7 @@ function ResetPasswordScreen() { const performPasswordReset = async () => { try { - await AuthAdapter.resetPassword(email); + await SurveyAdapter.resetPassword(email); } catch (error) { let errorMessage = t('login.generic-server-error');