I'm a beginner with next-auth. When I click on the sign in button, it redirects me to the Google sign-in page where it displays a list of accounts. Upon selecting an account, it shows the user's image and email.
The signIn() function is imported from the next-auth/react
module. If I want to store the user's email and image in a database, how can I achieve that? I would like this saving process to happen automatically when users choose their account.
Where should I incorporate the code for saving the user data in the database, including steps like using the model for data and establishing a DB connection?
//pages/index.js:
import { useSession, signIn, signOut } from "next-auth/react";
export default function Home() {
const { data: session } = useSession();
if (session) {
return (
<>
<p>Signed in as {session.user.email}</p>
<img src={session.user.image} />
<button onClick={() => signOut()}>Sign out</button>
</>
);
}
return (
<div>
<p>Not signed in</p>
<button onClick={() => signIn()}>Sign in</button>
</div>
);
}
//pages/api/auth/[...nextauth].js
import NextAuth from "next-auth/next";
import GoogleProvider from 'next-auth/providers/google'
export default NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET
})
],
});