fixed bug auth in useRequest

This commit is contained in:
Amirhossein Mahmoodi
2023-08-09 14:23:00 +03:30
parent c23672c86c
commit 65753c52ca

View File

@@ -6,6 +6,7 @@ import useUser from "@/lib/app/hooks/useUser";
import {errorRequest, errorResponse, errorSetting} from "@/core/utils/errorHandler";
const defaultOptions = {
auth: false,
data: {},
requestOptions: {
headers: {}
@@ -28,12 +29,16 @@ const useRequest = (initOptions) => {
const {token, clearToken} = useUser()
let _options = {...defaultOptions, ...initOptions}
function requestServer(url = '', method = 'get', auth = false, options) {
function requestServer(url = '', method = 'get', options) {
_options = {..._options, ...options}
if (auth) _options = {
if (_options.auth) _options = {
..._options,
requestOptions: {..._options.requestOptions.headers, authorization: `Bearer ${token}`}
requestOptions: {
..._options.requestOptions,
headers: {..._options.requestOptions.headers, authorization: `Bearer ${token}`}
}
}
return new Promise((resolve, reject) => {
if (_options.notification && _options.failed.notification.show && _options.pending) PendingNotification(t)
axios({