Skip to content

Commit

Permalink
Merge pull request #985 from DeepBlueCLtd/979_changing_role_triggered…
Browse files Browse the repository at this point in the history
…_change_password

Change user role triggered reset password issue resolved
  • Loading branch information
IanMayo authored Jan 16, 2024
2 parents 4a079dc + df078ec commit 00402d1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
VisibilityOff
} from '@mui/icons-material'
import { getDataProvider } from './providers/dataProvider'
import rcoAuthProvider from './providers/authProvider'
import rcoAuthProvider, { removeUserToken } from './providers/authProvider'
import { useForm } from 'react-hook-form'
import * as yup from 'yup'

Expand Down Expand Up @@ -232,6 +232,11 @@ function App(): React.ReactElement {
}

useEffect(() => {
// Check if session not exist. clear the user token from cookies
const storedSessionData = sessionStorage.getItem(constants.SESSION_LOGIN)
if (storedSessionData === null) {
removeUserToken()
}
const storedValue = localStorage.getItem(constants.LOGGING_ENABLED)
if (storedValue !== null) {
setLoggingPref(storedValue === 'true')
Expand Down
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export const LOCAL_STORAGE_DB_KEY = 'rco-'
export const DATE_FORMAT = 'yyyy-MM-dd'
export const DATETIME_FORMAT = 'dd/MMM/yyyy HH:mm'
export const TOKEN_KEY = 'rco-user'

export const MUTATION_MODE = 'optimistic'
// session storage value.
export const SESSION_LOGIN = 'login'


// major table/resource names
export const R_USERS = 'user'
Expand Down
7 changes: 4 additions & 3 deletions src/providers/authProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const setToken = (token: string): void => {
document.cookie = `${constants.TOKEN_KEY}=${token}; expires=${expires}; path=/ `
}

export const removeToken = (): void => {
export const removeUserToken = (): void => {
removeCookie(constants.TOKEN_KEY)
}

Expand Down Expand Up @@ -144,6 +144,7 @@ const authProvider = (dataProvider: DataProvider): AuthProvider => {
try {
const res = await login({ password, staffNumber })
await createUserToken(res.data.data, audit)
sessionStorage.setItem('login', 'true')
return await Promise.resolve(res.data.data)
} catch (error) {
if (isAxiosError(error))
Expand All @@ -163,7 +164,7 @@ const authProvider = (dataProvider: DataProvider): AuthProvider => {
securityRelated: null,
activityDetail: null
})
removeToken()
removeUserToken()
await Promise.resolve()
},
checkAuth: async (): Promise<void> => {
Expand All @@ -176,7 +177,7 @@ const authProvider = (dataProvider: DataProvider): AuthProvider => {
checkError: async (error): Promise<any> => {
const status = error.status
if (status === 401 || status === 403) {
removeToken()
removeUserToken()
await Promise.reject(
new Error('Server returned code ' + String(status))
)
Expand Down
2 changes: 1 addition & 1 deletion src/resources/users/UserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function UserForm({ isEdit }: FormProps): React.ReactElement {

return (
<SimpleForm
record={{ ...record, password: '' }}
record={{ ...record }}
toolbar={<EditToolBar />}
defaultValues={defaultValues}
resolver={yupResolver(schema)}>
Expand Down

0 comments on commit 00402d1

Please sign in to comment.