Skip to content

Commit

Permalink
removed access token from local storage (#1409)
Browse files Browse the repository at this point in the history
* removed access token from local storage

* fix
  • Loading branch information
Tolik170 authored Nov 17, 2023
1 parent b324dce commit 307f46c
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 42 deletions.
2 changes: 0 additions & 2 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { AlertColor } from '@mui/material/Alert'

export const s2s = 's2s'

export const accessToken = 'accessToken'

export const student = 'student'
export const tutor = 'tutor'
export const admin = 'admin'
Expand Down
2 changes: 1 addition & 1 deletion src/containers/my-quizzes/QuizzesContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const QuizzesContainer = () => {
(error: ErrorResponse) => {
setAlert({
severity: snackbarVariants.error,
message: error ? `errors.${error.message}` : ''
message: error ? `errors.${error.code}` : ''
})
},
[setAlert]
Expand Down
14 changes: 2 additions & 12 deletions src/redux/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import {
isRejected
} from '@reduxjs/toolkit'
import { AuthService } from '~/services/auth-service'
import {
getFromLocalStorage,
removeFromLocalStorage,
setToLocalStorage
} from '~/services/local-storage-service'
import { accessToken } from '~/constants'
import { AxiosError } from 'axios'
import {
AccessToken,
Expand Down Expand Up @@ -48,7 +42,6 @@ export const loginUser = createAsyncThunk(
async (userData: LoginParams, { rejectWithValue, dispatch }) => {
try {
const { data } = await AuthService.login(userData)
setToLocalStorage(accessToken, data.accessToken)
dispatch(setUser(data.accessToken))
} catch (e) {
const error = e as AxiosError<ErrorResponse>
Expand All @@ -62,7 +55,6 @@ export const googleAuth = createAsyncThunk(
async (userData: GoogleAuthParams, { rejectWithValue, dispatch }) => {
try {
const { data } = await AuthService.googleAuth(userData)
setToLocalStorage(accessToken, data.accessToken)
dispatch(setUser(data.accessToken))
} catch (e) {
const error = e as AxiosError<ErrorResponse>
Expand All @@ -88,7 +80,6 @@ export const logoutUser = createAsyncThunk(
async (_, { rejectWithValue, dispatch }) => {
try {
await AuthService.logout()
removeFromLocalStorage(accessToken)
dispatch(logout())
} catch (e) {
const error = e as AxiosError<ErrorResponse>
Expand All @@ -101,9 +92,8 @@ export const checkAuth = createAsyncThunk(
'appMain/checkAuth',
async (_, { rejectWithValue, dispatch }) => {
try {
if (getFromLocalStorage(accessToken)) {
const { data } = await AuthService.refresh()
setToLocalStorage(accessToken, data.accessToken)
const { data } = await AuthService.refresh()
if (data) {
dispatch(setUser(data.accessToken))
}
} catch (e) {
Expand Down
14 changes: 1 addition & 13 deletions src/services/setup-interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { AxiosError, AxiosRequestConfig } from 'axios'
import { accessToken } from '~/constants'
import { axiosClient } from '~/plugins/axiosClient'
import { logoutUser } from '~/redux/reducer'
import { AuthService } from '~/services/auth-service'
import { getFromLocalStorage, setToLocalStorage } from './local-storage-service'
import i18n from '~/plugins/i18n'
import { Store } from '~/redux/store'

export const setupInterceptors = (store: Store): void => {
axiosClient.interceptors.request.use((config: AxiosRequestConfig) => {
const token = getFromLocalStorage(accessToken)
if (token) {
config.headers = {
...config.headers,
Authorization: `Bearer ${token}`
}
}
config.headers = {
...config.headers,
'Accept-Language': i18n.language
Expand All @@ -31,9 +21,7 @@ export const setupInterceptors = (store: Store): void => {
const originalRequest = error.config
if (error.code === 'UNAUTHORIZED' && error.config) {
try {
const { data } = await AuthService.refresh()
setToLocalStorage(accessToken, data.accessToken)
return axiosClient.request(originalRequest)
return await axiosClient.request(originalRequest)
} catch (e) {
void store.dispatch(logoutUser())
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/user/user-interfaces/user.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '~/types'

export interface LocalStorage {
accessToken?: string
'emoji-mart.last'?: string
}

export interface GetUsersParams extends RequestParams {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/components/layout/AppMain.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('AppMain layout component test', () => {
expect(loader).toBeInTheDocument()
})

it('should dispatch checkAuth if accessToken exists in localStorage', async () => {
it('should dispatch checkAuth action', async () => {
renderWithProviders(<AppMain />, {
preloadedState: {
appMain: {
Expand Down
6 changes: 2 additions & 4 deletions tests/unit/containers/my-quizzes/QuizzesContainer.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('QuizzesContainer component with data', () => {
describe('QuizzesContainer component with an error', () => {
beforeEach(() => {
mockAxiosClient.onGet(URLs.quizzes.get).reply(404, {
code: 'error',
code: 'UNAUTHORIZED',
message: 'The requested URL was not found.'
})
renderWithProviders(<QuizzesContainer />)
Expand All @@ -72,8 +72,6 @@ describe('QuizzesContainer component with an error', () => {
})

it('should return error message', () => {
expect(
screen.getByText('errors.The requested URL was not found.')
).toBeInTheDocument()
expect(screen.getByText('errors.UNAUTHORIZED')).toBeInTheDocument()
})
})
24 changes: 16 additions & 8 deletions tests/unit/services/local-storage-service.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getFromLocalStorage, setToLocalStorage, removeFromLocalStorage } from '~/services/local-storage-service'
import {
getFromLocalStorage,
setToLocalStorage,
removeFromLocalStorage
} from '~/services/local-storage-service'
import { vi } from 'vitest'

describe('Local storage service test', () => {
Expand All @@ -17,12 +21,13 @@ describe('Local storage service test', () => {
})

it('should set item if local storage is empty', () => {
setToLocalStorage('accessToken', 'token')
setToLocalStorage('testItem', 'test')

expect(window.localStorage.setItem).toBeCalled()
})

it('should not remove item from local storage', () => {
removeFromLocalStorage('accessToken')
removeFromLocalStorage('testItem')

expect(window.localStorage.setItem).not.toBeUndefined()
})
Expand All @@ -34,23 +39,26 @@ describe('Local storage service test', () => {
value: {
getItem: () =>
JSON.stringify({
accessToken: 'token'
testItem: 'test'
}),
setItem: vi.fn()
}
})

const token = getFromLocalStorage('accessToken')
expect(token).toBe('token')
const item = getFromLocalStorage('testItem')

expect(item).toBe('test')
})

it('should set item to local storage', () => {
setToLocalStorage('accessToken', 'token')
setToLocalStorage('testItem', 'test')

expect(window.localStorage.setItem).toBeCalled()
})

it('should remove item from local storage', () => {
removeFromLocalStorage('accessToken')
removeFromLocalStorage('testItem')

expect(window.localStorage.setItem).toBeCalled()
})
})

0 comments on commit 307f46c

Please sign in to comment.