From c23672c86c54733405091d82ea6d4e569ca334ca Mon Sep 17 00:00:00 2001 From: Amirhossein Mahmoodi Date: Wed, 9 Aug 2023 14:05:08 +0330 Subject: [PATCH] add auth to useRequest --- src/lib/app/hooks/useRequest.jsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/app/hooks/useRequest.jsx b/src/lib/app/hooks/useRequest.jsx index f7db024..a4ccec5 100644 --- a/src/lib/app/hooks/useRequest.jsx +++ b/src/lib/app/hooks/useRequest.jsx @@ -7,7 +7,9 @@ import {errorRequest, errorResponse, errorSetting} from "@/core/utils/errorHandl const defaultOptions = { data: {}, - requestOptions: {}, + requestOptions: { + headers: {} + }, notification: true, pending: true, success: { @@ -23,11 +25,15 @@ const defaultOptions = { } const useRequest = (initOptions) => { const t = useTranslations() - const {clearToken} = useUser() + const {token, clearToken} = useUser() let _options = {...defaultOptions, ...initOptions} - function requestServer(url = '', method = 'get', options) { + function requestServer(url = '', method = 'get', auth = false, options) { _options = {..._options, ...options} + if (auth) _options = { + ..._options, + requestOptions: {..._options.requestOptions.headers, authorization: `Bearer ${token}`} + } return new Promise((resolve, reject) => { if (_options.notification && _options.failed.notification.show && _options.pending) PendingNotification(t) axios({