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.
|
* Authorize user through a form.
|
||||||
*/
|
*/
|
||||||
const Auth: React.FC = () => {
|
const Auth: React.FC = () => {
|
||||||
const [loginData, setLoginData] = useState<LoginDataInput>({
|
const [loginFormData, setLoginFormData] = useState<LoginDataInput>({
|
||||||
login: '',
|
login: '',
|
||||||
password: '',
|
password: '',
|
||||||
});
|
});
|
||||||
const { authStore } = useRootStore();
|
const { authStore } = useRootStore();
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
const handleLoginForm = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const { login, password } = loginData;
|
const { login, password } = loginFormData;
|
||||||
await authStore.login(login, password);
|
await authStore.login(login, password);
|
||||||
history.push('/');
|
history.push('/');
|
||||||
};
|
};
|
||||||
@ -54,7 +54,7 @@ const Auth: React.FC = () => {
|
|||||||
</Flex>
|
</Flex>
|
||||||
</Box>
|
</Box>
|
||||||
<Box my='30px'>
|
<Box my='30px'>
|
||||||
<form onSubmit={handleSubmit}>
|
<form onSubmit={handleLoginForm}>
|
||||||
<FormControl isRequired={true}>
|
<FormControl isRequired={true}>
|
||||||
<FormLabel>Login</FormLabel>
|
<FormLabel>Login</FormLabel>
|
||||||
<InputGroup>
|
<InputGroup>
|
||||||
@ -63,9 +63,12 @@ const Auth: React.FC = () => {
|
|||||||
type='text'
|
type='text'
|
||||||
name='login'
|
name='login'
|
||||||
placeholder='email@mail.com'
|
placeholder='email@mail.com'
|
||||||
value={loginData.login}
|
value={loginFormData.login}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
setLoginData({ ...loginData, login: e.target.value })
|
setLoginFormData({
|
||||||
|
...loginFormData,
|
||||||
|
login: e.target.value,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
@ -78,9 +81,12 @@ const Auth: React.FC = () => {
|
|||||||
type='password'
|
type='password'
|
||||||
name='password'
|
name='password'
|
||||||
placeholder='********'
|
placeholder='********'
|
||||||
value={loginData.password}
|
value={loginFormData.password}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
setLoginData({ ...loginData, password: e.target.value })
|
setLoginFormData({
|
||||||
|
...loginFormData,
|
||||||
|
password: e.target.value,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import api from '../axios/index';
|
import axios from 'axios';
|
||||||
|
import { API_BASE_URL } from '../constants';
|
||||||
import { IDataResult } from '../interfaces';
|
import { IDataResult } from '../interfaces';
|
||||||
|
|
||||||
interface ILoginResult extends IDataResult {
|
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 {
|
export default class AuthService {
|
||||||
public async requestLogin(
|
public async requestLogin(
|
||||||
login: string,
|
login: string,
|
||||||
|
|||||||
@ -1,9 +1,24 @@
|
|||||||
|
// import axios, { AxiosInstance } from 'axios';
|
||||||
|
// import { API_BASE_URL } from '../constants';
|
||||||
import AuthStore from './AuthStore';
|
import AuthStore from './AuthStore';
|
||||||
|
|
||||||
export default class RootStore {
|
export default class RootStore {
|
||||||
|
// public api: AxiosInstance;
|
||||||
|
|
||||||
public authStore: AuthStore;
|
public authStore: AuthStore;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
/**
|
||||||
|
* Api root instance
|
||||||
|
*/
|
||||||
|
// this.api = axios.create({
|
||||||
|
// baseURL: API_BASE_URL,
|
||||||
|
// responseType: 'json',
|
||||||
|
// });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auth store
|
||||||
|
*/
|
||||||
this.authStore = new AuthStore(this);
|
this.authStore = new AuthStore(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user