DEV Community

Cover image for Next.js authentication with existing backend

Next.js authentication with existing backend

Szymon Kabelis on March 31, 2021

Recently I was working on some Next.js project and came to issues regarding the authentication using a built-in Next.js server and the existing bac...
Collapse
 
dededavida profile image
David Cruz

Hi guys, if you need the token in an axios request, do it like this!

api.tsx

Collapse
 
wparad profile image
Warren Parad

Will have to say, it's much easier to pull in a package such as federated auth to do this automatically. You don't need to set anything extra or understand how JWTs are supported to work to make sure the UI app works correctly.

Collapse
 
szymkab profile image
Szymon Kabelis

Well, I would say using next-auth doesn't require knowing JWTs but I've included that in the post because I think it's always better to know at least something about what you are using...

Collapse
 
wparad profile image
Warren Parad

Sure, but it's better than to put that as the perspective in the article. "Hey, this is difficult, there are packages to handle it, and here we are going to dive into why these packages exist and what they are providing."

Thread Thread
 
jaketone profile image
Jake

Quite an aggressive way of referring others to your own package. Thank you @szymkab for the simple concept.

Collapse
 
dwalker93 profile image
Adam Walker

Hey Szymon this is great article.. But using this how to send authenticated requests to another server?
For example next app in client and main express graphql api in another api path. how to send authenticated request to other server?

Collapse
 
szymkab profile image
Szymon Kabelis

Hi, do you want to send requests from Next.js client (browser react app) or server?

Collapse
 
dwalker93 profile image
Adam Walker

yeah for example my front next js app in client folder and backend express graphql server in server folder. I can set token as a header when request from next to graphql api. but how to decode that jwt in graphql api ?

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
msahil1 profile image
msahil1

Hi
great info, Could also tell how we can do NextAuth Google login with exisitng backend API as backend API releases its own token.

Collapse
 
dmtrbch profile image
Dimitar Bochvarovski

Hello, is it possible to use this method for authentication with existing backend, but with other providers, such as google, twitter...

Thanks in advance