Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deny direct access to specific pages #5697

Closed
skjorrface opened this issue Jun 4, 2018 · 4 comments
Closed

Deny direct access to specific pages #5697

skjorrface opened this issue Jun 4, 2018 · 4 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@skjorrface
Copy link

This is more a question than a gatsby related issue. Is there a way to restrict direct access to a page (for instance: /app/download) and to make it available only from other paths inside the website? Thanks beforehand!

@ryanditjia
Copy link
Contributor

This issue talks about how you can pass data using navigateTo API.

src/pages/index.js

import React from 'react'
import Link, { navigateTo } from 'gatsby-link'

const IndexPage = () => (
  <div>
    <Link to="/secret-page">Regular link</Link>
    <button
      onClick={() =>
        navigateTo({
          pathname: '/secret-page',
          state: {
            showPage: true,
          },
        })
      }
    >
      Super secret link
    </button>
  </div>
)

export default IndexPage

src/pages/secret-page.js

import React from 'react'
import Link from 'gatsby-link'

const SecretPage = ({ location }) => {
  const { showPage } = location.state || false

  return showPage ? (
    <div>You have reached a secret page.</div>
  ) : (
    <div>You can’t access this secret page.</div>
  )
}

export default SecretPage

@pieh pieh added the type: question or discussion Issue discussing or asking a question about Gatsby label Jun 4, 2018
@skjorrface
Copy link
Author

Oh thank you so much, this works like a charm! Any hint on how to do this for a static file as well?

@ryanditjia
Copy link
Contributor

Hmm, I have no experience with that. Perhaps it requires something like authentication.

@tsegaat
Copy link

tsegaat commented Jan 22, 2022

Thank you so much. I have been trying to do this the whole day... Looked at several YT vids and documentation but your way was just perfect for my project.

BTW: instead of "navigateTo" imported from gatsby-link. "navigate" from "gatsby" worked for me. If anyone got that error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

4 participants