I have a private next project with Next Auth and I am implementing credentials only email and password because these users will be created at the Firebase Auth panel. My Next Auth seems like this:
import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { auth, app } from '@/util/firebase';
import { signInWithEmailAndPassword } from 'firebase/auth';
export default NextAuth({
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: '/auth/signin',
},
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials, req) {
try {
const userCredential = await signInWithEmailAndPassword(
auth,
credentials.email,
credentials.password
);
// I want to get this token and save as Bearer Authorization token userCredential.idToken
if (userCredential.user) return { email: userCredential.user.email };
else return null;
} catch (error) {
throw new Error('Invalid email or password');
}
},
}),
],
});
But there is a little problem. For the requests API, I need to send a valid Bearer token. How can I store and retrieve the token?
Observation: Token is available in userCredential.idToken