From c90105741d3565e6fd62aa1fc9ec782e499ea6fa Mon Sep 17 00:00:00 2001 From: nain93 Date: Sun, 26 May 2024 17:42:24 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EB=AF=B8=EB=93=A4=EC=9B=A8=EC=96=B4,=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=95=84=EC=9B=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20240526080214_add_provider/migration.sql | 23 ++++++++++++++++ .../migration.sql | 20 ++++++++++++++ prisma/schema.prisma | 3 ++- src/app/(home)/page.tsx | 21 ++++++++++++++- src/app/api/auth/[...nextauth]/route.ts | 8 +++--- src/app/sign-in/actions.ts | 27 +++++++++++++------ src/app/sign-in/page.tsx | 12 ++++++--- src/middleware.ts | 14 ++++++++++ src/utils/session.ts | 2 +- src/utils/type.ts | 3 +++ 10 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 prisma/migrations/20240526080214_add_provider/migration.sql create mode 100644 prisma/migrations/20240526081731_remove_username_required/migration.sql create mode 100644 src/middleware.ts create mode 100644 src/utils/type.ts diff --git a/prisma/migrations/20240526080214_add_provider/migration.sql b/prisma/migrations/20240526080214_add_provider/migration.sql new file mode 100644 index 0000000..b795f41 --- /dev/null +++ b/prisma/migrations/20240526080214_add_provider/migration.sql @@ -0,0 +1,23 @@ +/* + Warnings: + + - Added the required column `provider` to the `AuthToken` table without a default value. This is not possible if the table is not empty. + +*/ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_AuthToken" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "token" TEXT NOT NULL, + "user_id" INTEGER NOT NULL, + "provider" TEXT NOT NULL, + "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" DATETIME NOT NULL, + CONSTRAINT "AuthToken_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +INSERT INTO "new_AuthToken" ("created_at", "id", "token", "updated_at", "user_id") SELECT "created_at", "id", "token", "updated_at", "user_id" FROM "AuthToken"; +DROP TABLE "AuthToken"; +ALTER TABLE "new_AuthToken" RENAME TO "AuthToken"; +CREATE UNIQUE INDEX "AuthToken_token_key" ON "AuthToken"("token"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/20240526081731_remove_username_required/migration.sql b/prisma/migrations/20240526081731_remove_username_required/migration.sql new file mode 100644 index 0000000..4250779 --- /dev/null +++ b/prisma/migrations/20240526081731_remove_username_required/migration.sql @@ -0,0 +1,20 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_User" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "username" TEXT, + "email" TEXT, + "password" TEXT, + "phone" TEXT, + "avatar" TEXT, + "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" DATETIME NOT NULL +); +INSERT INTO "new_User" ("avatar", "created_at", "email", "id", "password", "phone", "updated_at", "username") SELECT "avatar", "created_at", "email", "id", "password", "phone", "updated_at", "username" FROM "User"; +DROP TABLE "User"; +ALTER TABLE "new_User" RENAME TO "User"; +CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); +CREATE UNIQUE INDEX "User_phone_key" ON "User"("phone"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c917702..d2ef56c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -17,7 +17,7 @@ datasource db { model User{ id Int @id @default(autoincrement()) - username String @unique + username String? @unique email String? @unique password String? phone String? @unique @@ -32,6 +32,7 @@ model AuthToken{ token String @unique user User @relation(fields: [user_id], references: [id], onDelete: Cascade) user_id Int + provider String created_at DateTime @default(now()) updated_at DateTime @updatedAt } \ No newline at end of file diff --git a/src/app/(home)/page.tsx b/src/app/(home)/page.tsx index 448a29a..926c2b1 100644 --- a/src/app/(home)/page.tsx +++ b/src/app/(home)/page.tsx @@ -1,7 +1,26 @@ "use client"; +import { Button } from "@material-tailwind/react"; import React from "react"; +import { signOut } from "next-auth/react"; export default function Home() { - return
home
; + return ( +
+

HOME

+ +
+ ); } diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 7c0c64d..8f8e8fa 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,6 +1,7 @@ import GoogleProvider from "next-auth/providers/google"; import KakaoProvider from "next-auth/providers/kakao"; import NextAuth from "next-auth"; +import { createUser } from "@/app/sign-in/actions"; import getSession from "@/utils/session"; const handler = NextAuth({ @@ -11,11 +12,12 @@ const handler = NextAuth({ }, }, callbacks: { - async signIn() { + async signIn({ user, account }) { const cookie = await getSession(); - // TODO db에 token 저장 - // cookie.token = token; + + cookie.id = user.id; await cookie.save(); + await createUser({ provider: account?.provider ?? "" }); return true; }, }, diff --git a/src/app/sign-in/actions.ts b/src/app/sign-in/actions.ts index d34efe1..a243b89 100644 --- a/src/app/sign-in/actions.ts +++ b/src/app/sign-in/actions.ts @@ -1,17 +1,28 @@ +"use server"; + import db from "@/db"; +import getSession from "@/utils/session"; + +export async function createUser({ provider }: { provider: string }) { + const cookie = await getSession(); -export async function checkSignInToken(token: string) { - const user = await db.user.findUnique({ + const user = await db.authToken.findUnique({ where: { - username: "test", - // AuthToken: { - // some: { - // token, - // }, - // }, + token: cookie.id, }, select: { id: true, }, }); + /// 유저가 존재하면 리턴 + if (user) return; + + /// 없으면 유저 생성 + await db.authToken.create({ + data: { + token: cookie.id, + provider, + user: {}, + }, + }); } diff --git a/src/app/sign-in/page.tsx b/src/app/sign-in/page.tsx index d57d8d9..2bf3029 100644 --- a/src/app/sign-in/page.tsx +++ b/src/app/sign-in/page.tsx @@ -1,11 +1,15 @@ -import React, { useState } from "react"; +"use client"; import { Button } from "@material-tailwind/react"; +import { LoginProvider } from "@/utils/type"; import { signIn } from "next-auth/react"; +import { useState } from "react"; export default function SignIn() { const [loading, setLoading] = useState(false); - const handleLogin = async (loginProvider: "kakao" | "google") => { + const [provider, setProvider] = useState(null); + const handleLogin = async (loginProvider: LoginProvider) => { + setProvider(loginProvider); try { setLoading(true); await signIn(loginProvider, { @@ -25,7 +29,7 @@ export default function SignIn() {