<?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>DEV Community: Purnima</title>
    <description>The latest articles on DEV Community by Purnima (@dhammapurnima).</description>
    <link>https://dev.to/dhammapurnima</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%2F108451%2F42216a49-5c22-4b00-b094-a4f5041af393.jpeg</url>
      <title>DEV Community: Purnima</title>
      <link>https://dev.to/dhammapurnima</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhammapurnima"/>
    <language>en</language>
    <item>
      <title>Gatsby Private Route not working as intended</title>
      <dc:creator>Purnima</dc:creator>
      <pubDate>Thu, 10 Jun 2021 16:02:18 +0000</pubDate>
      <link>https://dev.to/dhammapurnima/gatsby-private-route-not-working-as-intended-2cd3</link>
      <guid>https://dev.to/dhammapurnima/gatsby-private-route-not-working-as-intended-2cd3</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;I'm trying to implement a blog with gated content using Gatsby as the front-end and Strapi as the CMS for authentication. Have been following the instructions from the official Gatsby docs on &lt;a href="https://www.gatsbyjs.com/docs/how-to/routing/client-only-routes-and-user-authentication"&gt;client-only routes and user authentication&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I want to make the following URLs private:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/app/articles&lt;/code&gt; : This is the articles list page&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/articles/:slug&lt;/code&gt;: URL for individual article page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If an unauthenticated user visits the above 2 URLs, they should be redirected to &lt;code&gt;/app/login&lt;/code&gt; page.&lt;/p&gt;

&lt;p&gt;The issue I'm facing here is, the Private route seems to work &lt;strong&gt;only&lt;/strong&gt; for &lt;code&gt;/app/articles&lt;/code&gt; (the unauthenticated user gets redirected to &lt;code&gt;/app/login&lt;/code&gt;), but not for &lt;code&gt;app/articles/:slug&lt;/code&gt; - the page displays the complete article even when the user is unauthenticated.&lt;/p&gt;

&lt;p&gt;All the pages under both the URLs are prebuilt when I run the gatsby-server so that I can take advantage of static-site performance. I want to just prevent a user from seeing the content if they aren't logged in.&lt;/p&gt;

&lt;p&gt;Can you please help me understand if I'm doing something wrong? Here is my &lt;a href="https://github.com/purnimagupta/Gatbsy-Authentication-With-Strapi"&gt;code&lt;/a&gt; to get the complete picture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File: &lt;code&gt;/src/pages/app.js&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Index = ({location, history}) =&amp;gt; {
    return(
        &amp;lt;Layout&amp;gt; 
            &amp;lt;Logout/&amp;gt;
            &amp;lt;Router&amp;gt;
                &amp;lt;PrivateRoute path="/app/articles/:slug" component={Article} /&amp;gt;
                &amp;lt;PrivateRoute exact path="/app/articles" component={Home}/&amp;gt;
                &amp;lt;Login path="/app/login" location={location} history={history}/&amp;gt;
                &amp;lt;Signup path="/app/signup" /&amp;gt;
            &amp;lt;/Router&amp;gt;
        &amp;lt;/Layout&amp;gt;
    )
}
export default Index;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Individual Article pages is being generated using gatsby &lt;code&gt;createPages&lt;/code&gt;  API inside &lt;code&gt;gatsby-node.js&lt;/code&gt;. And the template resides inside &lt;code&gt;/src/templates/article.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File: &lt;code&gt;gatsby-node.js&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.onCreatePage = async ({ page, actions }) =&amp;gt; {
    const { createPage } = actions
    if (page.path.match(/^\/app/)) {
        console.log("matched app", page.path.match(/^\/app/))
        page.matchPath = "/app/*"
        // Update the page.
        createPage(page)
    }
}

exports.createPages = async function ({ actions, graphql }) {
    const { data } = await graphql(`
        query {
            allStrapiArticle {
                edges {
                    node {
                        slug
                    }
                }
            }
        }      
    `)

    data.allStrapiArticle.edges.forEach(article =&amp;gt; {
        const slug = article.node.slug;
        console.log("node.slug", article.node.slug);
        actions.createPage({
            path: `/app/articles/${slug}`,
            component: require.resolve(`./src/templates/article.js`),
            context: { slug: slug },
        })
    })

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

&lt;/div&gt;



&lt;p&gt;Thank you!&lt;/p&gt;

</description>
      <category>help</category>
      <category>gatsbyprivateroute</category>
    </item>
  </channel>
</rss>
