У нас вы можете посмотреть бесплатно How to Use useSWR with Access Token from Next.js Session или скачать в максимальном доступном качестве, видео которое было загружено на ютуб. Для загрузки выберите вариант из формы ниже:
Если кнопки скачивания не
загрузились
НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием видео, пожалуйста напишите в поддержку по адресу внизу
страницы.
Спасибо за использование сервиса ClipSaver.ru
Learn how to seamlessly integrate SWR for data fetching in Next.js while using access tokens from session management with NextAuth and Django backend. --- This video is based on the question https://stackoverflow.com/q/75204909/ asked by the user 'JHNT' ( https://stackoverflow.com/u/17743932/ ) and on the answer https://stackoverflow.com/a/75205016/ provided by the user 'Ahmed Sbai' ( https://stackoverflow.com/u/13488990/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: NextJS - useSWR with token from session Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Using useSWR with Access Token from Next.js Session When developing applications with Next.js and NextAuth, one common challenge developers face is how to securely fetch data from a backend using an access token stored in the session. This need arises particularly when you're working with authentication systems, such as verifying users against a Django backend. In this guide, we will explore how to efficiently manage session tokens while using the useSWR hook for data fetching. Understanding the Problem In your Next.js app, after a user successfully logs in, the access token received from the backend is stored in the session. The goal is to include this token in the Authorization header for every request made to the backend using useSWR. However, developers often encounter issues when trying to handle the session status efficiently. The error "Rendered more hooks than during the previous render" is particularly vexing. Why This Error Occurs The mentioned error typically occurs due to conditional usage of hooks based on session status which leads to different numbers of hooks being rendered in different scenarios. This can occur if you try to render a component that uses useSWR conditionally based on the session's status. Solution Overview The solution to this problem involves removing any conditional rendering of hooks and centralizing the authentication logic. Below are the detailed steps to implement this effectively in your application. Step 1: Creating an Authentication Component First, you need to create an authentication wrapper component that will handle the session management logic. This component will ensure that every page requiring a session is wrapped appropriately. Here's how to do that: [[See Video to Reveal this Text or Code Snippet]] Step 2: Implementing Auth Wrapper in Pages Once you have the Auth component, you must ensure that each page requiring authentication is flagged appropriately. You do this by adding an auth property to each page component: [[See Video to Reveal this Text or Code Snippet]] Step 3: Update the _app.js File Lastly, you need to update your _app.js file to wrap any authenticated pages with the Auth component. Change the App component as follows: [[See Video to Reveal this Text or Code Snippet]] Step 4: Simplifying the Data Fetching Logic Now that you have your authentication logic simplified, you can go back to your Profile component and remove any session state checks. You can make your requests directly since the Auth wrapper guarantees that a session will be available: [[See Video to Reveal this Text or Code Snippet]] Conclusion By following the above steps, you can efficiently use useSWR to fetch protected data from the backend without running into rendering issues. The key is to condense authentication logic into a single component (Auth) that standardizes session management across your application. Now you're ready to retrieve data securely from your backend with access tokens in a clean, manageable way! If you encounter any difficulties along the way, feel free to refer back to these steps or reach out for further assistance.