RC-4: move authRoles to src/app/configs
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
export { default as authRoles } from './authRoles';
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import JwtService from './jwtService';
|
|
||||||
|
|
||||||
export default JwtService;
|
|
||||||
@@ -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;
|
|
||||||
@@ -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;
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
|
|
||||||
import SignInPage from './SignInPage';
|
import SignInPage from './SignInPage';
|
||||||
import authRoles from '../../../auth/authRoles';
|
import authRoles from '../../../configs/authRoles';
|
||||||
import en from './i18n/en';
|
import en from './i18n/en';
|
||||||
|
|
||||||
i18next.addResourceBundle('en', 'signInPage', en);
|
i18next.addResourceBundle('en', 'signInPage', en);
|
||||||
|
|||||||
@@ -10,13 +10,14 @@ import Typography from '@mui/material/Typography';
|
|||||||
import { Controller, useForm } from 'react-hook-form';
|
import { Controller, useForm } from 'react-hook-form';
|
||||||
import { withTranslation } from 'react-i18next';
|
import { withTranslation } from 'react-i18next';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
import { authService } from 'src/app/services';
|
||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
import LeftSideCanvas from '../shared-components/LeftSideCanvas';
|
import LeftSideCanvas from '../shared-components/LeftSideCanvas';
|
||||||
|
|
||||||
const defaultValues = {
|
const defaultValues = {
|
||||||
email: '',
|
email: '',
|
||||||
password: '',
|
password: '',
|
||||||
remember: true,
|
remember: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
function SignInPage({ t }) {
|
function SignInPage({ t }) {
|
||||||
@@ -25,7 +26,7 @@ function SignInPage({ t }) {
|
|||||||
password: yup.string().required(t('password_error')).min(8, t('password_error')),
|
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',
|
mode: 'onChange',
|
||||||
defaultValues,
|
defaultValues,
|
||||||
resolver: yupResolver(schema),
|
resolver: yupResolver(schema),
|
||||||
@@ -33,8 +34,13 @@ function SignInPage({ t }) {
|
|||||||
|
|
||||||
const { isValid, dirtyFields, errors } = formState;
|
const { isValid, dirtyFields, errors } = formState;
|
||||||
|
|
||||||
function onSubmit() {
|
function onSubmit(data) {
|
||||||
reset(defaultValues);
|
authService.signInWithEmailAndPassword(data).catch((error) => {
|
||||||
|
setError('root', {
|
||||||
|
type: 'manual',
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -145,6 +151,7 @@ function SignInPage({ t }) {
|
|||||||
>
|
>
|
||||||
{t('sign_in_btn')}
|
{t('sign_in_btn')}
|
||||||
</Button>
|
</Button>
|
||||||
|
{errors.root && <p>{errors.root.message}</p>}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import Paper from '@mui/material/Paper';
|
import Paper from '@mui/material/Paper';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import JwtService from '../../../auth/services/jwtService';
|
import { authService } from 'src/app/services';
|
||||||
|
|
||||||
function SignOutPage() {
|
function SignOutPage() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
JwtService.logout();
|
authService.logout();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
|
|
||||||
import SignUpPage from './SignUpPage';
|
import SignUpPage from './SignUpPage';
|
||||||
import authRoles from '../../../auth/authRoles';
|
import authRoles from '../../../configs/authRoles';
|
||||||
import en from './i18n/en';
|
import en from './i18n/en';
|
||||||
|
|
||||||
i18next.addResourceBundle('en', 'signUpPage', en);
|
i18next.addResourceBundle('en', 'signUpPage', en);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import Typography from '@mui/material/Typography';
|
|||||||
import { Controller, useForm } from 'react-hook-form';
|
import { Controller, useForm } from 'react-hook-form';
|
||||||
import { withTranslation } from 'react-i18next';
|
import { withTranslation } from 'react-i18next';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
import { authService } from 'src/app/services';
|
||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
import LeftSideCanvas from '../shared-components/LeftSideCanvas';
|
import LeftSideCanvas from '../shared-components/LeftSideCanvas';
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ function SignUpPage({ t }) {
|
|||||||
acceptTermsConditions: yup.boolean().oneOf([true], t('accept_terms_error')),
|
acceptTermsConditions: yup.boolean().oneOf([true], t('accept_terms_error')),
|
||||||
});
|
});
|
||||||
|
|
||||||
const { control, formState, handleSubmit, reset } = useForm({
|
const { control, formState, handleSubmit, setError } = useForm({
|
||||||
mode: 'onChange',
|
mode: 'onChange',
|
||||||
defaultValues,
|
defaultValues,
|
||||||
resolver: yupResolver(schema),
|
resolver: yupResolver(schema),
|
||||||
@@ -39,8 +40,19 @@ function SignUpPage({ t }) {
|
|||||||
|
|
||||||
const { isValid, dirtyFields, errors } = formState;
|
const { isValid, dirtyFields, errors } = formState;
|
||||||
|
|
||||||
function onSubmit() {
|
function onSubmit({ name, password, email }) {
|
||||||
reset(defaultValues);
|
authService
|
||||||
|
.createUser({
|
||||||
|
displayName: name,
|
||||||
|
password,
|
||||||
|
email,
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
setError('root', {
|
||||||
|
type: 'manual',
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
|
|
||||||
import { authRoles } from 'src/app/auth';
|
import authRoles from '../../../configs/authRoles';
|
||||||
import Dashboard from './Dashboard';
|
import Dashboard from './Dashboard';
|
||||||
import en from './i18n/en';
|
import en from './i18n/en';
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
|
|
||||||
import { authRoles } from 'src/app/auth';
|
import authRoles from '../../../configs/authRoles';
|
||||||
import Favorites from './Favorites';
|
import Favorites from './Favorites';
|
||||||
import en from './i18n/en';
|
import en from './i18n/en';
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
|
|
||||||
import { authRoles } from 'src/app/auth';
|
import authRoles from '../../../configs/authRoles';
|
||||||
import History from './History';
|
import History from './History';
|
||||||
import en from './i18n/en';
|
import en from './i18n/en';
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
|
|
||||||
import { authRoles } from 'src/app/auth';
|
import authRoles from '../../../configs/authRoles';
|
||||||
import en from './i18n/en';
|
import en from './i18n/en';
|
||||||
import Profile from './Profile';
|
import Profile from './Profile';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user