2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-02-18 21:32:02 -06:00
import { prisma } from "@/lib/api/db" ;
2023-02-06 11:59:23 -06:00
import NextAuth from "next-auth/next" ;
import CredentialsProvider from "next-auth/providers/credentials" ;
import { AuthOptions } from "next-auth" ;
import bcrypt from "bcrypt" ;
export const authOptions : AuthOptions = {
session : {
strategy : "jwt" ,
} ,
providers : [
CredentialsProvider ( {
type : "credentials" ,
credentials : { } ,
async authorize ( credentials , req ) {
const { email , password } = credentials as {
2023-02-13 18:09:13 -06:00
id : number ;
2023-02-06 11:59:23 -06:00
email : string ;
password : string ;
} ;
console . log ( email , password ) ;
const findUser = await prisma . user . findFirst ( {
where : {
email : email ,
} ,
} ) ;
let passwordMatches : boolean = false ;
if ( findUser ? . password ) {
passwordMatches = bcrypt . compareSync ( password , findUser . password ) ;
}
if ( passwordMatches ) {
2023-02-08 15:11:33 -06:00
return {
id : findUser?.id ,
name : findUser?.name ,
email : findUser?.email ,
} ;
2023-02-06 11:59:23 -06:00
} else return null as any ;
} ,
} ) ,
] ,
pages : {
2023-02-08 15:11:33 -06:00
signIn : "/login" ,
} ,
callbacks : {
session : async ( { session , token } ) = > {
2023-02-13 18:09:13 -06:00
session . user . id = parseInt ( token ? . sub as any ) ;
2023-02-08 15:11:33 -06:00
return session ;
} ,
2023-05-21 04:54:42 -05:00
// Using the `...rest` parameter to be able to narrow down the type based on `trigger`
jwt ( { token , trigger , session } ) {
if ( trigger === "update" && session ? . name && session ? . email ) {
// Note, that `session` can be any arbitrary object, remember to validate it!
token . name = session . name ;
token . email = session . email ;
}
return token ;
} ,
2023-02-06 11:59:23 -06:00
} ,
} ;
export default NextAuth ( authOptions ) ;