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

SSR: Restricting Access to Specific Pages Based on Authenticated User #36427

Open
2 tasks done
karlhorky opened this issue Aug 20, 2022 · 6 comments
Open
2 tasks done
Labels
help wanted Issue with a clear description that the community can help with. not stale type: documentation An issue or pull request for improving or updating Gatsby's documentation

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Aug 20, 2022

Preliminary Checks

Summary

@KyleAMathews asked for this issue: #1100 (comment)


A guide about how to perform simple authentication and authorization in Server-Side Rendering (SSR) using getServerData would be great!

I would suggest that it include the following:

  1. A page and related static images that are fully public (no authentication required) - eg. a login page
  2. A page and related static images that are accessible only to any logged-in user - eg. an index page
  3. A page and related static images that are accessible only to any admin user - eg. an admin dashboard page

Further background information / motivation:

Steps to Resolve this Issue

  1. Look for a guide for authentication / authorization for SSR in the Gatsby docs
  2. Fail in finding such a guide
@karlhorky karlhorky added the type: documentation An issue or pull request for improving or updating Gatsby's documentation label Aug 20, 2022
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Aug 20, 2022
@tyhopp tyhopp added help wanted Issue with a clear description that the community can help with. and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Aug 23, 2022
@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 12, 2022
@karlhorky
Copy link
Contributor Author

@KyleAMathews since you requested this, do you think you can disable the stale bot?

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 13, 2022
@github-actions
Copy link

github-actions bot commented Oct 3, 2022

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Oct 3, 2022
@karlhorky
Copy link
Contributor Author

@LekoArts can you disable the stale bot here? @KyleAMathews requested that I create this issue.

@pieh pieh added the not stale label Oct 3, 2022
@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Oct 3, 2022
@iamalexm
Copy link

I'd love a documentation about this too!

@kdichev
Copy link
Contributor

kdichev commented Dec 10, 2022

Hi guys I've recently tried to implement this feature on my SSR app routes and I've done it like so:

// routes
index.tsx
sign-in.tsx
sign-up.tsx
./dashboard/[...].tsx
// ./dashboard/[...].tsx
export const getServerData = async () => {
  const { access_token } = headers.get('cookie')
  const user = await getProfile({ auth: access_token })
  if (!user) {
    return {
      status: 301,
      headers: {
        Location: '/'
      }
    }
  }
  return {
    props: { user, access_token }
  }
}

I also have a function that I call when access_token is available from my auth package and set a cookie. Seems to be working alright.

// api/session
export default function sessionAPI(req: Req, res: Res) {
  const { access_token, expires_in }= JSON.parse(req.body);
  if (!access_token|| !expires_in ) {
    res.status(401).json({
      message: "Unauthorized",
    });
  } else {
    res.setHeader(
      "Set-Cookie",
      `access_token=${accessToken};Secure=true;HttpOnly=true;Path=/`
    );
    res.status(200).json({ message: "session created successfully" });
  }
}

// setter function when auth token is available from auth package
export const setSessionToken = async (token) =>
  fetch("/api/session", {
    method: "POST",
    body: JSON.stringify(token),
  });

Obviously I don't have much experience implementing ssr auth so I might be doing it wrong but also there are some issues with this setup:

  1. redirect does not work in gatsby dev (it matches to 404 always) so I have to build and serve to test my code
  2. cookies are not parsed, so I have to use a parser package

p.p let me know if you see if I can enhance my setup if it's not totally right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue with a clear description that the community can help with. not stale type: documentation An issue or pull request for improving or updating Gatsby's documentation
Projects
None yet
Development

No branches or pull requests

5 participants