I'm working with auth in Nextjs, I'm wondering what is the best strategy to handle authentication in NextJS ?
Here my services structure :
If I understand well I have to handle the server side rendering in NextJS, so I understand I have to put cookies from my external server to my NextJS client, then handle the server side rendering checkings. To do that I assume I have to create connection between the NextJS server and the other services. Before dive more deeper in the subject I would discuss with you about the possibilities available to me. It seems the NextJS auth is a subject in plain development.
Any hint would be great, Thanks
I've recently added an example with cookie auth which explains what you are trying to do on the frontend.
For the backend, optimally you'll have your API in an external server, apart from the server you use for rendering your Next.js app. This API will handle the database and the token creation business. Then the basics of the authentication are like this:
POST
a request with username and password to the server.{ token: "secrettoken" }
./dashboard
or /profile
if everything is okay.getInitialProps
. If the cookie validation fails you redirect the user away.I've created a small library to abstract this logic.
So in the end, your Next.js app shouldn't know what's happening in the server, it only should receive the token, save it, validate it, and redirect the user if something is wrong.
How you want to handle the token creation, on the external server, is up to you.
Check out this thread. There are several examples of how to do Authentication with JWT, OAuth etc throughout the thread. You'll see that the examples are utilizing getInitialProps
and there are several examples utilizing cookies throughout to extract the Authentication tokens.
You'll have to write a custom server.js
file using express.js
to serve the tokens through your route requests. I'm assuming by "external server" you mean some third party Authenticator using OAuth or OpenId protocols to retrieve tokens. If so, you're right to say that you'll need to request the tokens (or Authentication mechanism) from those external services and then decide how you're going to utilize them in your own client. You'll probably be using getInitialProps
to do what you need to do with your Authentication tokens in the client once you are rendering to the browser.
More examples of Authentication here -- one for firebase and another for Apollo.
Is is possible to debug (breakpoints, watches) client-side .ts typescript files from visual studio (2015) directly? Most of the related issues that I have found on google and stackoverflow suggest ...
Is is possible to debug (breakpoints, watches) client-side .ts typescript files from visual studio (2015) directly? Most of the related issues that I have found on google and stackoverflow suggest ...
I have created an image slider that has 3 images and 3 dots, each dot runs a function that loads an image, the first dot the first image etc. Now what I'm asking is how can I create a loop for the ...
I have created an image slider that has 3 images and 3 dots, each dot runs a function that loads an image, the first dot the first image etc. Now what I'm asking is how can I create a loop for the ...
I'm using following JS code to add extra information[LINK] to clipboard while copying the text from website: $(document).on('copy', function(e) { var sel = window....
I'm using following JS code to add extra information[LINK] to clipboard while copying the text from website: $(document).on('copy', function(e) { var sel = window....
I have found a few examples out there but having a hard time getting any to work, if anyone had a clearer or some insight would be much appreciated. var wrap = function() { var self = d3.select(this),...
I have found a few examples out there but having a hard time getting any to work, if anyone had a clearer or some insight would be much appreciated. var wrap = function() { var self = d3.select(this),...