diff --git a/.eslintrc.json b/.eslintrc.json index 3722418..e4b6f50 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,3 +1,7 @@ { - "extends": ["next/core-web-vitals", "next/typescript"] + "extends": ["next/core-web-vitals", "next/typescript"], + "rules": { + "@typescript-eslint/no-unused-vars": "warn", + "@next/next/no-img-element": "warn" + } } diff --git a/app/(root)/adt/[id]/page.tsx b/app/(root)/adt/[id]/page.tsx index c4005e1..205b5ad 100644 --- a/app/(root)/adt/[id]/page.tsx +++ b/app/(root)/adt/[id]/page.tsx @@ -1,20 +1,22 @@ import React from 'react'; // import { useParams } from 'next/navigation'; import { MapPin, Calendar, Phone, MessageCircle, Share2, Flag, Heart } from 'lucide-react'; -// import { adts } from '@/data/adt'; import { prisma } from '@/prisma/prisma-client'; -import Header from '@/components/Header'; import { notFound } from 'next/navigation'; -export default async function AdtPage({params: { id } }: { params: { id: string } }) { +type Params = Promise<{ id: string }> + +export default async function AdtPage(props: { params: Params }) { + const params = await props.params; + const adt = await prisma.adt.findUnique({ where: { - id: Number(id), + id: Number(params.id), }, include: { user: true } - }) + }) if (!adt) { return notFound(); diff --git a/app/(root)/page.tsx b/app/(root)/page.tsx index 64461df..81b0295 100644 --- a/app/(root)/page.tsx +++ b/app/(root)/page.tsx @@ -1,11 +1,8 @@ // "use client" import Categories from "@/components/Categories"; -import Header from "@/components/Header"; import ListingCard from "@/components/ListingCard"; -import { adts } from "@/data/adt"; import { prisma } from "@/prisma/prisma-client"; -import Image from "next/image"; export default async function Home() { const adts = await prisma.adt.findMany() @@ -28,7 +25,7 @@ export default async function Home() {
{adts.map((adt) => ( - + ))}
diff --git a/app/(root)/profile/page.tsx b/app/(root)/profile/page.tsx index 00837fd..abc1b4b 100644 --- a/app/(root)/profile/page.tsx +++ b/app/(root)/profile/page.tsx @@ -1,8 +1,6 @@ import React, { use } from 'react'; import { Settings, Package, Heart, Bell } from 'lucide-react'; import ListingCard from '@/components/ListingCard'; -import { adts } from '@/data/adt'; -import Header from '@/components/Header'; import { getUserSession } from '@/lib/get-user-session'; import { redirect } from 'next/navigation'; import { prisma } from '@/prisma/prisma-client'; @@ -76,7 +74,7 @@ export default async function Profile() {
{user?.adts.map((adt) => ( - + ))}
diff --git a/app/api/adt/route.ts b/app/api/adt/route.ts index 875e805..9d62885 100644 --- a/app/api/adt/route.ts +++ b/app/api/adt/route.ts @@ -1,8 +1,6 @@ import { NextResponse } from 'next/server'; import { PrismaClient } from '@prisma/client'; import { formAdtCreateSchema } from '@/components/shared/adt-create/schemas'; -import { getServerSession } from 'next-auth/next'; -import { authOptions } from '../auth/[...nextauth]/route'; import { getUserSession } from '@/lib/get-user-session'; const prisma = new PrismaClient(); diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index ec10efb..c447fa0 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -1,156 +1,8 @@ -import NextAuth, { AuthOptions } from "next-auth" -import GithubProvider from "next-auth/providers/github" -import CredentialsProvider from 'next-auth/providers/credentials'; -import { prisma } from "@/prisma/prisma-client"; -import { compare, hashSync } from "bcrypt"; -import { Role } from "@prisma/client"; +import NextAuth from "next-auth" +import { authOptions } from "@/constants/auth-options" -export const dynamic = 'force-dynamic'; +const handler = NextAuth(authOptions) -export const authOptions: AuthOptions = { - providers: [ - GithubProvider({ - clientId: process.env.GITHUB_ID || '', - clientSecret: process.env.GITHUB_SECRET || '', - profile(profile) { - return { - id: profile.id, - name: profile.name || profile.login, - email: profile.email, - image: profile.avatar_url, - role: 'USER' as Role - } - } - }), - CredentialsProvider({ - name: 'Credentials', - credentials: { - email: {label: 'Email', type: 'text'}, - password: {label: 'Password', type: 'password'}, - }, - async authorize(credentials) { - if (!credentials) { - return null; - } - - const values = { - email: credentials.email - } - - const findUser = await prisma.user.findFirst({ - where: values - }) - - if (!findUser) { - return null; - } - - const isPasswordValid = await compare(credentials.password, findUser.password); - - if (!isPasswordValid) { - return null; - } - - // verified // - - return { - id: findUser.id, - - email: findUser.email, - name: findUser.name, - role: findUser.role, - } - - - } - }) - ], - secret: process.env.NEXTAUTH_SECRET, - session: { - strategy: 'jwt' - }, - callbacks: { - async signIn({ user, account}) { - try { - if (account?.provaider === 'credentials') { - return true - } - - if (!user.email){ - return false - } - - const findUser = await prisma.user.findFirst({ - where: { - OR: [ - { provider: account?.provider, providerId: account?.providerId as string }, - { email: user.email } - ] - } - }) - - if (findUser) { - await prisma.user.update({ - where: { - id: findUser.id - }, - data: { - provider: account?.provider, - providerId: account?.providerAccountId - } - }) - return true - } - - await prisma.user.create({ - data: { - email: user.email, - name: user.name || 'User #' + user.id, - password: hashSync(user.id.toString(), 10), // ИЗМЕНИТЬ - provider: account?.provider, - providerId: account?.providerAccountId - } - }) - - return true; - - } catch (error) { - console.error('Error [SIGNIN]', error) - return false - } - }, - - async jwt({ token }) { - if (!token.email) { - return token; - } - - const findUser = await prisma.user.findFirst({ - where: { - email: token.email - } - }) - if (findUser) { - token.id = String(findUser.id); - token.email = String(findUser.email); - token.name = String(findUser.name); - token.role = String(findUser.role); - } - - return token - }, - session({ session, token }) { - if (session?.user) { - session.user.id = token.id, - // session.user.email = token.email, - session.user.role = token.role - } - - return session; - } - } -} - -export const handler = NextAuth(authOptions) - -export { handler as GET, handler as POST } \ No newline at end of file +// Экспортируем напрямую функции GET и POST, без промежуточной переменной +export const GET = handler +export const POST = handler \ No newline at end of file diff --git a/components/shared/profile-form.tsx b/components/shared/profile-form.tsx index f76c560..eee7f76 100644 --- a/components/shared/profile-form.tsx +++ b/components/shared/profile-form.tsx @@ -17,22 +17,22 @@ interface Props { } export const ProfileForm: React.FC = ({ data }) => { - const form = useForm({ + const form = useForm({ resolver: zodResolver(formRegisterSchema), defaultValues: { - name: data.name, + name: data.name || '', email: data.email, password: '', confirmPassword: '', }, }); - const onSubmit = async (data: TFormRegisterValues) => { + const onSubmit = async (formData: TFormRegisterValues) => { try { await updateUserInfo({ - email: data.email, - name: data.name, - password: data.password, + email: formData.email, + name: formData.name, + password: formData.password, }); // toast.error('Данные обновлены 📝', { diff --git a/constants/auth-options.ts b/constants/auth-options.ts new file mode 100644 index 0000000..c09f912 --- /dev/null +++ b/constants/auth-options.ts @@ -0,0 +1,151 @@ +import NextAuth, { AuthOptions } from "next-auth" +import GithubProvider from "next-auth/providers/github" +import CredentialsProvider from 'next-auth/providers/credentials'; +import { prisma } from "@/prisma/prisma-client"; +import { compare, hashSync } from "bcrypt"; +import { Role } from "@prisma/client"; + + +export const authOptions: AuthOptions = { + providers: [ + GithubProvider({ + clientId: process.env.GITHUB_ID || '', + clientSecret: process.env.GITHUB_SECRET || '', + profile(profile) { + return { + id: profile.id, + name: profile.name || profile.login, + email: profile.email, + image: profile.avatar_url, + role: 'USER' as Role + } + } + }), + CredentialsProvider({ + name: 'Credentials', + credentials: { + email: {label: 'Email', type: 'text'}, + password: {label: 'Password', type: 'password'}, + }, + async authorize(credentials) { + if (!credentials) { + return null; + } + + const values = { + email: credentials.email + } + + const findUser = await prisma.user.findFirst({ + where: values + }) + + if (!findUser) { + return null; + } + + const isPasswordValid = await compare(credentials.password, findUser.password); + + if (!isPasswordValid) { + return null; + } + + // verified // + + return { + id: findUser.id, + + email: findUser.email, + name: findUser.name, + role: findUser.role, + } + + + } + }) + ], + secret: process.env.NEXTAUTH_SECRET, + session: { + strategy: 'jwt' + }, + callbacks: { + async signIn({ user, account}) { + try { + if (account?.provaider === 'credentials') { + return true + } + + if (!user.email){ + return false + } + + const findUser = await prisma.user.findFirst({ + where: { + OR: [ + { provider: account?.provider, providerId: account?.providerId as string }, + { email: user.email } + ] + } + }) + + if (findUser) { + await prisma.user.update({ + where: { + id: findUser.id + }, + data: { + provider: account?.provider, + providerId: account?.providerAccountId + } + }) + return true + } + + await prisma.user.create({ + data: { + email: user.email, + name: user.name || 'User #' + user.id, + password: hashSync(user.id.toString(), 10), // ИЗМЕНИТЬ + provider: account?.provider, + providerId: account?.providerAccountId + } + }) + + return true; + + } catch (error) { + console.error('Error [SIGNIN]', error) + return false + } + }, + + async jwt({ token }) { + if (!token.email) { + return token; + } + + const findUser = await prisma.user.findFirst({ + where: { + email: token.email + } + }) + if (findUser) { + token.id = String(findUser.id); + token.email = String(findUser.email); + token.name = String(findUser.name); + token.role = String(findUser.role); + } + + return token + }, + session({ session, token }) { + if (session?.user) { + session.user.id = token.id; + // session.user.email = token.email; + session.user.role = token.role; + } + + return session; + } + } +} \ No newline at end of file diff --git a/lib/get-user-session.ts b/lib/get-user-session.ts index 9081391..1f71c50 100644 --- a/lib/get-user-session.ts +++ b/lib/get-user-session.ts @@ -1,4 +1,4 @@ -import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import { authOptions } from "@/constants/auth-options" import { getServerSession } from "next-auth" export const getUserSession = async () => {