-
what's the correct way of getting the token from useAuth()? I've tried Due to data types I'm trying to use |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
update and use |
Beta Was this translation helpful? Give feedback.
-
thank you, I noticed this is fresh on 2.3.2 ;-) the following is a fully typed wrapper for useFetch(). but I'm a noob and a lot could be nonsense. export const useAPI = <
DataT = void,
ErrorT = FetchError,
_DataT = DataT extends void ? FetchResult<NitroFetchRequest> : DataT,
Transform extends (res: _DataT) => any = (res: _DataT) => _DataT,
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
>(
url: string,
opts: UseFetchOptions<_DataT, Transform, PickKeys> = {}
) => {
const { public: config } = useRuntimeConfig();
const Authorization = useAuth().tokenStrategy?.token?.get();
console.log("useAPI", Authorization);
if (typeof Authorization === "string") {
// headers
const headers = opts.headers || {};
opts.headers = {
Authorization,
Accept: "application/json",
...headers,
};
}
return useFetch<
DataT,
ErrorT,
NitroFetchRequest,
_DataT,
Transform,
PickKeys
>(url, {
baseURL: config.apiPrefix,
server: false,
...opts,
onResponseError({ response }) {
if (response.status === 401) {
redirectToLogin();
} else {
return Promise.reject(response);
}
},
});
}; |
Beta Was this translation helpful? Give feedback.
update and use
useAuth().tokenStrategy.token.get()
there isn't proper typing for this yet, im working on it.