<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: daiyan1998</title>
    <description>The latest articles on Forem by daiyan1998 (@daiyan1998).</description>
    <link>https://forem.com/daiyan1998</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1105681%2F3cd127fb-1101-4887-8567-e9cb8c70e79b.png</url>
      <title>Forem: daiyan1998</title>
      <link>https://forem.com/daiyan1998</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/daiyan1998"/>
    <language>en</language>
    <item>
      <title>How to Implement Redirect After Login in a Next.js Application (Redux,MERN)</title>
      <dc:creator>daiyan1998</dc:creator>
      <pubDate>Fri, 06 Sep 2024 05:51:28 +0000</pubDate>
      <link>https://forem.com/daiyan1998/how-to-implement-redirect-after-login-in-a-nextjs-application-reduxmern-3men</link>
      <guid>https://forem.com/daiyan1998/how-to-implement-redirect-after-login-in-a-nextjs-application-reduxmern-3men</guid>
      <description>&lt;p&gt;In many web applications, you want users to return to the page they were originally trying to access after logging in. For example, if a user tries to place an order but isn’t logged in, it’s common to redirect them to a login page. After successful authentication, you want to send them back to the original page—like an order screen. This blog will show you how to implement this functionality in a Next.js application using React hooks and query parameters.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Setting Up the Redirect Logic on Protected Pages&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In this example, we’ll use a &lt;code&gt;PlaceOrderScreen&lt;/code&gt; component where the user is required to log in before placing an order. We’ll check whether the user is logged in by looking at the &lt;code&gt;userInfo&lt;/code&gt; object from the Redux store. If the user is not logged in, we’ll redirect them to the login page, passing the current URL as a query parameter.&lt;/p&gt;

&lt;p&gt;Here's the key code to achieve this in &lt;code&gt;PlaceOrderScreen&lt;/code&gt;:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { usePathname} from  "next/navigation";

