From 49cb07df11d4b9573142907b49dd62f701c994bc Mon Sep 17 00:00:00 2001 From: besscroft Date: Fri, 5 Apr 2024 23:44:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=20authjs=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20prisma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 14 +- .env.production | 14 +- app/(admin)/admin/page.tsx | 6 +- app/actions.ts | 27 -- components/UserFrom.tsx | 12 +- package.json | 8 +- pnpm-lock.yaml | 237 ++++-------------- .../20240405130405_init/migration.sql | 67 +++++ prisma/migrations/migration_lock.toml | 3 + prisma/schema.prisma | 54 ++++ utils/lib/auth.ts | 93 ------- utils/lib/db.ts | 15 +- utils/lib/session.ts | 7 - utils/lib/userContext.ts | 7 - 14 files changed, 194 insertions(+), 370 deletions(-) delete mode 100644 app/actions.ts create mode 100644 prisma/migrations/20240405130405_init/migration.sql create mode 100644 prisma/migrations/migration_lock.toml create mode 100644 prisma/schema.prisma delete mode 100644 utils/lib/auth.ts delete mode 100644 utils/lib/session.ts delete mode 100644 utils/lib/userContext.ts diff --git a/.env b/.env index 20ad4e3..8d93782 100644 --- a/.env +++ b/.env @@ -1,12 +1,2 @@ -# Postgre 主机 -POSTGRE_HOST="localhost" -# Postgre 端口 -POSTGRE_PORT=5432 -# Postgre 数据库 -POSTGRE_DATABASE="postgres" -# Postgre 用户名 -POSTGRE_USERNAME="postgres" -# Postgre 密码 -POSTGRE_PASSWORD="postgres" -# AUTH_SECRET npx auth secret -AUTH_SECRET="T7tQXKqqKjZrJhN62Z05AHY8qKGQKaG/4iqv0tKmEvw=" +# 数据库 url +DATABASE_URL="db.picimpact.supabase.co" diff --git a/.env.production b/.env.production index 898a80d..8d93782 100644 --- a/.env.production +++ b/.env.production @@ -1,12 +1,2 @@ -# Postgre 主机 -POSTGRE_HOST="db.picimpact.supabase.co" -# Postgre 端口 -POSTGRE_PORT=5432 -# Postgre 数据库 -POSTGRE_DATABASE="postgres" -# Postgre 用户名 -POSTGRE_USERNAME="postgres" -# Postgre 密码 -POSTGRE_PASSWORD="postgres" -# AUTH_SECRET npx auth secret -AUTH_SECRET="T7tQXKqqKjZrJhN62Z05AHY8qKGQKaG/4iqv0tKmEvw=" +# 数据库 url +DATABASE_URL="db.picimpact.supabase.co" diff --git a/app/(admin)/admin/page.tsx b/app/(admin)/admin/page.tsx index 7804cd9..8a287e4 100644 --- a/app/(admin)/admin/page.tsx +++ b/app/(admin)/admin/page.tsx @@ -1,11 +1,9 @@ -import { auth } from '~/utils/lib/auth' - export default async function Admin() { - const session = await auth() + return ( <> - 控制台 {session?.user} + 控制台 ) } \ No newline at end of file diff --git a/app/actions.ts b/app/actions.ts deleted file mode 100644 index e16ed69..0000000 --- a/app/actions.ts +++ /dev/null @@ -1,27 +0,0 @@ -'use server' - -import { AuthError } from 'next-auth' -import { signIn } from '~/utils/lib/auth' - -export async function authenticate( - email:string, password: string -) { - try { - await signIn('credentials', { - email: email, - password: password, - redirect: true, - redirectTo: '/admin' - }); - } catch (error) { - if (error instanceof AuthError) { - switch (error.type) { - case 'CredentialsSignin': - return 'Invalid credentials.'; - default: - return 'Something went wrong.'; - } - } - throw error; - } -} \ No newline at end of file diff --git a/components/UserFrom.tsx b/components/UserFrom.tsx index 3c3048b..7dbc28e 100644 --- a/components/UserFrom.tsx +++ b/components/UserFrom.tsx @@ -5,15 +5,11 @@ import { Input } from '@nextui-org/input' import Link from 'next/link' import { Button } from '@nextui-org/react' import { useRouter } from 'next/navigation' -import { authenticate } from '~/app/actions' -// import { useFormState, useFormStatus } from 'react-dom' +import { toast } from 'sonner' export const UserFrom = () => { const router = useRouter() - // const [errorMessage, dispatch] = useFormState(authenticate, undefined) - // const { pending } = useFormStatus() - const [isLoading, setIsLoading] = useState(false) const [email, setEmail] = useState('') @@ -63,11 +59,7 @@ export const UserFrom = () => { type="submit" className="w-full" isLoading={isLoading} - onClick={async () => { - setIsLoading(true) - await authenticate(email, password) - setIsLoading(false) - }} + onClick={() => toast.info('还没写!')} > 登录 diff --git a/package.json b/package.json index 979e4c3..88a9b01 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,14 @@ "dev": "next dev", "build": "next build", "start": "next start", - "lint": "next lint" + "lint": "next lint", + "postinstall": "npx prisma migrate deploy && npx prisma generate" }, "dependencies": { - "@auth/pg-adapter": "^0.7.2", "@nextui-org/react": "^2.2.10", + "@prisma/client": "^5.12.1", "framer-motion": "^11.0.24", "next": "^14.1.4", - "next-auth": "5.0.0-beta.16", "next-themes": "^0.3.0", "pg": "^8.11.5", "react": "^18.2.0", @@ -28,13 +28,13 @@ }, "devDependencies": { "@types/node": "^20.12.2", - "@types/pg": "^8.11.4", "@types/react": "^18.2.73", "@types/react-dom": "^18.2.23", "autoprefixer": "^10.4.19", "eslint": "^8.57.0", "eslint-config-next": "^14.1.4", "postcss": "^8.4.38", + "prisma": "^5.12.1", "tailwindcss": "^3.4.3", "typescript": "^5.4.3" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 96f1180..3b0cc4a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,21 +5,18 @@ settings: excludeLinksFromLockfile: false dependencies: - '@auth/pg-adapter': - specifier: ^0.7.2 - version: 0.7.2(pg@8.11.5) '@nextui-org/react': specifier: ^2.2.10 version: 2.2.10(@types/react@18.2.73)(framer-motion@11.0.24)(react-dom@18.2.0)(react@18.2.0)(tailwind-variants@0.2.1)(tailwindcss@3.4.3) + '@prisma/client': + specifier: ^5.12.1 + version: 5.12.1(prisma@5.12.1) framer-motion: specifier: ^11.0.24 version: 11.0.24(react-dom@18.2.0)(react@18.2.0) next: specifier: ^14.1.4 version: 14.1.4(react-dom@18.2.0)(react@18.2.0) - next-auth: - specifier: 5.0.0-beta.16 - version: 5.0.0-beta.16(next@14.1.4)(react@18.2.0) next-themes: specifier: ^0.3.0 version: 0.3.0(react-dom@18.2.0)(react@18.2.0) @@ -58,9 +55,6 @@ devDependencies: '@types/node': specifier: ^20.12.2 version: 20.12.2 - '@types/pg': - specifier: ^8.11.4 - version: 8.11.4 '@types/react': specifier: ^18.2.73 version: 18.2.73 @@ -79,6 +73,9 @@ devDependencies: postcss: specifier: ^8.4.38 version: 8.4.38 + prisma: + specifier: ^5.12.1 + version: 5.12.1 tailwindcss: specifier: ^3.4.3 version: 3.4.3 @@ -97,65 +94,6 @@ packages: resolution: {integrity: sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==} engines: {node: '>=10'} - /@auth/core@0.28.1: - resolution: {integrity: sha512-gvp74mypYZADpTlfGRp6HE0G3pIHWvtJpy+KZ+8FvY0cmlIpHog+jdMOdd29dQtLtN25kF2YbfHsesCFuGUQbg==} - peerDependencies: - '@simplewebauthn/browser': ^9.0.1 - '@simplewebauthn/server': ^9.0.2 - nodemailer: ^6.8.0 - peerDependenciesMeta: - '@simplewebauthn/browser': - optional: true - '@simplewebauthn/server': - optional: true - nodemailer: - optional: true - dependencies: - '@panva/hkdf': 1.1.1 - '@types/cookie': 0.6.0 - cookie: 0.6.0 - jose: 5.2.3 - oauth4webapi: 2.10.4 - preact: 10.11.3 - preact-render-to-string: 5.2.3(preact@10.11.3) - dev: false - - /@auth/core@0.28.2: - resolution: {integrity: sha512-Rlvu6yKa4bKbhQESMaEm6jHOY5ncIrsrQkC8tcwVQmf+cBLk7ReI9DIJS2O/WkIDoOwvM9PHiXTi5b+b/eyXxw==} - peerDependencies: - '@simplewebauthn/browser': ^9.0.1 - '@simplewebauthn/server': ^9.0.2 - nodemailer: ^6.8.0 - peerDependenciesMeta: - '@simplewebauthn/browser': - optional: true - '@simplewebauthn/server': - optional: true - nodemailer: - optional: true - dependencies: - '@panva/hkdf': 1.1.1 - '@types/cookie': 0.6.0 - cookie: 0.6.0 - jose: 5.2.3 - oauth4webapi: 2.10.4 - preact: 10.11.3 - preact-render-to-string: 5.2.3(preact@10.11.3) - dev: false - - /@auth/pg-adapter@0.7.2(pg@8.11.5): - resolution: {integrity: sha512-hhMflWXzkB1TVBZs/1zdgVHgnAkxL+xZrK5F3l44RQZ7ZAUoIvVXYKJva551wj0F11x04cpy+noeXg3a0jDnHw==} - peerDependencies: - pg: ^8 - dependencies: - '@auth/core': 0.28.2 - pg: 8.11.5 - transitivePeerDependencies: - - '@simplewebauthn/browser' - - '@simplewebauthn/server' - - nodemailer - dev: false - /@babel/runtime@7.24.1: resolution: {integrity: sha512-+BIznRzyqBf+2wCTxcKE3wDjfGeCoVE61KSHGpkzqrLi8qxqFwBeUFyId2cxkTmm55fzDGnm0+yCxaxygrLUnQ==} engines: {node: '>=6.9.0'} @@ -1717,16 +1655,52 @@ packages: '@nodelib/fs.scandir': 2.1.5 fastq: 1.17.1 - /@panva/hkdf@1.1.1: - resolution: {integrity: sha512-dhPeilub1NuIG0X5Kvhh9lH4iW3ZsHlnzwgwbOlgwQ2wG1IqFzsgHqmKPk3WzsdWAeaxKJxgM0+W433RmN45GA==} - dev: false - /@pkgjs/parseargs@0.11.0: resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==} engines: {node: '>=14'} requiresBuild: true optional: true + /@prisma/client@5.12.1(prisma@5.12.1): + resolution: {integrity: sha512-6/JnizEdlSBxDIdiLbrBdMW5NqDxOmhXAJaNXiPpgzAPr/nLZResT6MMpbOHLo5yAbQ1Vv5UU8PTPRzb0WIxdA==} + engines: {node: '>=16.13'} + requiresBuild: true + peerDependencies: + prisma: '*' + peerDependenciesMeta: + prisma: + optional: true + dependencies: + prisma: 5.12.1 + dev: false + + /@prisma/debug@5.12.1: + resolution: {integrity: sha512-kd/wNsR0klrv79o1ITsbWxYyh4QWuBidvxsXSParPsYSu0ircUmNk3q4ojsgNc3/81b0ozg76iastOG43tbf8A==} + + /@prisma/engines-version@5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab: + resolution: {integrity: sha512-6yvO8s80Tym61aB4QNtYZfWVmE3pwqe807jEtzm8C5VDe7nw8O1FGX3TXUaXmWV0fQTIAfRbeL2Gwrndabp/0g==} + + /@prisma/engines@5.12.1: + resolution: {integrity: sha512-HQDdglLw2bZR/TXD2Y+YfDMvi5Q8H+acbswqOsWyq9pPjBLYJ6gzM+ptlTU/AV6tl0XSZLU1/7F4qaWa8bqpJA==} + requiresBuild: true + dependencies: + '@prisma/debug': 5.12.1 + '@prisma/engines-version': 5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab + '@prisma/fetch-engine': 5.12.1 + '@prisma/get-platform': 5.12.1 + + /@prisma/fetch-engine@5.12.1: + resolution: {integrity: sha512-qSs3KcX1HKcea1A+hlJVK/ljj0PNIUHDxAayGMvgJBqmaN32P9tCidlKz1EGv6WoRFICYnk3Dd/YFLBwnFIozA==} + dependencies: + '@prisma/debug': 5.12.1 + '@prisma/engines-version': 5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab + '@prisma/get-platform': 5.12.1 + + /@prisma/get-platform@5.12.1: + resolution: {integrity: sha512-pgIR+pSvhYHiUcqXVEZS31NrFOTENC9yFUdEAcx7cdQBoZPmHVjtjN4Ss6NzVDMYPrKJJ51U14EhEoeuBlMioQ==} + dependencies: + '@prisma/debug': 5.12.1 + /@radix-ui/primitive@1.0.1: resolution: {integrity: sha512-yQ8oGX2GVsEYMWGxcovu1uGWPCxV5BFfeeYxqPmuAzUyLT9qmaMXSAhXpb0WrspIeqYzdJpkh2vHModJPgRIaw==} dependencies: @@ -2935,10 +2909,6 @@ packages: tslib: 2.6.2 dev: false - /@types/cookie@0.6.0: - resolution: {integrity: sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA==} - dev: false - /@types/json5@0.0.29: resolution: {integrity: sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==} dev: true @@ -2949,14 +2919,6 @@ packages: undici-types: 5.26.5 dev: true - /@types/pg@8.11.4: - resolution: {integrity: sha512-yw3Bwbda6vO+NvI1Ue/YKOwtl31AYvvd/e73O3V4ZkNzuGpTDndLSyc0dQRB2xrQqDePd20pEGIfqSp/GH3pRw==} - dependencies: - '@types/node': 20.12.2 - pg-protocol: 1.6.1 - pg-types: 4.0.2 - dev: true - /@types/prop-types@15.7.12: resolution: {integrity: sha512-5zvhXYtRNRluoE/jAp4GVsSduVUzNWKkOZrCDBWYtE7biZywwdC2AcEzg+cSMLFRfVgeAFqpfNabiPjxFddV1Q==} @@ -3392,11 +3354,6 @@ packages: resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} dev: true - /cookie@0.6.0: - resolution: {integrity: sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==} - engines: {node: '>= 0.6'} - dev: false - /cross-spawn@7.0.3: resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==} engines: {node: '>= 8'} @@ -4503,10 +4460,6 @@ packages: resolution: {integrity: sha512-gFqAIbuKyyso/3G2qhiO2OM6shY6EPP/R0+mkDbyspxKazh8BXDC5FiFsUjlczgdNz/vfra0da2y+aHrusLG/Q==} hasBin: true - /jose@5.2.3: - resolution: {integrity: sha512-KUXdbctm1uHVL8BYhnyHkgp3zDX5KW8ZhAKVFEfUbU2P8Alpzjb+48hHvjOdQIyPshoblhzsuqOwEEAbtHVirA==} - dev: false - /js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} @@ -4698,27 +4651,6 @@ packages: resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==} dev: true - /next-auth@5.0.0-beta.16(next@14.1.4)(react@18.2.0): - resolution: {integrity: sha512-dX2snB+ezN23tFzSes3n3uosT9iBf0eILPYWH/R2fd9n3ZzdMQlRzq7JIOPeS1aLc84IuRlyuyXyx9XmmZB6og==} - peerDependencies: - '@simplewebauthn/browser': ^9.0.1 - '@simplewebauthn/server': ^9.0.2 - next: ^14 - nodemailer: ^6.6.5 - react: ^18.2.0 - peerDependenciesMeta: - '@simplewebauthn/browser': - optional: true - '@simplewebauthn/server': - optional: true - nodemailer: - optional: true - dependencies: - '@auth/core': 0.28.1 - next: 14.1.4(react-dom@18.2.0)(react@18.2.0) - react: 18.2.0 - dev: false - /next-themes@0.3.0(react-dom@18.2.0)(react@18.2.0): resolution: {integrity: sha512-/QHIrsYpd6Kfk7xakK4svpDI5mmXP0gfvCoJdGpZQ2TOrQZmsW0QxjaiLn8wbIKjtm4BTSqLoix4lxYYOnLJ/w==} peerDependencies: @@ -4781,10 +4713,6 @@ packages: engines: {node: '>=0.10.0'} dev: true - /oauth4webapi@2.10.4: - resolution: {integrity: sha512-DSoj8QoChzOCQlJkRmYxAJCIpnXFW32R0Uq7avyghIeB6iJq0XAblOD7pcq3mx4WEBDwMuKr0Y1qveCBleG2Xw==} - dev: false - /object-assign@4.1.1: resolution: {integrity: sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==} engines: {node: '>=0.10.0'} @@ -4858,10 +4786,6 @@ packages: es-object-atoms: 1.0.0 dev: true - /obuf@1.1.2: - resolution: {integrity: sha512-PX1wu0AmAdPqOL1mWhqmlOd8kOIZQwGZw6rh7uby9fTc5lhaOWFLX3I6R1hrF9k3zUY40e6igsLGkDXK92LJNg==} - dev: true - /once@1.4.0: resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==} dependencies: @@ -4943,11 +4867,7 @@ packages: /pg-int8@1.0.1: resolution: {integrity: sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==} engines: {node: '>=4.0.0'} - - /pg-numeric@1.0.2: - resolution: {integrity: sha512-BM/Thnrw5jm2kKLE5uJkXqqExRUY/toLHda65XgFTBTFYZyopbKjBe29Ii3RbkvlsMoFwD+tHeGaCjjv0gHlyw==} - engines: {node: '>=4'} - dev: true + dev: false /pg-pool@3.6.2(pg@8.11.5): resolution: {integrity: sha512-Htjbg8BlwXqSBQ9V8Vjtc+vzf/6fVUuak/3/XXKA9oxZprwW3IMDQTGHP+KDmVL7rtd+R1QjbnCFPuTHm3G4hg==} @@ -4959,6 +4879,7 @@ packages: /pg-protocol@1.6.1: resolution: {integrity: sha512-jPIlvgoD63hrEuihvIg+tJhoGjUsLPn6poJY9N5CnlPd91c2T18T/9zBtLxZSb1EhYxBRoZJtzScCaWlYLtktg==} + dev: false /pg-types@2.2.0: resolution: {integrity: sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==} @@ -4971,19 +4892,6 @@ packages: postgres-interval: 1.2.0 dev: false - /pg-types@4.0.2: - resolution: {integrity: sha512-cRL3JpS3lKMGsKaWndugWQoLOCoP+Cic8oseVcbr0qhPzYD5DWXK+RZ9LY9wxRf7RQia4SCwQlXk0q6FCPrVng==} - engines: {node: '>=10'} - dependencies: - pg-int8: 1.0.1 - pg-numeric: 1.0.2 - postgres-array: 3.0.2 - postgres-bytea: 3.0.0 - postgres-date: 2.1.0 - postgres-interval: 3.0.0 - postgres-range: 1.1.4 - dev: true - /pg@8.11.5: resolution: {integrity: sha512-jqgNHSKL5cbDjFlHyYsCXmQDrfIX/3RsNwYqpd4N0Kt8niLuNoRNH+aazv6cOd43gPh9Y4DjQCtb+X0MH0Hvnw==} engines: {node: '>= 8.0.0'} @@ -5105,33 +5013,16 @@ packages: engines: {node: '>=4'} dev: false - /postgres-array@3.0.2: - resolution: {integrity: sha512-6faShkdFugNQCLwucjPcY5ARoW1SlbnrZjmGl0IrrqewpvxvhSLHimCVzqeuULCbG0fQv7Dtk1yDbG3xv7Veog==} - engines: {node: '>=12'} - dev: true - /postgres-bytea@1.0.0: resolution: {integrity: sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==} engines: {node: '>=0.10.0'} dev: false - /postgres-bytea@3.0.0: - resolution: {integrity: sha512-CNd4jim9RFPkObHSjVHlVrxoVQXz7quwNFpz7RY1okNNme49+sVyiTvTRobiLV548Hx/hb1BG+iE7h9493WzFw==} - engines: {node: '>= 6'} - dependencies: - obuf: 1.1.2 - dev: true - /postgres-date@1.0.7: resolution: {integrity: sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==} engines: {node: '>=0.10.0'} dev: false - /postgres-date@2.1.0: - resolution: {integrity: sha512-K7Juri8gtgXVcDfZttFKVmhglp7epKb1K4pgrkLxehjqkrgPhfG6OO8LHLkfaqkbpjNRnra018XwAr1yQFWGcA==} - engines: {node: '>=12'} - dev: true - /postgres-interval@1.2.0: resolution: {integrity: sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==} engines: {node: '>=0.10.0'} @@ -5139,36 +5030,18 @@ packages: xtend: 4.0.2 dev: false - /postgres-interval@3.0.0: - resolution: {integrity: sha512-BSNDnbyZCXSxgA+1f5UU2GmwhoI0aU5yMxRGO8CdFEcY2BQF9xm/7MqKnYoM1nJDk8nONNWDk9WeSmePFhQdlw==} - engines: {node: '>=12'} - dev: true - - /postgres-range@1.1.4: - resolution: {integrity: sha512-i/hbxIE9803Alj/6ytL7UHQxRvZkI9O4Sy+J3HGc4F4oo/2eQAjTSNJ0bfxyse3bH0nuVesCk+3IRLaMtG3H6w==} - dev: true - - /preact-render-to-string@5.2.3(preact@10.11.3): - resolution: {integrity: sha512-aPDxUn5o3GhWdtJtW0svRC2SS/l8D9MAgo2+AWml+BhDImb27ALf04Q2d+AHqUUOc6RdSXFIBVa2gxzgMKgtZA==} - peerDependencies: - preact: '>=10' - dependencies: - preact: 10.11.3 - pretty-format: 3.8.0 - dev: false - - /preact@10.11.3: - resolution: {integrity: sha512-eY93IVpod/zG3uMF22Unl8h9KkrcKIRs2EGar8hwLZZDU1lkjph303V9HZBwufh2s736U6VXuhD109LYqPoffg==} - dev: false - /prelude-ls@1.2.1: resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==} engines: {node: '>= 0.8.0'} dev: true - /pretty-format@3.8.0: - resolution: {integrity: sha512-WuxUnVtlWL1OfZFQFuqvnvs6MiAGk9UNsBostyBOB0Is9wb5uRESevA6rnl/rkksXaGX3GzZhPup5d6Vp1nFew==} - dev: false + /prisma@5.12.1: + resolution: {integrity: sha512-SkMnb6wyIxTv9ACqiHBI2u9gD6y98qXRoCoLEnZsF6yee5Qg828G+ARrESN+lQHdw4maSZFFSBPPDpvSiVTo0Q==} + engines: {node: '>=16.13'} + hasBin: true + requiresBuild: true + dependencies: + '@prisma/engines': 5.12.1 /prop-types@15.8.1: resolution: {integrity: sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==} diff --git a/prisma/migrations/20240405130405_init/migration.sql b/prisma/migrations/20240405130405_init/migration.sql new file mode 100644 index 0000000..c8e107a --- /dev/null +++ b/prisma/migrations/20240405130405_init/migration.sql @@ -0,0 +1,67 @@ +-- CreateTable +CREATE TABLE "Account" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "type" TEXT NOT NULL, + "provider" TEXT NOT NULL, + "providerAccountId" TEXT NOT NULL, + "refresh_token" TEXT, + "access_token" TEXT, + "expires_at" INTEGER, + "token_type" TEXT, + "scope" TEXT, + "id_token" TEXT, + "session_state" TEXT, + + CONSTRAINT "Account_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Session" ( + "id" TEXT NOT NULL, + "sessionToken" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "expires" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Session_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL, + "name" TEXT, + "email" TEXT, + "password" TEXT, + "emailVerified" TIMESTAMP(3), + "image" TEXT, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "VerificationToken" ( + "identifier" TEXT NOT NULL, + "token" TEXT NOT NULL, + "expires" TIMESTAMP(3) NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId"); + +-- CreateIndex +CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken"); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token"); + +-- CreateIndex +CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token"); + +-- AddForeignKey +ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..770ccfe --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,54 @@ +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} + +generator client { + provider = "prisma-client-js" +} + +model Account { + id String @id @default(cuid()) + userId String + type String + provider String + providerAccountId String + refresh_token String? @db.Text + access_token String? @db.Text + expires_at Int? + token_type String? + scope String? + id_token String? @db.Text + session_state String? + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + @@unique([provider, providerAccountId]) +} + +model Session { + id String @id @default(cuid()) + sessionToken String @unique + userId String + expires DateTime + user User @relation(fields: [userId], references: [id], onDelete: Cascade) +} + +model User { + id String @id @default(cuid()) + name String? + email String? @unique + password String? + emailVerified DateTime? + image String? + accounts Account[] + sessions Session[] +} + +model VerificationToken { + identifier String + token String @unique + expires DateTime + + @@unique([identifier, token]) +} \ No newline at end of file diff --git a/utils/lib/auth.ts b/utils/lib/auth.ts deleted file mode 100644 index 54db8d9..0000000 --- a/utils/lib/auth.ts +++ /dev/null @@ -1,93 +0,0 @@ -import NextAuth from 'next-auth' -import type { NextAuthConfig } from 'next-auth' -import CredentialsProvider from 'next-auth/providers/credentials' -import PostgresAdapter from '@auth/pg-adapter' -import pool from '~/utils/lib/db' -import { z } from 'zod' -// import { toast } from 'sonner' - -const authConfig = { - secret: 'T7tQXKqqKjZrJhN62Z05AHY8qKGQKaG/4iqv0tKmEvw=', - adapter: PostgresAdapter(pool), - session: { - strategy: "jwt", - }, - pages: { - signIn: "/login", - }, - providers: [ - CredentialsProvider({ - id: "credentials", - name: "Credentials", - type: "credentials", - credentials: { - email: { label: "Email", type: "email", placeholder: "Email" }, - password: { label: "Password", type: "password" } - }, - async authorize(credentials, req) { - const parsedCredentials = z - .object({ email: z.string().email(), password: z.string().min(6) }) - .safeParse(credentials); - - if (parsedCredentials.success) { - const { email, password } = parsedCredentials.data; - // TODO 邮件和密码校验 - console.log(email, password) - - const user = await pool.query('SELECT * FROM users WHERE email = $1::text LIMIT 1', [`${email}`]) - - console.log(user.rowCount) - console.log(user.rows[0]) - if (user.rowCount === 0) { - // toast.error('登录失败!', {duration: 1000}) - return null; - } - - // toast.success('登录成功!', {duration: 1000}) - return user.rows[0]; - } - - console.log('Invalid credentials'); - // toast.error('登录失败!', {duration: 1000}) - return null; - } - }) - ], - callbacks: { - async session({ token, session }) { - if (token) { - session.user = { - ...session.user, - id: session.user.id, - name: session.user.name, - email: session.user.email, - image: session.user.image, - } - } - - return session - }, - async jwt({ token, user }) { - const dbUser = await pool.query('SELECT * FROM users WHERE email = $1::text LIMIT 1', [`${user.email}`]) - - if (dbUser.rowCount === 0) { - token.id = Number(user?.id) - return token - } - - return { - ...token, - id: dbUser.rows[0]?.id, - name: dbUser.rows[0]?.name, - email: dbUser.rows[0]?.email, - picture: dbUser.rows[0]?.image, - } - }, - }, -} satisfies NextAuthConfig; - -export const { - handlers: { GET, POST }, - auth, - signIn, -} = NextAuth(authConfig) \ No newline at end of file diff --git a/utils/lib/db.ts b/utils/lib/db.ts index a466675..5e4d599 100644 --- a/utils/lib/db.ts +++ b/utils/lib/db.ts @@ -1,14 +1,5 @@ -import { Pool } from 'pg' +import { PrismaClient } from '@prisma/client' -const pool = new Pool({ - host: process.env.POSTGRE_HOST, - port: process.env.POSTGRE_PORT, - database: process.env.POSTGRE_DATABASE, - user: process.env.POSTGRE_USERNAME, - password: process.env.POSTGRE_PASSWORD, - max: 20, - idleTimeoutMillis: 30000, - connectionTimeoutMillis: 2000, -}) +const prisma = new PrismaClient() -export default pool; \ No newline at end of file +export const db = prisma \ No newline at end of file diff --git a/utils/lib/session.ts b/utils/lib/session.ts deleted file mode 100644 index b71d3c5..0000000 --- a/utils/lib/session.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { auth } from '~/utils/lib/auth' - -export async function getSession() { - const session = await auth() - - return session -} \ No newline at end of file diff --git a/utils/lib/userContext.ts b/utils/lib/userContext.ts deleted file mode 100644 index a9cce77..0000000 --- a/utils/lib/userContext.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { auth } from '~/utils/lib/auth' - -export async function getCurrentUser() { - const session = await auth() - - return session?.user -} \ No newline at end of file