-
Notifications
You must be signed in to change notification settings - Fork 86
Netlify Identity support in SSR #107
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
Comments
Having the same issue, glad to see you posted this. To add the matter.
|
@iRyanBell I came up with this workaround HOF in the meantime. Honestly I've had a terrible time using the netlify JS libraries. After wasting about a week trying to get identity working with the various libraries, I've resorted to a semi-bespoke solution. When you log a user in via identity, you need to set a cookie When you log them out, clear this. Then use the following HOF to provide server side authentication to all your pages const withNetlifySession = (innerHandler) => {
return async ({ req, res }) => {
const token = req?.cookies?.[`nf-jwt`];
let user;
try {
const userRes = await fetch(`${url}/.netlify/identity/user`, {
headers: {
authorization: `Bearer ${token}`,
},
});
user = await userRes.json();
} catch (e) {
console.error(e);
}
return innerHandler({ req, res, user });
};
}; This allows you to access export const getServerSideProps = withNetlifySession(
async ({ user }: { user: User }) => {
if (!user) {
return {
redirect: {
destination: `/login`,
permanent: true,
},
};
}
return {
props: {
user,
},
};
},
); This solution is hacky because netlify seems to set a |
@dan-cooke It is tempting to just grab some off-the-shelf firebase auth, and keep on coding. I've thought about using the ServerSideProps to call a lambda to get the data, but I don't like the idea of needing these two calls. What I'm going to roll with for now is skipping the SSR auth, and just running the frontend updates in an Effect hook with state from |
@iRyanBell I considered the same thing honestly. But I found that the useEffects for redirecting the user in various scenarios can become quite complex and difficult to mantain. And in some cases, you don't want the user to even land on the page at all if they are not authenticated. This will result in a flash of skeleton text, then reroutes. Server side authentication just seems like a cleaner solution and will provide better UX in the end. I think my solution above is pretty much what this plugin is doing under the hood anyway. So until they fix this issue, I will continue using this. |
On the unauthenticated redirect front, I was looking at this as a very cool way to set that up declaratively: But, it's a |
I stumbled upon this after reading through netlify/next-on-netlify#20 it seems like this used to be supported in the older version of the plugin but is not anymore? At least |
As pointed out by other folks in this issue, this functionality is missing between this newer iteration of the NextJS plugin and the previous one ( You can read more in the section below on the context of why this functionality doesn't exist within this iteration of the plugin, but other than what was suggested as a workaround by @dan-cooke (thank you for providing that!) it's also possible that you could get the The object that's encoded as the As a result, you could decode the token within The only difference between this approach and what you would receive from directly querying the Identity service is verification that the JWT is valid because Netlify doesn't give the JWT secret to folks using the Identity service. Sorry that this answer took so long in getting to you. Hopefully this workaround helps, and if you (or others) have additional questions, feel free to reach out to me here. Additional background/context@tbgse is correct in that the older version of the plugin used to support this. To give some background on the previous state of things and why we don't currently have the same functionality in this iteration of the plugin: The Next.js server expects to be run through an Express app, rather than via a lambda event, so we (Netlify) needed some kind of compatibility layer to bridge between them. In the old plugin, we used a custom compatibility layer which is how we could attach the extra props that you see in the related documentation for Netlify Identity in the old plugin README. In this iteration of the plugin, we’re instead using the same compatibility layer that Vercel uses. As a result, we lost the ability to modify the request object ourselves, but no longer need to worry about keeping it compatible which we felt was a worthwhile tradeoff. Since compatibility is the main goal of the plugin, whenever possible we’ve aimed to use Vercel’s implementation of things, which is why we are where we are today with the difference in Netlify Identity functionality between the previous NextJS plugin ( |
Describe the bug
Inside a Next.js project, after logging in with the
netlify-identity-widget
, the request'sclientContext
should provide service-side access to the user's authentication state.To Reproduce
Steps to reproduce the behavior:
plugin-nextjs
netlify-identity-widget
Expected behavior
The
user
object in the clientContext is undefined after signing in with the Netlify Identity Widget and visiting the page.On the client side, I can confirm that the user is authenticated from the
netlifyIdentity.on('init', callback)
handler.I'm not sure if I'm missing a simple build configuration setting or environment variable to access this value from the Identity service as described here: https://github.com/netlify/next-on-netlify#using-netlify-identity
Versions
If you're using the CLI to build
If you're using file-based installation
Netlify Plan
The text was updated successfully, but these errors were encountered: