Refactor to use hooks (#436)
This commit is contained in:
parent
124efca9a1
commit
cb60f8b143
139 changed files with 8871 additions and 8726 deletions
53
app/(auth)/auth.ts
Normal file
53
app/(auth)/auth.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { compare } from "bcrypt-ts";
|
||||
import NextAuth, { User, Session } from "next-auth";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
|
||||
import { getUser } from "@/db/queries";
|
||||
|
||||
import { authConfig } from "./auth.config";
|
||||
|
||||
interface ExtendedSession extends Session {
|
||||
user: User;
|
||||
}
|
||||
|
||||
export const {
|
||||
handlers: { GET, POST },
|
||||
auth,
|
||||
signIn,
|
||||
signOut,
|
||||
} = NextAuth({
|
||||
...authConfig,
|
||||
providers: [
|
||||
Credentials({
|
||||
credentials: {},
|
||||
async authorize({ email, password }: any) {
|
||||
let users = await getUser(email);
|
||||
if (users.length === 0) return null;
|
||||
let passwordsMatch = await compare(password, users[0].password!);
|
||||
if (passwordsMatch) return users[0] as any;
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (user) {
|
||||
token.id = user.id;
|
||||
}
|
||||
|
||||
return token;
|
||||
},
|
||||
async session({
|
||||
session,
|
||||
token,
|
||||
}: {
|
||||
session: ExtendedSession;
|
||||
token: any;
|
||||
}) {
|
||||
if (session.user) {
|
||||
session.user.id = token.id as string;
|
||||
}
|
||||
|
||||
return session;
|
||||
},
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue