Receiving a 403 (forbidden) error from my Credentials Provider #2445
-
I'm trying to connect with my Django backend API, authenticate my user, and bring that information back into my Next.js app to create a session. I attempted to write my own function, but I couldn't figure out how to create the session manually, so it looks like the Credentials provider may be the only/best way to do this? Right now, import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next';
import NextAuth, { Account, CallbacksOptions, Profile, Session } from 'next-auth';
import Providers from 'next-auth/providers';
import { AuthenticatedUser } from '../../../interfaces/user';
const providers = [
Providers.Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
Providers.Credentials({
id: 'credentials',
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'text', placeholder: '[email protected]' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials, req) {
const res = await axios.post(
'http://127.0.0.1:8000/api/auth/login/',
{
password: credentials.password,
username: credentials.email,
email: credentials.email,
},
{
headers: {
accept: '*/*',
'Content-Type': 'application/json',
},
}
);
if (res.data.user) {
const user = {
email: res,
name: `${res.data.user.first_name} ${res.data.user.last_name}`,
id: res.data.user.pk,
};
return user;
} else {
throw new Error('error message');
}
},
}),
];
const callbacks: CallbacksOptions = {};
callbacks.signIn = async function signIn(user: AuthenticatedUser, account: Account) {
if (account.provider === 'google') {
const { accessToken, idToken } = account;
// Make the post req to the DRF api backend
try {
const res = await axios.post(
// use sep .ts .json file to store url endpoints
'http://127.0.0.1:8000/api/social/login/google/',
{
access_token: accessToken,
id_token: idToken,
}
);
// extract the returned token from the DRF backend and add it to the `user` object
const { access_token } = await res.data;
user.accessToken = access_token;
return true;
} catch (err) {
throw new Error(err);
}
}
return false;
};
callbacks.jwt = async function jwt(token: any, user: AuthenticatedUser) {
if (user) {
const { accessToken } = user;
// reform the `token` object from the access token we appended to the `user object
token.accessToken = accessToken;
}
return token;
};
callbacks.session = async function session(session: Session, user: AuthenticatedUser) {
session.accessToken = user.accessToken;
return session;
};
const options = {
providers,
callbacks,
};
const AuthenticationRequest = (req: NextApiRequest, res: NextApiResponse) =>
NextAuth(req, res, options);
export default AuthenticationRequest; I call it with a sign in button like this: const sendCredentials = async () => {
signIn('credentials', {
email: email,
password: password,
username: email,
callbackUrl: `/`,
}); I've read through the docs, looked at a bunch of tutorials, and nothing is really clicking as far as solutions here. I've console logged my response from the POST request inside the Credentials Provider and it's sending back everything I'd expect it to with a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem is in your signIn callback. You constantly return false if the provider is not Google, and so when you use the credentials flow, you always reject the signIn. |
Beta Was this translation helpful? Give feedback.
The problem is in your signIn callback. You constantly return false if the provider is not Google, and so when you use the credentials flow, you always reject the signIn.