add auth to useRequest
This commit is contained in:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user