Skip to content

Commit

Permalink
[#26] Change AuthAdapter and SurveyAdapter to just SurveyAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Mar 13, 2023
1 parent 3fcc5ff commit d348717
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 87 deletions.
64 changes: 0 additions & 64 deletions src/adapters/authAdapter.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nock from 'nock';

import AuthAdapter, { commonParams } from './authAdapter';
import SurveyAdapter, { commonParams } from './surveyAdapter';

/* eslint-disable camelcase */
const mockLoginCredentials = {
Expand All @@ -20,7 +20,7 @@ const commonUserProfileResponse = {
},
};

describe('AuthAdapter', () => {
describe('SurveyAdapter', () => {
afterAll(() => {
nock.cleanAll();
nock.restore();
Expand All @@ -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);
});
});
Expand All @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down
59 changes: 53 additions & 6 deletions src/adapters/surveyAdapter.ts
Original file line number Diff line number Diff line change
@@ -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', {});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/requestManager.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/screens/Login/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(<LoginScreen />, { wrapper: BrowserRouter });

Expand Down Expand Up @@ -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(<LoginScreen />, { wrapper: BrowserRouter });

Expand Down
6 changes: 3 additions & 3 deletions src/screens/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/screens/ResetPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');

Expand Down

0 comments on commit d348717

Please sign in to comment.