Next.js Discord

Discord Forum

Next-Auth on next js while I have a separated backend

Answered
Scaly-naped Pigeon posted this in #help-forum
Open in Discord
Avatar
Scaly-naped PigeonOP
I am using next js as a frontend while I am uisng bun js as backend , I want to handle authentication can I use next-auth as frontend while prisma on my backend ( bun.js ) ? can this be safe or is there a better approach or should I install next-auth on bun js , what is the best approach ?

also can I add more functionality to google providers ?
Answered by Alfonsus Ardani
One way of doing it is by using Next.js backend as the middleman between your front end and your back end. You can get the user's session and validate it via await getServerSession() or await auth() (if using auth.js) before hitting your real backend.
View full answer

5 Replies

Avatar
One way of doing it is by using Next.js backend as the middleman between your front end and your back end. You can get the user's session and validate it via await getServerSession() or await auth() (if using auth.js) before hitting your real backend.
Answer
Avatar
Scaly-naped PigeonOP
so I can import prisma from backend to frontend and then use next-auth or auth.js inside next.js and use my bun js server normally like rest api , just the auth will be inside next js api , right ?
Avatar
wdym "import prisma from backend to frontend"
"use next-auth or auth.js inside next.js and use my bun js server normally like rest api , just the auth will be inside next js api"
Yes, attach a secret token with env var so that noone else can access it. ur backend only need to check if the env var is correct
Avatar
Scaly-naped PigeonOP
i have frontend folder and backend folder inside a root folder , i am exporting prisma from backend and importing it to the frontend so I can use prisma in next js normally, I think i will go with this approach because it makes life easier also next-auth can auto sync with prisma automatically so if user gets blocked or changed something the user will be automatically redirected to specific page