use custom user endpoint for validation and JWT auth for login
This commit is contained in:
@@ -34,56 +34,23 @@ export const validateToken = () => ({
|
||||
type: VALIDATE_TOKEN
|
||||
});
|
||||
|
||||
export const validateAccessToken = (token) => {
|
||||
export const validateAccessToken = () => {
|
||||
return dispatch => {
|
||||
dispatch(validateToken());
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/wp-json/jwt-auth/v1/token/validate`,
|
||||
method: 'post'
|
||||
})
|
||||
url: `${API_SERVER}/wp-json/wiaas/user/validate-token`,
|
||||
method: 'post'
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data && response.data.data.status === 200) {
|
||||
if (response.data && response.status === 200) {
|
||||
|
||||
// TODO: Implement refresh logic on backend as it was on old wias , or find a nother way
|
||||
// to handle token validation another way
|
||||
// const serverTime = response.data.serverTime || 1;
|
||||
|
||||
dispatch(loggedIn({
|
||||
accessToken: token,
|
||||
userInfo: {
|
||||
"id": 2,
|
||||
"name": "Customer User",
|
||||
"mail": "customer@mail.com",
|
||||
"phone": "",
|
||||
"userType": "customer",
|
||||
"vatCode": "556084-6783",
|
||||
"companyName": "Coor Service Management AB",
|
||||
"billingAddresses": [
|
||||
{
|
||||
"id": 1,
|
||||
"city": "fsdfcsdfcs",
|
||||
"countryName": "SE",
|
||||
"detailedAddress": "sdfcsvfsdf, fdfvds, fdfvds",
|
||||
"firstName": "Customer",
|
||||
"lastName": "User",
|
||||
"zipCode": "323232"
|
||||
}
|
||||
],
|
||||
"profileAddresses": [
|
||||
{
|
||||
"id": 1,
|
||||
"city": "fsdfcsdfcs",
|
||||
"countryName": "fsdfcsdfcs",
|
||||
"detailedAddress": "sdfcsvfsdf, fdfvds, fdfvds",
|
||||
"zipCode": "323232"
|
||||
}
|
||||
]
|
||||
}
|
||||
}));
|
||||
dispatch(loggedIn(response.data.userInfo));
|
||||
// refreshToken = response.data.refreshToken;
|
||||
// startRefreshTimer(dispatch, serverTime);
|
||||
// dispatch(setUserAsCompanyAdmin(response.data.userInfo.wiaas_is_company_admin));
|
||||
dispatch(setUserAsCompanyAdmin(false));
|
||||
dispatch(setUserAsCompanyAdmin(response.data.userInfo.wiaas_is_company_admin));
|
||||
} else {
|
||||
dispatch(loginFail(response.data));
|
||||
}
|
||||
@@ -96,7 +63,7 @@ export const validateAccessToken = (token) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const setUserAsCompanyAdmin = (isCompanyAdmin) => ({type: SET_COMPANY_ADMIN_FLAG, isCompanyAdmin});
|
||||
export const setUserAsCompanyAdmin = (isCompanyAdmin) => ({ type: SET_COMPANY_ADMIN_FLAG, isCompanyAdmin });
|
||||
|
||||
export const validateCredentials = (username, password) => {
|
||||
return dispatch => {
|
||||
@@ -113,22 +80,12 @@ export const validateCredentials = (username, password) => {
|
||||
.then(response => {
|
||||
if (response.data && response.data.token) {
|
||||
const decodedAceessToken = jwtDecode(response.data.token);
|
||||
|
||||
// TODO : Uncomment code, and fix user type logic after adding customer type to woocommerce backend
|
||||
|
||||
// if(decodedAceessToken.data.wiaas_user_type === 'customer'){
|
||||
localStorage.setItem('accessToken', response.data.token);
|
||||
localStorage.setItem('username', username);
|
||||
localStorage.setItem('userInfo', JSON.stringify(response.data.userInfo));
|
||||
const serverTime = decodedAceessToken.nbf || 1;
|
||||
// refreshToken = response.data.refreshToken;
|
||||
startRefreshTimer(dispatch, serverTime);
|
||||
dispatch(loggedIn(response.data));
|
||||
// dispatch(setUserAsCompanyAdmin(response.data.userInfo.wiaas_is_company_admin));
|
||||
dispatch(setUserAsCompanyAdmin(false));
|
||||
|
||||
// }else{
|
||||
// dispatch(loginFail({status: 'fail', errorMessage: 'INVALID_USER_TYPE'}));
|
||||
// }
|
||||
dispatch(loggedIn(response.data.userInfo));
|
||||
dispatch(setUserAsCompanyAdmin(response.data.userInfo.wiaas_is_company_admin));
|
||||
} else {
|
||||
dispatch(loginFail(response.data));
|
||||
}
|
||||
@@ -145,10 +102,10 @@ const startRefreshTimer = (dispatch, serverTime) => {
|
||||
const tokenTimeLeft = decodedAceessToken.exp - serverTime;
|
||||
const refreshTime = tokenTimeLeft ? (tokenTimeLeft - TEN_MINUTES) * 1000 : REFRESH_TIME;
|
||||
|
||||
if(refreshTime <= 0){
|
||||
if (refreshTime <= 0) {
|
||||
dispatch(validateRefreshToken());
|
||||
}else{
|
||||
refreshTimer = setTimeout(()=>{
|
||||
} else {
|
||||
refreshTimer = setTimeout(() => {
|
||||
dispatch(validateRefreshToken());
|
||||
}, refreshTime);
|
||||
}
|
||||
@@ -162,28 +119,28 @@ const validateRefreshToken = () => {
|
||||
return dispatch => {
|
||||
dispatch(requestRefreshToken());
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/login/api/refreshToken`,
|
||||
method: 'post',
|
||||
data: {
|
||||
refreshToken,
|
||||
lastActivity: authActivity.lastActivity
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data.status === 'success') {
|
||||
localStorage.setItem('accessToken', response.data.accessToken);
|
||||
const serverTime = response.data.serverTime || 1;
|
||||
refreshToken = response.data.refreshToken;
|
||||
dispatch(setUserAsCompanyAdmin(response.data.userInfo.wiaas_is_company_admin));
|
||||
startRefreshTimer(dispatch, serverTime);
|
||||
} else {
|
||||
dispatch(logout(response.data));
|
||||
dispatch(loginFail(response.data));
|
||||
url: `${API_SERVER}/login/api/refreshToken`,
|
||||
method: 'post',
|
||||
data: {
|
||||
refreshToken,
|
||||
lastActivity: authActivity.lastActivity
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
htmlClient.onError(error, dispatch);
|
||||
});
|
||||
.then(response => {
|
||||
if (response.data.status === 'success') {
|
||||
localStorage.setItem('accessToken', response.data.accessToken);
|
||||
const serverTime = response.data.serverTime || 1;
|
||||
refreshToken = response.data.refreshToken;
|
||||
dispatch(setUserAsCompanyAdmin(response.data.userInfo.wiaas_is_company_admin));
|
||||
startRefreshTimer(dispatch, serverTime);
|
||||
} else {
|
||||
dispatch(logout(response.data));
|
||||
dispatch(loginFail(response.data));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
htmlClient.onError(error, dispatch);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -193,62 +150,62 @@ export const getModules = () => {
|
||||
dispatch(requestModules());
|
||||
let appModules = {
|
||||
modules: {
|
||||
modules: [
|
||||
{
|
||||
id: '15',
|
||||
name: 'Terms',
|
||||
menuName: 'Terms',
|
||||
url: 'terms',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '19',
|
||||
name: 'Cart',
|
||||
menuName: 'Cart',
|
||||
url: 'cart',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '14',
|
||||
name: 'ProfileSettings',
|
||||
menuName: 'ProfileSettings',
|
||||
url: 'profileSettings',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '23',
|
||||
name: 'OrderProjects',
|
||||
menuName: 'OrderProjects',
|
||||
url: 'orderProjects',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '1',
|
||||
name: 'Dashboards',
|
||||
menuName: 'Overview',
|
||||
url: 'dashboards',
|
||||
isInMenu: '1'
|
||||
},
|
||||
{
|
||||
id: '18',
|
||||
name: 'CoMarket',
|
||||
menuName: 'Co-Market',
|
||||
url: 'co-market',
|
||||
isInMenu: '1'
|
||||
modules: [
|
||||
{
|
||||
id: '15',
|
||||
name: 'Terms',
|
||||
menuName: 'Terms',
|
||||
url: 'terms',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '19',
|
||||
name: 'Cart',
|
||||
menuName: 'Cart',
|
||||
url: 'cart',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '14',
|
||||
name: 'ProfileSettings',
|
||||
menuName: 'ProfileSettings',
|
||||
url: 'profileSettings',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '23',
|
||||
name: 'OrderProjects',
|
||||
menuName: 'OrderProjects',
|
||||
url: 'orderProjects',
|
||||
isInMenu: '0'
|
||||
},
|
||||
{
|
||||
id: '1',
|
||||
name: 'Dashboards',
|
||||
menuName: 'Overview',
|
||||
url: 'dashboards',
|
||||
isInMenu: '1'
|
||||
},
|
||||
{
|
||||
id: '18',
|
||||
name: 'CoMarket',
|
||||
menuName: 'Co-Market',
|
||||
url: 'co-market',
|
||||
isInMenu: '1'
|
||||
}
|
||||
],
|
||||
subModules: {
|
||||
'co-market': [
|
||||
{
|
||||
moduleUrl: 'co-market',
|
||||
menuName: 'Orders',
|
||||
name: 'Orders',
|
||||
url: 'orders'
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
subModules: {
|
||||
'co-market': [
|
||||
{
|
||||
moduleUrl: 'co-market',
|
||||
menuName: 'Orders',
|
||||
name: 'Orders',
|
||||
url: 'orders'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dispatch(recieveModules(appModules));
|
||||
// return htmlClient.fetch({
|
||||
// url: `${API_SERVER}/login/api/getModules`,
|
||||
@@ -275,6 +232,7 @@ const recieveModules = (json) => ({
|
||||
|
||||
export const logout = () => {
|
||||
localStorage.removeItem('accessToken');
|
||||
localStorage.removeItem('userInfo');
|
||||
clearInterval(refreshTimer);
|
||||
return {
|
||||
type: LOGOUT,
|
||||
@@ -283,11 +241,11 @@ export const logout = () => {
|
||||
}
|
||||
}
|
||||
|
||||
export const loggedIn = (jsonData) => {
|
||||
export const loggedIn = (userInfo) => {
|
||||
return {
|
||||
type: LOGIN_SUCCESS,
|
||||
isLoggedIn: true
|
||||
// userInfo: jsonData.userInfo
|
||||
isLoggedIn: true,
|
||||
userInfo: userInfo
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,13 +261,13 @@ export const generatePassword = (mail) => {
|
||||
return dispatch => {
|
||||
dispatch(requestForgotPassword());
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/login/api/forgotPassword`,
|
||||
method: 'post',
|
||||
data: {mail},
|
||||
header: {}
|
||||
})
|
||||
url: `${API_SERVER}/login/api/forgotPassword`,
|
||||
method: 'post',
|
||||
data: { mail },
|
||||
header: {}
|
||||
})
|
||||
.then(response => {
|
||||
if(typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
if (typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
dispatch(forgotPasswordMessage(response.data.messages[0]));
|
||||
}
|
||||
})
|
||||
@@ -319,7 +277,7 @@ export const generatePassword = (mail) => {
|
||||
}
|
||||
}
|
||||
|
||||
const requestForgotPassword = () => ({
|
||||
const requestForgotPassword = () => ({
|
||||
type: REQUEST_FORGOT_PASSWORD,
|
||||
errorMessage: 'FORGOT_REQUEST_SENT'
|
||||
});
|
||||
@@ -350,13 +308,13 @@ export const changePassword = (token, newPassword, confirmPassword) => {
|
||||
return dispatch => {
|
||||
dispatch(requestChange());
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/login/api/changePassword`,
|
||||
method: 'post',
|
||||
data: {token, newPassword, confirmPassword},
|
||||
header: {}
|
||||
})
|
||||
url: `${API_SERVER}/login/api/changePassword`,
|
||||
method: 'post',
|
||||
data: { token, newPassword, confirmPassword },
|
||||
header: {}
|
||||
})
|
||||
.then(response => {
|
||||
if(response.data.messages && response.data.messages.length > 0){
|
||||
if (response.data.messages && response.data.messages.length > 0) {
|
||||
dispatch(passwordChanged(response.data.messages[0]));
|
||||
}
|
||||
})
|
||||
|
||||
@@ -29,36 +29,15 @@ export const recieveProfileInfo = (json) => ({
|
||||
export const fetchProfileInfo = (idUser) => {
|
||||
return dispatch => {
|
||||
dispatch(requestProfileInfo());
|
||||
|
||||
dispatch(recieveProfileInfo({
|
||||
"id": 2,
|
||||
"name": "Customer User",
|
||||
"mail": "customer@mail.com",
|
||||
"phone": "",
|
||||
"userType": "customer",
|
||||
"vatCode": "556084-6783",
|
||||
"companyName": "Coor Service Management AB",
|
||||
"billingAddresses": [
|
||||
{
|
||||
"id": 1,
|
||||
"city": "Göteborg",
|
||||
"countryName": "SE",
|
||||
"detailedAddress": "Lilla Bommen 2",
|
||||
"firstName": "Customer",
|
||||
"lastName": "User",
|
||||
"zipCode": "12323"
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`,
|
||||
method: 'get'
|
||||
})
|
||||
.then(response => {
|
||||
if(response.data){
|
||||
dispatch(recieveProfileInfo(response.data));
|
||||
}
|
||||
],
|
||||
"profileAddresses": [
|
||||
{
|
||||
"id": 1,
|
||||
"city": "Göteborg",
|
||||
"countryName": "Göteborg",
|
||||
"detailedAddress": "Lilla Bommen 2",
|
||||
"zipCode": "12323"
|
||||
}
|
||||
]
|
||||
}));
|
||||
});
|
||||
// return client.fetch({
|
||||
// url: `${API_SERVER}/wp-json/wiaas/cart/customer-info`,
|
||||
// method: 'get',
|
||||
@@ -82,10 +61,14 @@ const requestSaveProfile = () => ({
|
||||
export const saveProfileInfo = (idUser, profile) => {
|
||||
return dispatch => {
|
||||
dispatch(requestSaveProfile());
|
||||
const parsedFullName = profile.name.trim().split(' ');
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/profileSettings/api/saveProfileInfo`,
|
||||
method: 'post',
|
||||
data: {idUser, profile: JSON.stringify(profile)}
|
||||
url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`,
|
||||
method: 'put',
|
||||
data: {
|
||||
'first_name': parsedFullName[0],
|
||||
'last_name': parsedFullName[1],
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if(response.data && response.data.messages){
|
||||
|
||||
@@ -55,7 +55,7 @@ class LogInForm extends Component {
|
||||
|
||||
componentDidMount() {
|
||||
if (localStorage.accessToken) {
|
||||
this.props.dispatch(validateAccessToken(localStorage.accessToken));
|
||||
this.props.dispatch(validateAccessToken());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user