add auth to useRequest
This commit is contained in:
@@ -7,7 +7,9 @@ import {errorRequest, errorResponse, errorSetting} from "@/core/utils/errorHandl
|
|||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
data: {},
|
data: {},
|
||||||
requestOptions: {},
|
requestOptions: {
|
||||||
|
headers: {}
|
||||||
|
},
|
||||||
notification: true,
|
notification: true,
|
||||||
pending: true,
|
pending: true,
|
||||||
success: {
|
success: {
|
||||||
@@ -23,11 +25,15 @@ const defaultOptions = {
|
|||||||
}
|
}
|
||||||
const useRequest = (initOptions) => {
|
const useRequest = (initOptions) => {
|
||||||
const t = useTranslations()
|
const t = useTranslations()
|
||||||
const {clearToken} = useUser()
|
const {token, clearToken} = useUser()
|
||||||
let _options = {...defaultOptions, ...initOptions}
|
let _options = {...defaultOptions, ...initOptions}
|
||||||
|
|
||||||
function requestServer(url = '', method = 'get', options) {
|
function requestServer(url = '', method = 'get', auth = false, options) {
|
||||||
_options = {..._options, ...options}
|
_options = {..._options, ...options}
|
||||||
|
if (auth) _options = {
|
||||||
|
..._options,
|
||||||
|
requestOptions: {..._options.requestOptions.headers, authorization: `Bearer ${token}`}
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (_options.notification && _options.failed.notification.show && _options.pending) PendingNotification(t)
|
if (_options.notification && _options.failed.notification.show && _options.pending) PendingNotification(t)
|
||||||
axios({
|
axios({
|
||||||
|
|||||||
Reference in New Issue
Block a user