diff --git a/src/app/auth/index.js b/src/app/auth/index.js deleted file mode 100644 index 174e805..0000000 --- a/src/app/auth/index.js +++ /dev/null @@ -1 +0,0 @@ -export { default as authRoles } from './authRoles'; diff --git a/src/app/auth/services/jwtService/index.js b/src/app/auth/services/jwtService/index.js deleted file mode 100644 index 1b18d9c..0000000 --- a/src/app/auth/services/jwtService/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import JwtService from './jwtService'; - -export default JwtService; diff --git a/src/app/auth/services/jwtService/jwtService.js b/src/app/auth/services/jwtService/jwtService.js deleted file mode 100644 index 99a7511..0000000 --- a/src/app/auth/services/jwtService/jwtService.js +++ /dev/null @@ -1,151 +0,0 @@ -import FuseUtils from '@fuse/utils/FuseUtils'; -import axios from 'axios'; -import jwtDecode from 'jwt-decode'; -import jwtServiceConfig from './jwtServiceConfig'; - -/* eslint-disable camelcase */ - -class JwtService extends FuseUtils.EventEmitter { - init() { - this.setInterceptors(); - this.handleAuthentication(); - } - - setInterceptors = () => { - axios.interceptors.response.use( - (response) => { - return response; - }, - (err) => { - return new Promise((resolve, reject) => { - if (err.response.status === 401 && err.config && !err.config.__isRetryRequest) { - // if you ever get an unauthorized response, logout the user - this.emit('onAutoLogout', 'Invalid access_token'); - this.setSession(null); - } - throw err; - }); - } - ); - }; - - handleAuthentication = () => { - const access_token = this.getAccessToken(); - - if (!access_token) { - this.emit('onNoAccessToken'); - - return; - } - - if (this.isAuthTokenValid(access_token)) { - this.setSession(access_token); - this.emit('onAutoLogin', true); - } else { - this.setSession(null); - this.emit('onAutoLogout', 'access_token expired'); - } - }; - - createUser = (data) => { - return new Promise((resolve, reject) => { - axios.post(jwtServiceConfig.signUp, data).then((response) => { - if (response.data.user) { - this.setSession(response.data.access_token); - resolve(response.data.user); - this.emit('onLogin', response.data.user); - } else { - reject(response.data.error); - } - }); - }); - }; - - signInWithEmailAndPassword = (email, password) => { - return new Promise((resolve, reject) => { - axios - .get(jwtServiceConfig.signIn, { - data: { - email, - password, - }, - }) - .then((response) => { - if (response.data.user) { - this.setSession(response.data.access_token); - resolve(response.data.user); - this.emit('onLogin', response.data.user); - } else { - reject(response.data.error); - } - }); - }); - }; - - signInWithToken = () => { - return new Promise((resolve, reject) => { - axios - .get(jwtServiceConfig.accessToken, { - data: { - access_token: this.getAccessToken(), - }, - }) - .then((response) => { - if (response.data.user) { - this.setSession(response.data.access_token); - resolve(response.data.user); - } else { - this.logout(); - reject(new Error('Failed to login with token.')); - } - }) - .catch((error) => { - this.logout(); - reject(new Error('Failed to login with token.')); - }); - }); - }; - - updateUserData = (user) => { - return axios.post(jwtServiceConfig.updateUser, { - user, - }); - }; - - setSession = (access_token) => { - if (access_token) { - localStorage.setItem('jwt_access_token', access_token); - axios.defaults.headers.common.Authorization = `Bearer ${access_token}`; - } else { - localStorage.removeItem('jwt_access_token'); - delete axios.defaults.headers.common.Authorization; - } - }; - - logout = () => { - this.setSession(null); - this.emit('onLogout', 'Logged out'); - }; - - isAuthTokenValid = (access_token) => { - if (!access_token) { - return false; - } - const decoded = jwtDecode(access_token); - const currentTime = Date.now() / 1000; - if (decoded.exp < currentTime) { - console.warn('access token expired'); - return false; - } - - return true; - }; - - getAccessToken = () => { - return window.localStorage.getItem('jwt_access_token'); - }; -} - -const instance = new JwtService(); - -export default instance; diff --git a/src/app/auth/services/jwtService/jwtServiceConfig.js b/src/app/auth/services/jwtService/jwtServiceConfig.js deleted file mode 100644 index 52dfa61..0000000 --- a/src/app/auth/services/jwtService/jwtServiceConfig.js +++ /dev/null @@ -1,8 +0,0 @@ -const jwtServiceConfig = { - signIn: 'api/auth/sign-in', - signUp: 'api/auth/sign-up', - accessToken: 'api/auth/access-token', - updateUser: 'api/auth/user/update', -}; - -export default jwtServiceConfig; diff --git a/src/app/auth/authRoles.js b/src/app/configs/authRoles.js similarity index 100% rename from src/app/auth/authRoles.js rename to src/app/configs/authRoles.js diff --git a/src/app/main/authPages/sign-in/SignInConfig.js b/src/app/main/authPages/sign-in/SignInConfig.js index fa7d38a..8f48632 100644 --- a/src/app/main/authPages/sign-in/SignInConfig.js +++ b/src/app/main/authPages/sign-in/SignInConfig.js @@ -1,7 +1,7 @@ import i18next from 'i18next'; import SignInPage from './SignInPage'; -import authRoles from '../../../auth/authRoles'; +import authRoles from '../../../configs/authRoles'; import en from './i18n/en'; i18next.addResourceBundle('en', 'signInPage', en); diff --git a/src/app/main/authPages/sign-in/SignInPage.js b/src/app/main/authPages/sign-in/SignInPage.js index 761ab9c..d8e6ca3 100644 --- a/src/app/main/authPages/sign-in/SignInPage.js +++ b/src/app/main/authPages/sign-in/SignInPage.js @@ -10,13 +10,14 @@ import Typography from '@mui/material/Typography'; import { Controller, useForm } from 'react-hook-form'; import { withTranslation } from 'react-i18next'; import { Link } from 'react-router-dom'; +import { authService } from 'src/app/services'; import * as yup from 'yup'; import LeftSideCanvas from '../shared-components/LeftSideCanvas'; const defaultValues = { email: '', password: '', - remember: true, + remember: false, }; function SignInPage({ t }) { @@ -25,7 +26,7 @@ function SignInPage({ t }) { password: yup.string().required(t('password_error')).min(8, t('password_error')), }); - const { control, formState, handleSubmit, reset } = useForm({ + const { control, formState, handleSubmit, setError } = useForm({ mode: 'onChange', defaultValues, resolver: yupResolver(schema), @@ -33,8 +34,13 @@ function SignInPage({ t }) { const { isValid, dirtyFields, errors } = formState; - function onSubmit() { - reset(defaultValues); + function onSubmit(data) { + authService.signInWithEmailAndPassword(data).catch((error) => { + setError('root', { + type: 'manual', + message: error.message, + }); + }); } return ( @@ -145,6 +151,7 @@ function SignInPage({ t }) { > {t('sign_in_btn')} + {errors.root &&
{errors.root.message}
} diff --git a/src/app/main/authPages/sign-out/SignOutPage.js b/src/app/main/authPages/sign-out/SignOutPage.js index 8cb6be9..3332fcd 100644 --- a/src/app/main/authPages/sign-out/SignOutPage.js +++ b/src/app/main/authPages/sign-out/SignOutPage.js @@ -1,12 +1,12 @@ import Typography from '@mui/material/Typography'; import Paper from '@mui/material/Paper'; import { useEffect } from 'react'; -import JwtService from '../../../auth/services/jwtService'; +import { authService } from 'src/app/services'; function SignOutPage() { useEffect(() => { setTimeout(() => { - JwtService.logout(); + authService.logout(); }, 1000); }, []); diff --git a/src/app/main/authPages/sign-up/SignUpConfig.js b/src/app/main/authPages/sign-up/SignUpConfig.js index 3a7e9fc..1fdf098 100644 --- a/src/app/main/authPages/sign-up/SignUpConfig.js +++ b/src/app/main/authPages/sign-up/SignUpConfig.js @@ -1,7 +1,7 @@ import i18next from 'i18next'; import SignUpPage from './SignUpPage'; -import authRoles from '../../../auth/authRoles'; +import authRoles from '../../../configs/authRoles'; import en from './i18n/en'; i18next.addResourceBundle('en', 'signUpPage', en); diff --git a/src/app/main/authPages/sign-up/SignUpPage.js b/src/app/main/authPages/sign-up/SignUpPage.js index c1a3bf2..2519c73 100644 --- a/src/app/main/authPages/sign-up/SignUpPage.js +++ b/src/app/main/authPages/sign-up/SignUpPage.js @@ -11,6 +11,7 @@ import Typography from '@mui/material/Typography'; import { Controller, useForm } from 'react-hook-form'; import { withTranslation } from 'react-i18next'; import { Link } from 'react-router-dom'; +import { authService } from 'src/app/services'; import * as yup from 'yup'; import LeftSideCanvas from '../shared-components/LeftSideCanvas'; @@ -31,7 +32,7 @@ function SignUpPage({ t }) { acceptTermsConditions: yup.boolean().oneOf([true], t('accept_terms_error')), }); - const { control, formState, handleSubmit, reset } = useForm({ + const { control, formState, handleSubmit, setError } = useForm({ mode: 'onChange', defaultValues, resolver: yupResolver(schema), @@ -39,8 +40,19 @@ function SignUpPage({ t }) { const { isValid, dirtyFields, errors } = formState; - function onSubmit() { - reset(defaultValues); + function onSubmit({ name, password, email }) { + authService + .createUser({ + displayName: name, + password, + email, + }) + .catch((error) => { + setError('root', { + type: 'manual', + message: error.message, + }); + }); } return ( diff --git a/src/app/main/navigationPages/dashboard/DashboardConfig.js b/src/app/main/navigationPages/dashboard/DashboardConfig.js index 1912a20..1c860bb 100644 --- a/src/app/main/navigationPages/dashboard/DashboardConfig.js +++ b/src/app/main/navigationPages/dashboard/DashboardConfig.js @@ -1,6 +1,6 @@ import i18next from 'i18next'; -import { authRoles } from 'src/app/auth'; +import authRoles from '../../../configs/authRoles'; import Dashboard from './Dashboard'; import en from './i18n/en'; diff --git a/src/app/main/navigationPages/favorites/FavoritesConfig.js b/src/app/main/navigationPages/favorites/FavoritesConfig.js index f041d50..5c1998e 100644 --- a/src/app/main/navigationPages/favorites/FavoritesConfig.js +++ b/src/app/main/navigationPages/favorites/FavoritesConfig.js @@ -1,6 +1,6 @@ import i18next from 'i18next'; -import { authRoles } from 'src/app/auth'; +import authRoles from '../../../configs/authRoles'; import Favorites from './Favorites'; import en from './i18n/en'; diff --git a/src/app/main/navigationPages/history/HistoryConfig.js b/src/app/main/navigationPages/history/HistoryConfig.js index 8b4943c..c19ee03 100644 --- a/src/app/main/navigationPages/history/HistoryConfig.js +++ b/src/app/main/navigationPages/history/HistoryConfig.js @@ -1,6 +1,6 @@ import i18next from 'i18next'; -import { authRoles } from 'src/app/auth'; +import authRoles from '../../../configs/authRoles'; import History from './History'; import en from './i18n/en'; diff --git a/src/app/main/navigationPages/profile/ProfileConfig.js b/src/app/main/navigationPages/profile/ProfileConfig.js index 73ed73a..1603bd4 100644 --- a/src/app/main/navigationPages/profile/ProfileConfig.js +++ b/src/app/main/navigationPages/profile/ProfileConfig.js @@ -1,6 +1,6 @@ import i18next from 'i18next'; -import { authRoles } from 'src/app/auth'; +import authRoles from '../../../configs/authRoles'; import en from './i18n/en'; import Profile from './Profile';