action/authActionimport { Dispatch } from '../../../node_modules/redux';
import {asyncConstant} from '../constant/asyncConstant';
import {commonConstant} from '../constant/commonConstant';
import {authenticationRequest} from 'api/authApi';
export const login = (email: string, password: string): any => {
return (dispatch: Dispatch) => {
dispatch({type: `${commonConstant.REQUES_FOR_AUTH}${asyncConstant.BEGIN}`});
authenticationRequest(email, password)
.then((data: any) => {
if (data.auth) {
dispatch({type: `${commonConstant.REQUES_FOR_AUTH}${asyncConstant.SUCCESS}`});
} else {
dispatch({type: `${commonConstant.REQUES_FOR_AUTH}${asyncConstant.FAILURE}`, payload: {getServerDataError: true}});
}
})
.catch((error: any) => {
console.log(error);
dispatch({type: `${commonConstant.REQUES_FOR_AUTH}${asyncConstant.FAILURE}`, payload: {connectServerError: true}});
});
};
};
api/authApiimport axios, { AxiosPromise } from 'axios';
import {config} from '../config';
import {ServerRoters} from '../constant/serverRouters';
export function authenticationRequest(email: string, password: string): AxiosPromise {
return axios.post(`${config.apiPrefix}:${config.serverPort}/${ServerRoters.auth}`,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
email,
password,
})
.then((response: any) => {
if (response.status === 200 || 304) {
return response;
}
}).then((response: any) => {
if (response.data.auth) {
return {...response.data};
} else {
return {...response.data, auth: false };
}
});
}
component/Authimport * as hash from 'object-hash';
import * as React from 'react';
export interface IAuthDispatch {
login: (email: string, password: string) => any;
}
export class Auth extends React.Component<IAuthDispatch> {
public state = {
emailValue: '',
passwordValue: '',
};
public onChangeInput = (e: React.FormEvent<HTMLInputElement>) => {
const state: any = {...this.state};
state[e.currentTarget.name] = e.currentTarget.name === 'passwordValue' ? hash(e.currentTarget.value) : e.currentTarget.value;
this.setState(state);
}
public onHandleSubmit = () => {
console.log(this.state);
this.props.login(this.state.emailValue, this.state.passwordValue);
}
public render() {
return (
<React.Fragment>
<form onSubmit={ (e: React.FormEvent<HTMLFormElement>) => { e.preventDefault(); } }>
<input onChange={this.onChangeInput} type='text' name='emailValue' placeholder='user name' />
<input onChange={this.onChangeInput} type='password' name='passwordValue' placeholder='password' />
<input onClick={this.onHandleSubmit} type='submit' name='submit'/>
</form>
</React.Fragment>
);
}
}
constantexport enum asyncConstant {
'BEGIN' = '_BEGIN',
'SUCCESS' = '_SUCCESS',
'FAILURE' = '_FAILURE',
}
export enum commonConstant {
'REQUES_FOR_AUTH' = 'REQUES_FOR_AUTH',
}
export enum ServerRoters {
auth = 'api/auth/login',
getRole = 'api/auth/role',
logout = 'api/auth/logout',
getTestListData = 'api/student/gettestlist',
getIssues = 'api/student/gettestissues',
}
container/ContainerAuthimport {connect} from 'react-redux';
import {Dispatch} from 'redux';
import * as actions from 'actions/authActions';
import {Auth, IAuthDispatch} from 'components/Auth';
const mapDispatchToProps = (dispatch: Dispatch): IAuthDispatch => ({
login: (email: string, password: string) => {
dispatch(actions.login(email, password));
},
});
export const ContainerAuth = connect(
mapDispatchToProps,
)(Auth);