اذهب إلى المحتوى
  • 0

إعداد axios instance في next.js

عبد النور محمد

السؤال

مرحبا أحاول اعداد  axios واستدعاءه لاستخدامه في كل طلباتي لكن الجزء الخاص بوضع ال token authorization لا يعمل 

import axios from 'axios';

const Axios = axios.create({
  baseURL: process.env.NEXT_PUBLIC_API,
});

let token;
if (window) {
  token = JSON.parse(localStorage.getItem('zu')).state?.auth?.token;
}

Axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;

export default Axios;

هل من اقتراح

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

جرب استخدام process.browser محل window

import axios from 'axios';

const Axios = axios.create({
  baseURL: process.env.NEXT_PUBLIC_API,
});

let token;
if (process.browser) {
  token = JSON.parse(localStorage.getItem('zu')).state?.auth?.token;
}

Axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;

export default Axios;

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...