From 879eb0eb4f0f816fd1706fa9ce6f7e4bb817589f Mon Sep 17 00:00:00 2001 From: isra el Date: Mon, 17 Apr 2023 07:22:58 +0300 Subject: [PATCH] chore(web): use custom axios instance --- web/lib/axiosInstance.ts | 16 ++++++++++++++++ web/pages/index.tsx | 1 + web/pages/login.tsx | 2 +- web/services/index.ts | 27 ++++++++++----------------- web/services/types.ts | 4 +++- 5 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 web/lib/axiosInstance.ts diff --git a/web/lib/axiosInstance.ts b/web/lib/axiosInstance.ts new file mode 100644 index 0000000..2d4c6d7 --- /dev/null +++ b/web/lib/axiosInstance.ts @@ -0,0 +1,16 @@ +import axios from 'axios' +import { LOCAL_STORAGE_KEY } from '../shared/constants' + +const axiosInstance = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_BASE_URL, +}) + +axiosInstance.interceptors.request.use((config) => { + const token = localStorage.getItem(LOCAL_STORAGE_KEY.TOKEN) + if (token) { + config.headers.Authorization = `Bearer ${token}` + } + return config +}) + +export default axiosInstance diff --git a/web/pages/index.tsx b/web/pages/index.tsx index 6d6023d..9417636 100644 --- a/web/pages/index.tsx +++ b/web/pages/index.tsx @@ -27,6 +27,7 @@ export default function HomePage() { ) }, onError: () => {}, + disabled: !!user, }) return ( diff --git a/web/pages/login.tsx b/web/pages/login.tsx index 8db2dd0..9e46515 100644 --- a/web/pages/login.tsx +++ b/web/pages/login.tsx @@ -124,7 +124,7 @@ export default function LoginPage() { status: 'error', }) }} - useOneTap={true} + useOneTap={!authState.user} width='100%' /> diff --git a/web/services/index.ts b/web/services/index.ts index 7293f91..157e911 100644 --- a/web/services/index.ts +++ b/web/services/index.ts @@ -1,5 +1,4 @@ -import axios from 'axios' -import { LOCAL_STORAGE_KEY } from '../shared/constants' +import axiosInstance from '../lib/axiosInstance' import { GoogleLoginRequestPayload, LoginRequestPayload, @@ -8,51 +7,45 @@ import { RegisterResponse, SendSMSRequestPayload, } from './types' -const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL - -if (typeof localStorage !== 'undefined') - axios.defaults.headers.common[ - 'Authorization' - ] = `Bearer ${localStorage.getItem(LOCAL_STORAGE_KEY.TOKEN)}` export const loginRequest = async ( payload: LoginRequestPayload ): Promise => { - const res = await axios.post(`${BASE_URL}/auth/login`, payload) + const res = await axiosInstance.post(`/auth/login`, payload) return res.data.data } export const loginWithGoogleRequest = async ( payload: GoogleLoginRequestPayload ): Promise => { - const res = await axios.post(`${BASE_URL}/auth/google-login`, payload) + const res = await axiosInstance.post(`/auth/google-login`, payload) return res.data.data } export const registerRequest = async ( payload: RegisterRequestPayload ): Promise => { - const res = await axios.post(`${BASE_URL}/auth/register`, payload) + const res = await axiosInstance.post(`/auth/register`, payload) return res.data.data } export const generateApiKeyRequest = async () => { - const res = await axios.post(`${BASE_URL}/auth/api-keys`, {}) + const res = await axiosInstance.post(`/auth/api-keys`, {}) return res.data.data } export const getApiKeyListRequest = async () => { - const res = await axios.get(`${BASE_URL}/auth/api-keys`) + const res = await axiosInstance.get(`/auth/api-keys`) return res.data.data } export const deleteApiKeyRequest = async (id: string) => { - const res = await axios.delete(`${BASE_URL}/auth/api-keys/${id}`) + const res = await axiosInstance.delete(`/auth/api-keys/${id}`) return res.data.data } export const getDeviceListRequest = async () => { - const res = await axios.get(`${BASE_URL}/gateway/devices`) + const res = await axiosInstance.get(`/gateway/devices`) return res.data.data } @@ -60,8 +53,8 @@ export const sendSMSRequest = async ( deviceId: string, payload: SendSMSRequestPayload ) => { - const res = await axios.post( - `${BASE_URL}/gateway/devices/${deviceId}/sendSMS`, + const res = await axiosInstance.post( + `/gateway/devices/${deviceId}/sendSMS`, payload ) return res.data.data diff --git a/web/services/types.ts b/web/services/types.ts index a02a5bf..28b15a1 100644 --- a/web/services/types.ts +++ b/web/services/types.ts @@ -25,8 +25,10 @@ export interface GoogleLoginRequestPayload { idToken: string } -export interface RegisterRequestPayload extends LoginRequestPayload { +export interface RegisterRequestPayload { name: string + email: string + password: string } export interface BaseResponse {