next auth help

does anyone have experience with next auth? I'm finding the documentation to be sub par. Currently I'm struggling to figure out how to handle signin errors using the credentials provider.
1 Reply
Shane
ShaneOP9mo ago
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
// Your own logic for dealing with plaintext password strings; be careful!
import { saltAndHashPassword } from "@/utils/password";
import { getUserFromDb } from "./utils/db";

export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
Credentials({
id: "credentials",
name: "Credentials",
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
// e.g. domain, username, password, 2FA token, etc.
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
try {
let user = null;

// validate the credential formatting
if (
!credentials.password ||
typeof credentials.password != "string"
) {
throw new Error("No password provided.");
}
if (!credentials.email || typeof credentials.email != "string") {
throw new Error("No email provided.");
}

const pwHash = await saltAndHashPassword(credentials.password);

user = await getUserFromDb(credentials.email, pwHash);
// return user object with the their profile data
return user;
} catch (e) {
console.error("Error logging in:", e);
return {};
}
},
}),
],
});
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
// Your own logic for dealing with plaintext password strings; be careful!
import { saltAndHashPassword } from "@/utils/password";
import { getUserFromDb } from "./utils/db";

export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
Credentials({
id: "credentials",
name: "Credentials",
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
// e.g. domain, username, password, 2FA token, etc.
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
try {
let user = null;

// validate the credential formatting
if (
!credentials.password ||
typeof credentials.password != "string"
) {
throw new Error("No password provided.");
}
if (!credentials.email || typeof credentials.email != "string") {
throw new Error("No email provided.");
}

const pwHash = await saltAndHashPassword(credentials.password);

user = await getUserFromDb(credentials.email, pwHash);
// return user object with the their profile data
return user;
} catch (e) {
console.error("Error logging in:", e);
return {};
}
},
}),
],
});
This is likely bad and wrong because I have no idea what I'm doing
Want results from more Discord servers?
Add your server