DEV Community

Cover image for NextAuth.js / Auth.js credential authentication with methods you need !

NextAuth.js / Auth.js credential authentication with methods you need !

Ekim Cem Ülger on January 18, 2023

Hey Devs ! As a front-end developer my nightmare was to handle the user authentication. With NextAuth.js (I will call it auth.js since they are c...
Collapse
 
saitgungor profile image
Sait

Quite useful content and very clear explanation, thanks @ekimcem

Collapse
 
sergo profile image
Sergo

Thank you, @ekimcem.

Collapse
 
meanurag profile image
Anurag Parmar

One of the best articles on this topic.

Collapse
 
haniframadhani profile image
muhammad hanif ramadhani

what does the expected fetch response look like? and what does should return after fetch? my response look like this

{
  "status": 200,
  "message": "success",
  "accessToken": "jwt token"
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ekimcem profile image
Ekim Cem Ülger

Actually it depends what you want to return when the user signin.

Generally I return the user data like username, email, user type, user id, etc.

When the user signs in you will need some of the user's properties in order to display relevant data in your app.

so it depends on your app and backend structure. @haniframadhani

Collapse
 
derick1530 profile image
Derick Zihalirwa

I have my custom signin page, How can I adjust your code in my code ?
@sait

Collapse
 
ekimcem profile image
Ekim Cem Ülger

For custom signin page, you should add "pages"object into your nextauth config;
like in the example

 pages: {
    signIn: '/auth/signin',
  }
Enter fullscreen mode Exit fullscreen mode

and then assign your custom signin page route into the signIn.

The thing is you need to import signin function from nextauth and provide the required credentials into that function.

Collapse
 
derick1530 profile image
Derick Zihalirwa

Thanks

Collapse
 
derick1530 profile image
Derick Zihalirwa

Can you make an updated version of this code in TS?

Collapse
 
ekimcem profile image
Ekim Cem Ülger

Will add it soon.