From 16613a22a79248c7b6fd02c2deae550fd6be1a70 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Thu, 19 Aug 2021 13:52:36 +0300 Subject: [PATCH] feat: init auth service --- src/axios/index.ts | 10 ---------- src/components/Auth/Auth.tsx | 22 ++++++++++++++-------- src/services/AuthService.ts | 8 +++++++- src/stores/RootStore.ts | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 19 deletions(-) delete mode 100644 src/axios/index.ts diff --git a/src/axios/index.ts b/src/axios/index.ts deleted file mode 100644 index e792de1..0000000 --- a/src/axios/index.ts +++ /dev/null @@ -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', -}); diff --git a/src/components/Auth/Auth.tsx b/src/components/Auth/Auth.tsx index 924bcf2..e955afc 100644 --- a/src/components/Auth/Auth.tsx +++ b/src/components/Auth/Auth.tsx @@ -24,17 +24,17 @@ type LoginDataInput = { * Authorize user through a form. */ const Auth: React.FC = () => { - const [loginData, setLoginData] = useState({ + const [loginFormData, setLoginFormData] = useState({ login: '', password: '', }); const { authStore } = useRootStore(); const history = useHistory(); - const handleSubmit = async (e: React.FormEvent) => { + const handleLoginForm = async (e: React.FormEvent) => { 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 = () => { -
+ Login @@ -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, + }) } /> @@ -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, + }) } /> diff --git a/src/services/AuthService.ts b/src/services/AuthService.ts index 13d01b7..ceac2e3 100644 --- a/src/services/AuthService.ts +++ b/src/services/AuthService.ts @@ -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, diff --git a/src/stores/RootStore.ts b/src/stores/RootStore.ts index b58e4b4..54a6d01 100644 --- a/src/stores/RootStore.ts +++ b/src/stores/RootStore.ts @@ -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); } }