fixed credentials provider

This commit is contained in:
Juthatip McDevitt 2024-05-30 00:42:59 -05:00
parent 96647fbf5a
commit d089fe461a
2 changed files with 34 additions and 31 deletions

View file

@ -9,35 +9,38 @@ import clientPromise from "../../../../libs/mongoConnect"
import { UserInfo } from "../../models/UserInfo" import { UserInfo } from "../../models/UserInfo"
export const authOptions = { export const authOptions = {
secret: process.env.SECRET, secret: process.env.SECRET,
adapter: MongoDBAdapter(clientPromise), adapter: MongoDBAdapter(clientPromise),
providers: [ providers: [
GoogleProvider({ GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID, clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET, clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}), }),
CredentialsProvider({ CredentialsProvider({
name: 'Credentials', name: 'Credentials',
id: 'credentials', id: 'credentials',
credentials: { credentials: {
username: { label: "Email", type: "email", placeholder: "test@example.com" }, username: { label: "Email", type: "email", placeholder: "test@example.com" },
password: { label: "Password", type: "password" } password: { label: "Password", type: "password" },
}, },
async authorize(credentials, req) { async authorize(credentials, req) {
const email = credentials?.email; const email = credentials?.email;
const password = credentials?.password; const password = credentials?.password;
mongoose.connect(process.env.MONGO_URL); mongoose.connect(process.env.MONGO_URL);
const user = await User.findOne({email}); const user = await User.findOne({email});
const passwordOk = user && bcrypt.compareSync(password, user.password); const passwordOk = user && bcrypt.compareSync(password, user.password);
if(passwordOk){ if(passwordOk){
return user return user;
} } else {
return null return null;
} }}
}), }),
], ],
session:{
strategy: "jwt"
},
}; };
export async function isAdmin(){ export async function isAdmin(){

View file

@ -8,15 +8,15 @@ import { signIn } from "next-auth/react";
const LoginPage = () => { const LoginPage = () => {
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const [loginInProgress, setLoginInProgreass] = useState(false); const [loginInProgress, setLoginInProgress] = useState(false);
async function handleFormSubmit(ev) { async function handleFormSubmit(ev) {
ev.preventDefault(); ev.preventDefault();
setLoginInProgreass(true) setLoginInProgress(true)
await signIn('credentials', {email, password, callbackUrl:'/'}) await signIn('credentials', {email, password, callbackUrl:'/', redirect: true});
setLoginInProgreass(false) setLoginInProgress(false)
} }