&lt;p&gt;const PlaceOrderScreen = () =&amp;gt; {&lt;/p&gt;

&lt;p&gt;const router = useRouter();&lt;/p&gt;

&lt;p&gt;const { userInfo } = useSelector((state) =&amp;gt; state.auth); // Get user info&lt;/p&gt;

&lt;p&gt;const pathName = usePathname(); // Get current URL&lt;/p&gt;

&lt;p&gt;const placeOrderHandler = async () =&amp;gt; {&lt;/p&gt;

&lt;p&gt;if (!userInfo) router.push(&lt;code&gt;/login?redirect=${pathName}&lt;/code&gt;); // Redirect to login&lt;/p&gt;

&lt;p&gt;// Rest of the order logic...&lt;/p&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;return (/* UI Code */);&lt;/p&gt;

&lt;p&gt;};&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;strong&gt;Key Points:&lt;/strong&gt;&lt;br&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;useRouter&lt;/strong&gt; and &lt;strong&gt;usePathname&lt;/strong&gt;: These hooks help us capture the current URL (&lt;code&gt;pathName&lt;/code&gt;) and handle redirections (&lt;code&gt;router.push()&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Redirect with Query Parameter&lt;/strong&gt;: When the user is not logged in, we use &lt;code&gt;router.push(&lt;/code&gt;/login?redirect=${pathName}&lt;code&gt;)&lt;/code&gt; to redirect them to the login page and append the current page's URL as a query parameter (&lt;code&gt;redirect&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;2. Handling the Redirect After Login&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Once the user is successfully authenticated, they should be redirected back to the original page they were trying to access (e.g., &lt;code&gt;PlaceOrderScreen&lt;/code&gt;). We achieve this by retrieving the &lt;code&gt;redirect&lt;/code&gt; query parameter on the login screen and using it to navigate back to the original page.&lt;/p&gt;

&lt;p&gt;Here’s the key code in your &lt;code&gt;LoginScreen&lt;/code&gt;:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { usePathname, useRouter } from  "next/navigation";

&lt;p&gt;export default function LogInScreen() {&lt;/p&gt;

&lt;p&gt;const router = useRouter();&lt;/p&gt;

&lt;p&gt;const searchParams = useSearchParams(); // Get query parameters&lt;/p&gt;

&lt;p&gt;const { userInfo } = useSelector((state) =&amp;gt; state.auth);&lt;/p&gt;

&lt;p&gt;React.useEffect(() =&amp;gt; {&lt;/p&gt;

&lt;p&gt;if (userInfo) return router.push(searchParams.get("redirect") || "/"); // Redirect after login&lt;/p&gt;

&lt;p&gt;}, [userInfo]);&lt;/p&gt;

&lt;p&gt;const handleSubmit = async (event) =&amp;gt; {&lt;/p&gt;

&lt;p&gt;event.preventDefault();&lt;/p&gt;

&lt;p&gt;// Handle form submission and login logic...&lt;/p&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;return (/* UI Code */);&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;strong&gt;Key Points:&lt;/strong&gt;&lt;br&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;useSearchParams&lt;/strong&gt;: This hook allows us to access query parameters in the URL, like &lt;code&gt;redirect&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;React.useEffect&lt;/strong&gt;: After the user logs in (i.e., &lt;code&gt;userInfo&lt;/code&gt; is available), we check if the &lt;code&gt;redirect&lt;/code&gt; query parameter exists. If it does, the user is redirected to that page; otherwise, they are sent to the homepage (&lt;code&gt;"/"&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;3. How the Flow Works&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;User Tries to Place an Order:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If the user is not logged in, they are redirected to the login page with the current URL as a query parameter (e.g., &lt;code&gt;/login?redirect=/place-order&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;User Logs In:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;On the &lt;code&gt;LoginScreen&lt;/code&gt;, we retrieve the &lt;code&gt;redirect&lt;/code&gt; parameter and redirect the user back to that page after successful authentication.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;User is Returned to the Original Page:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;The user is seamlessly redirected back to the &lt;code&gt;PlaceOrderScreen&lt;/code&gt; (or any page they were originally trying to access).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Implementing a post-login redirect flow in Next.js involves passing the original page URL as a query parameter during login redirects and handling that parameter after the user authenticates. By using &lt;code&gt;useRouter&lt;/code&gt; and &lt;code&gt;useSearchParams&lt;/code&gt; effectively, you can create a smooth experience where users are returned to where they left off after logging in.&lt;/p&gt;

&lt;p&gt;This approach can be applied to other scenarios where users need to authenticate before accessing a page, ensuring a seamless user experience across your Next.js app.&lt;/p&gt;




&lt;p&gt;With this method, your application provides a more intuitive and polished experience, keeping users on track even when login is required mid-process.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Resolve Cookies Not Being Stored in the Browser: A Redux Configuration Fix (MERN &amp; Next.js)</title>
      <dc:creator>daiyan1998</dc:creator>
      <pubDate>Tue, 20 Aug 2024 20:07:04 +0000</pubDate>
      <link>https://forem.com/daiyan1998/how-to-resolve-cookies-not-being-stored-in-the-browser-a-redux-configuration-fix-mern-nextjs-2h6j</link>
      <guid>https://forem.com/daiyan1998/how-to-resolve-cookies-not-being-stored-in-the-browser-a-redux-configuration-fix-mern-nextjs-2h6j</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;When implementing a project based on Brad Traversy's MERN stack eCommerce tutorial with Next.js, I encountered an issue with cookie storage. The tutorial's &lt;code&gt;fetchBaseQuery&lt;/code&gt; configuration did not include &lt;code&gt;credentials&lt;/code&gt;, but I discovered that adding &lt;code&gt;credentials: "include"&lt;/code&gt; was essential for Next.js to store cookies correctly.&lt;/p&gt;

&lt;p&gt;Managing cookies is essential for user sessions and authentication in web applications. However, developers may encounter issues where cookies are not stored in the browser, despite being sent from the server. In this article, we'll explore a real-world scenario where a misconfiguration in Redux led to cookies not being stored and how the issue was resolved.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Issue Description :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cookies were visible in the Network tab under &lt;code&gt;Set-Cookie&lt;/code&gt; headers but were not being stored in the browser. This issue occurred in a web application built with Next.js, MongoDB, and Express.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initial Setup :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Frontend:&lt;/strong&gt; Next.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Backend:&lt;/strong&gt; Express&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Database:&lt;/strong&gt; MongoDB&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Initial Findings :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After verifying the configurations in Next.js, MongoDB, and Express, it was clear that these were set up correctly. The problem seemed to lie elsewhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  Diagnosing the Issue
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Observations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookies were visible in the browser's Network tab under &lt;code&gt;Headers &amp;gt; Set-Cookie&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;However, they were not being stored or accessible in the browser.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Initial Troubleshooting Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Checked cookie settings in Express (e.g., &lt;code&gt;secure&lt;/code&gt;, &lt;code&gt;httpOnly&lt;/code&gt;, &lt;code&gt;sameSite&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verified CORS configuration to ensure credentials were allowed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reviewed Next.js API routes for proper cookie handling.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Root Cause
&lt;/h3&gt;

&lt;p&gt;Upon closer inspection, the issue was identified in the Redux configuration. Specifically, the &lt;code&gt;fetchBaseQuery&lt;/code&gt; configuration did not include the necessary credentials setting.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Solution
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Locate the Configuration:&lt;/strong&gt; The problematic configuration was found in &lt;code&gt;frontend/src/slices/apiSlice.js&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Update &lt;code&gt;_fetchBaseQuery_&lt;/code&gt; : The credentials: "include" option was added to the &lt;em&gt;fetchBaseQuery&lt;/em&gt; configuration to ensure that cookies are included in cross-origin requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { BASE_URL } from "@/constants.js";
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

const baseQuery = fetchBaseQuery({ baseUrl: BASE_URL, credentials: "include" });

export const apiSlice = createApi({
  baseQuery,
  tagTypes: ["Product", "Order", "User"],
  endpoints: (builder) =&amp;gt; ({}),
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3*&lt;em&gt;. Verify the Fix :&lt;/em&gt;* After making the change, cookies were successfully stored in the browser and were accessible for subsequent requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Properly managing cookies is crucial for ensuring session and authentication functionalities in web applications. In this case, the issue of cookies not being stored was traced to an incorrect Redux configuration. By adding &lt;code&gt;credentials: "include"&lt;/code&gt; to the &lt;code&gt;fetchBaseQuery&lt;/code&gt; setup, the problem was resolved, allowing cookies to be stored and utilized effectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional Tips:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Always ensure that &lt;code&gt;credentials&lt;/code&gt; are configured correctly for cross-origin requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use browser developer tools to inspect cookies and network requests to troubleshoot issues.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>redux</category>
      <category>nextjs</category>
      <category>react</category>
      <category>express</category>
    </item>
  </channel>
</rss>
