-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Comments
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. Thanks for being a part of the Gatsby community! 💪💜 |
@KyleAMathews since you requested this, do you think you can disable the stale bot? |
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. Thanks for being a part of the Gatsby community! 💪💜 |
@LekoArts can you disable the stale bot here? @KyleAMathews requested that I create this issue. |
I'd love a documentation about this too! |
Hi guys I've recently tried to implement this feature on my SSR app routes and I've done it like so:
// ./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 // 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:
p.p let me know if you see if I can enhance my setup if it's not totally right |
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:
Further background information / motivation:
Steps to Resolve this Issue
The text was updated successfully, but these errors were encountered: