Next.js Discord

Discord Forum

How to setup static site export with route handlers for auth.

Unanswered
Oriental Scops-Owl posted this in #help-forum
Open in Discord
Oriental Scops-OwlOP
So i want to know if it's possible to use nextjs with pure client side auth.
I heard that api routes are for server auth and route handlers can use Web requests for its auth. I am wondering how this all fits in and if someone has a demo project I can look at. Or just some pointers in the right direction. Atm the site will just be SPA with static pages for some SEO. I have a different api all together for my app data. So thus I don't really want to rely on a server side render.

26 Replies

You could do the auth within a proxy server is the site is static
basically doing it before the request even reaches Next
or your exported HTML files in this case
client-side auth is not possible, there is no security in a client
this is an example of basic auth in a middleware
so if you export the app, you will not have middleware but you can rather use what your host provide
Next.js middlewares are equivalent to Vercel Edge Handlers
or Netlify Edge Functions
or AWS Route 53
basically any kind of proxy server that can do authentication check why staying lightweight and cheap enough
this is for checking if the user is authenticated, but for the actual login process I am pretty sure you'll need to have a server around, unless you use a cloud service to achieve that
@Anay-208 Why would you use client side auth?
Oriental Scops-OwlOP
I mean authentication via aws. Calling aws from the client. Receiving a jwt token.
@Oriental Scops-Owl I mean authentication via aws. Calling aws from the client. Receiving a jwt token.
Yeah but then when do you check this token validity ?
@Eric Burel Yeah but then when do you check this token validity ?
Oriental Scops-OwlOP
It would be per request no?
The setup of such tool often supposes having an API endpoint in your own app
@Eric Burel The setup of such tool often supposes having an API endpoint in your own app
Oriental Scops-OwlOP
Yes. But what I don't understand is why do I need to call my server when I'm calling cognito for authentication or in my case, account creation
from their standpoint your app is their client
and often the documentation will implicitely tell you to setup stuff that can actually only be setup from a server
it's not always explicit, but for instance if it talks about "Authorization" header, it's about server-server communication, while client uses cookies instead
I don't know Cognito specifically so I can't tell but that's the idea
but it depends on the tool you pick, you have to give it a try or look at the doc, perhaps they even have Next.js specific examples