mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
feat: init auth service
This commit is contained in:
parent
73841e8f68
commit
16613a22a7
@ -1,10 +0,0 @@
|
||||
import axios from 'axios';
|
||||
import { API_BASE_URL } from '../constants';
|
||||
|
||||
/**
|
||||
* Create axios config
|
||||
*/
|
||||
export default axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
responseType: 'json',
|
||||
});
|
||||
@ -24,17 +24,17 @@ type LoginDataInput = {
|
||||
* Authorize user through a form.
|
||||
*/
|
||||
const Auth: React.FC = () => {
|
||||
const [loginData, setLoginData] = useState<LoginDataInput>({
|
||||
const [loginFormData, setLoginFormData] = useState<LoginDataInput>({
|
||||
login: '',
|
||||
password: '',
|
||||
});
|
||||
const { authStore } = useRootStore();
|
||||
const history = useHistory();
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
const handleLoginForm = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
const { login, password } = loginData;
|
||||
const { login, password } = loginFormData;
|
||||
await authStore.login(login, password);
|
||||
history.push('/');
|
||||
};
|
||||
@ -54,7 +54,7 @@ const Auth: React.FC = () => {
|
||||
</Flex>
|
||||
</Box>
|
||||
<Box my='30px'>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<form onSubmit={handleLoginForm}>
|
||||
<FormControl isRequired={true}>
|
||||
<FormLabel>Login</FormLabel>
|
||||
<InputGroup>
|
||||
@ -63,9 +63,12 @@ const Auth: React.FC = () => {
|
||||
type='text'
|
||||
name='login'
|
||||
placeholder='email@mail.com'
|
||||
value={loginData.login}
|
||||
value={loginFormData.login}
|
||||
onChange={(e) =>
|
||||
setLoginData({ ...loginData, login: e.target.value })
|
||||
setLoginFormData({
|
||||
...loginFormData,
|
||||
login: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</InputGroup>
|
||||
@ -78,9 +81,12 @@ const Auth: React.FC = () => {
|
||||
type='password'
|
||||
name='password'
|
||||
placeholder='********'
|
||||
value={loginData.password}
|
||||
value={loginFormData.password}
|
||||
onChange={(e) =>
|
||||
setLoginData({ ...loginData, password: e.target.value })
|
||||
setLoginFormData({
|
||||
...loginFormData,
|
||||
password: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</InputGroup>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import api from '../axios/index';
|
||||
import axios from 'axios';
|
||||
import { API_BASE_URL } from '../constants';
|
||||
import { IDataResult } from '../interfaces';
|
||||
|
||||
interface ILoginResult extends IDataResult {
|
||||
@ -9,6 +10,11 @@ interface ILoginResult extends IDataResult {
|
||||
};
|
||||
}
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
responseType: 'json',
|
||||
});
|
||||
|
||||
export default class AuthService {
|
||||
public async requestLogin(
|
||||
login: string,
|
||||
|
||||
@ -1,9 +1,24 @@
|
||||
// import axios, { AxiosInstance } from 'axios';
|
||||
// import { API_BASE_URL } from '../constants';
|
||||
import AuthStore from './AuthStore';
|
||||
|
||||
export default class RootStore {
|
||||
// public api: AxiosInstance;
|
||||
|
||||
public authStore: AuthStore;
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
* Api root instance
|
||||
*/
|
||||
// this.api = axios.create({
|
||||
// baseURL: API_BASE_URL,
|
||||
// responseType: 'json',
|
||||
// });
|
||||
|
||||
/**
|
||||
* Auth store
|
||||
*/
|
||||
this.authStore = new AuthStore(this);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user