JavaScript NextJS auth with an external server next js auth,next js with auth,next js authentication,

I'm working with auth in Nextjs, I'm wondering what is the best strategy to handle authentication in NextJS ?

Here my services structure :

enter image description here

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

Answer:1

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:

  • The client POST a request with username and password to the server.
  • The server gets the request and generate a token based on the data received.
  • If everything went okay validating the data, the server responds with the token, e.g., { token: "secrettoken" }.
  • The client receives the token and saves it in a cookie. Optionally you redirect the user to the /dashboard or /profile if everything is okay.
  • The client, on restricted pages, will check if the cookie exists and optionally validate that against the server, you do this last part in 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.

Answer:2

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.

Answer:3

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 ...

  1. node typescript http client
  2. node-rest-client typescript
  3. node-zookeeper-client typescript
  4. node.js typescript rest client
  5. node-event store-client typescript

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 ...

  1. looping through htmlcollection
  2. looping through html elements javascript
  3. looping through html table jquery
  4. looping through html headers
  5. looping through html table
  6. loop through html elements
  7. loop through html table rows jquery
  8. loop through html list javascript
  9. loop through html elements jquery
  10. loop through html table javascript
  11. loop through html table in c#
  12. loop through html table rows javascript
  13. javascript loop through htmlcollection
  14. loop through html
  15. loop through html table in php
  16. loop through html table rows c#
  17. loop through html select options
  18. js loop through htmlcollection
  19. loop through html elements c#
  20. loop through html table vba

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....

  1. text formatting include

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),...

  1. ellipsis long text css
  2. long text ellipsis
  3. javascript ellipsis for long text
  4. html ellipsis for long text
  5. ellipsis for long text css