From 724a3af42f1df14de44f852fdd5d36175cd14d26 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Yusuf=20Akg=C3=BCl?= <s86116@bht-berlin.de>
Date: Sun, 4 Jun 2023 22:05:48 +0200
Subject: [PATCH] fix github auth

---
 components/user-auth-form.tsx |  7 +++--
 lib/auth.ts                   |  4 +--
 lib/session.ts                |  9 ++++++
 package-lock.json             | 58 +++++++++++++++++------------------
 package.json                  | 12 ++++----
 prisma/schema.prisma          | 10 +++---
 types/next-auth.d.ts          | 22 ++++++++-----
 7 files changed, 71 insertions(+), 51 deletions(-)
 create mode 100644 lib/session.ts

diff --git a/components/user-auth-form.tsx b/components/user-auth-form.tsx
index 0595588..f4f2072 100644
--- a/components/user-auth-form.tsx
+++ b/components/user-auth-form.tsx
@@ -2,6 +2,7 @@
 
 import { zodResolver } from "@hookform/resolvers/zod"
 import { signIn } from 'next-auth/react'
+import { useSearchParams } from "next/navigation"
 import { HTMLAttributes, useState } from 'react'
 import { useForm } from 'react-hook-form'
 import * as z from "zod"
@@ -32,6 +33,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
     })
     const [isLoading, setIsLoading] = useState<boolean>(false)
     const [isGitHubLoading, setIsGitHubLoading] = useState<boolean>(false)
+    const searchParams = useSearchParams()
 
     async function onSubmit(data: FormData) {
         setIsLoading(true)
@@ -62,11 +64,12 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
                 })
             }
         }
+
         const signInResult = await signIn("credentials", {
             usernameOrEmail: data.email?.toLowerCase() || data.usernameOrEmail?.toLowerCase(),
             password: data.password,
             redirect: true,
-            callbackUrl: "/home",
+            callbackUrl: searchParams?.get("from") || "/home",
         });
 
         setIsLoading(false)
@@ -205,7 +208,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
                 type="button"
                 onClick={() => {
                     setIsGitHubLoading(true)
-                    signIn("github")
+                    signIn("github", { callbackUrl: searchParams?.get("from") || "/home" })
                 }}
                 disabled={isLoading || isGitHubLoading}
             >
diff --git a/lib/auth.ts b/lib/auth.ts
index 9238719..7763e34 100644
--- a/lib/auth.ts
+++ b/lib/auth.ts
@@ -41,7 +41,7 @@ export const authOptions: NextAuthOptions = {
                     },
                 });
 
-                if (!user) {
+                if (!user || !user.password) {
                     return null
                 }
 
@@ -66,7 +66,7 @@ export const authOptions: NextAuthOptions = {
     callbacks: {
         async session({ token, session }) {
             if (token) {
-                session.user.id = token.id + ''
+                session.user.id = token.id
                 session.user.name = token.name
                 session.user.email = token.email
                 session.user.image = token.picture
diff --git a/lib/session.ts b/lib/session.ts
new file mode 100644
index 0000000..1bc9e23
--- /dev/null
+++ b/lib/session.ts
@@ -0,0 +1,9 @@
+import { getServerSession } from "next-auth/next"
+
+import { authOptions } from "@/lib/auth"
+
+export async function getCurrentUser() {
+    const session = await getServerSession(authOptions)
+
+    return session?.user
+}
\ No newline at end of file
diff --git a/package-lock.json b/package-lock.json
index 1e17dcd..f3aa7f9 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -22,7 +22,7 @@
         "bcrypt": "^5.1.0",
         "class-variance-authority": "^0.6.0",
         "clsx": "^1.2.1",
-        "lucide-react": "^0.224.0",
+        "lucide-react": "^0.234.0",
         "next": "^13.4.4",
         "next-auth": "^4.22.1",
         "next-themes": "^0.2.1",
@@ -30,7 +30,7 @@
         "react-dom": "18.2.0",
         "react-hook-form": "^7.44.3",
         "react-infinite-scroll-component": "^6.1.0",
-        "tailwind-merge": "^1.12.0",
+        "tailwind-merge": "^1.13.0",
         "tailwindcss-animate": "^1.0.5",
         "zod": "^3.21.4"
       },
@@ -38,15 +38,15 @@
         "@tanstack/eslint-plugin-query": "^4.29.9",
         "@types/bcrypt": "^5.0.0",
         "@types/node": "^20.2.5",
-        "@types/react": "^18.2.7",
+        "@types/react": "^18.2.8",
         "@types/react-dom": "^18.2.4",
         "autoprefixer": "10.4.14",
-        "eslint": "^8.41.0",
+        "eslint": "^8.42.0",
         "eslint-config-next": "^13.4.4",
         "postcss": "8.4.24",
         "prisma": "^4.15.0",
         "tailwindcss": "3.3.2",
-        "typescript": "^5.0.4"
+        "typescript": "^5.1.3"
       }
     },
     "node_modules/@alloc/quick-lru": {
@@ -171,9 +171,9 @@
       }
     },
     "node_modules/@eslint/js": {
-      "version": "8.41.0",
-      "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.41.0.tgz",
-      "integrity": "sha512-LxcyMGxwmTh2lY9FwHPGWOHmYFCZvbrFCBZL4FzSSsxsRPuhrYUg/49/0KDfW8tnIEaEHtfmn6+NPN+1DqaNmA==",
+      "version": "8.42.0",
+      "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.42.0.tgz",
+      "integrity": "sha512-6SWlXpWU5AvId8Ac7zjzmIOqMOba/JWY8XZ4A7q7Gn1Vlfg/SFFIlrtHXt9nPn4op9ZPAkl91Jao+QQv3r/ukw==",
       "dev": true,
       "engines": {
         "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
@@ -213,9 +213,9 @@
       }
     },
     "node_modules/@humanwhocodes/config-array": {
-      "version": "0.11.8",
-      "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz",
-      "integrity": "sha512-UybHIJzJnR5Qc/MsD9Kr+RpO2h+/P1GhOwdiLPXK5TWk5sgTdu88bTD9UP+CKbPPh5Rni1u0GjAdYQLemG8g+g==",
+      "version": "0.11.10",
+      "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.10.tgz",
+      "integrity": "sha512-KVVjQmNUepDVGXNuoRRdmmEjruj0KfiGSbS8LVc12LMsWDQzRXJ0qdhN8L8uUigKpfEHRhlaQFY0ib1tnUbNeQ==",
       "dev": true,
       "dependencies": {
         "@humanwhocodes/object-schema": "^1.2.1",
@@ -1369,9 +1369,9 @@
       "devOptional": true
     },
     "node_modules/@types/react": {
-      "version": "18.2.7",
-      "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.7.tgz",
-      "integrity": "sha512-ojrXpSH2XFCmHm7Jy3q44nXDyN54+EYKP2lBhJ2bqfyPj6cIUW/FZW/Csdia34NQgq7KYcAlHi5184m4X88+yw==",
+      "version": "18.2.8",
+      "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.8.tgz",
+      "integrity": "sha512-lTyWUNrd8ntVkqycEEplasWy2OxNlShj3zqS0LuB1ENUGis5HodmhM7DtCoUGbxj3VW/WsGA0DUhpG6XrM7gPA==",
       "devOptional": true,
       "dependencies": {
         "@types/prop-types": "*",
@@ -2467,16 +2467,16 @@
       }
     },
     "node_modules/eslint": {
-      "version": "8.41.0",
-      "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.41.0.tgz",
-      "integrity": "sha512-WQDQpzGBOP5IrXPo4Hc0814r4/v2rrIsB0rhT7jtunIalgg6gYXWhRMOejVO8yH21T/FGaxjmFjBMNqcIlmH1Q==",
+      "version": "8.42.0",
+      "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.42.0.tgz",
+      "integrity": "sha512-ulg9Ms6E1WPf67PHaEY4/6E2tEn5/f7FXGzr3t9cBMugOmf1INYvuUwwh1aXQN4MfJ6a5K2iNwP3w4AColvI9A==",
       "dev": true,
       "dependencies": {
         "@eslint-community/eslint-utils": "^4.2.0",
         "@eslint-community/regexpp": "^4.4.0",
         "@eslint/eslintrc": "^2.0.3",
-        "@eslint/js": "8.41.0",
-        "@humanwhocodes/config-array": "^0.11.8",
+        "@eslint/js": "8.42.0",
+        "@humanwhocodes/config-array": "^0.11.10",
         "@humanwhocodes/module-importer": "^1.0.1",
         "@nodelib/fs.walk": "^1.2.8",
         "ajv": "^6.10.0",
@@ -4047,9 +4047,9 @@
       }
     },
     "node_modules/lucide-react": {
-      "version": "0.224.0",
-      "resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.224.0.tgz",
-      "integrity": "sha512-2QuPhbEAicN1Ak9DSeViYhZLYogW54e802IFDasoy/AXKGrnzTcBLQM8gidXbOd2DSWbrLHDgN6n4340QzyujQ==",
+      "version": "0.234.0",
+      "resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.234.0.tgz",
+      "integrity": "sha512-7MtwK9zPXyvpHv9Tf0anraIjI9yRDdkfYIPt8KOC54NvimBAxbNpeIBb5/f+tZVIkN0hMgZdrAFugZe4YPZHcA==",
       "peerDependencies": {
         "react": "^16.5.1 || ^17.0.0 || ^18.0.0"
       }
@@ -5703,9 +5703,9 @@
       }
     },
     "node_modules/tailwind-merge": {
-      "version": "1.12.0",
-      "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-1.12.0.tgz",
-      "integrity": "sha512-Y17eDp7FtN1+JJ4OY0Bqv9OA41O+MS8c1Iyr3T6JFLnOgLg3EvcyMKZAnQ8AGyvB5Nxm3t9Xb5Mhe139m8QT/g==",
+      "version": "1.13.0",
+      "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-1.13.0.tgz",
+      "integrity": "sha512-mUTmDbcU+IhOvJ0c42eLQ/nRkvolTqfpVaVQRSxfJAv9TabS6Y2zW/1wKpKLdKzyL3Gh8j6NTLl6MWNmvOM6kA==",
       "funding": {
         "type": "github",
         "url": "https://github.com/sponsors/dcastil"
@@ -5924,15 +5924,15 @@
       }
     },
     "node_modules/typescript": {
-      "version": "5.0.4",
-      "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz",
-      "integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==",
+      "version": "5.1.3",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.3.tgz",
+      "integrity": "sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==",
       "bin": {
         "tsc": "bin/tsc",
         "tsserver": "bin/tsserver"
       },
       "engines": {
-        "node": ">=12.20"
+        "node": ">=14.17"
       }
     },
     "node_modules/unbox-primitive": {
diff --git a/package.json b/package.json
index 5dc490f..926c0c1 100644
--- a/package.json
+++ b/package.json
@@ -25,7 +25,7 @@
     "bcrypt": "^5.1.0",
     "class-variance-authority": "^0.6.0",
     "clsx": "^1.2.1",
-    "lucide-react": "^0.224.0",
+    "lucide-react": "^0.234.0",
     "next": "^13.4.4",
     "next-auth": "^4.22.1",
     "next-themes": "^0.2.1",
@@ -33,7 +33,7 @@
     "react-dom": "18.2.0",
     "react-hook-form": "^7.44.3",
     "react-infinite-scroll-component": "^6.1.0",
-    "tailwind-merge": "^1.12.0",
+    "tailwind-merge": "^1.13.0",
     "tailwindcss-animate": "^1.0.5",
     "zod": "^3.21.4"
   },
@@ -41,14 +41,14 @@
     "@tanstack/eslint-plugin-query": "^4.29.9",
     "@types/bcrypt": "^5.0.0",
     "@types/node": "^20.2.5",
-    "@types/react": "^18.2.7",
+    "@types/react": "^18.2.8",
     "@types/react-dom": "^18.2.4",
     "autoprefixer": "10.4.14",
-    "eslint": "^8.41.0",
+    "eslint": "^8.42.0",
     "eslint-config-next": "^13.4.4",
     "postcss": "8.4.24",
     "prisma": "^4.15.0",
     "tailwindcss": "3.3.2",
-    "typescript": "^5.0.4"
+    "typescript": "^5.1.3"
   }
-}
+}
\ No newline at end of file
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 70b33ff..c9a096a 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -12,7 +12,7 @@ datasource db {
 
 model Account {
   id                       String   @id @default(cuid())
-  userId                   String
+  userId                   String   @unique
   type                     String
   provider                 String
   providerAccountId        String
@@ -36,7 +36,7 @@ model Account {
 model Session {
   id           String   @id @default(cuid())
   sessionToken String   @unique
-  userId       String
+  userId       String   @unique
   expires      DateTime
 
   user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@ -47,10 +47,10 @@ model Session {
 model User {
   id            String    @id @default(cuid())
   name          String?
-  username      String?   @unique @map("username")
+  username      String?   @unique @map("usernames")
   email         String?   @unique
   emailVerified DateTime?
-  password      String
+  password      String?
   image         String?
   createdAt     DateTime  @default(now()) @map(name: "created_at")
   updatedAt     DateTime  @default(now()) @map(name: "updated_at")
@@ -64,6 +64,8 @@ model User {
 
   followers Follows[] @relation("follower")
   following Follows[] @relation("following")
+
+  @@map(name: "users")
 }
 
 model VerificationToken {
diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts
index 007c24c..19ff225 100644
--- a/types/next-auth.d.ts
+++ b/types/next-auth.d.ts
@@ -1,12 +1,18 @@
-import 'next-auth';
+import { User } from "next-auth"
+import { JWT } from "next-auth/jwt"
 
-declare module 'next-auth' {
+type UserId = string
+
+declare module "next-auth/jwt" {
+    interface JWT {
+        id: UserId
+    }
+}
+
+declare module "next-auth" {
     interface Session {
-        user: {
-            id: string;
-            name?: string | null;
-            email?: string | null;
-            image?: string | null;
-        };
+        user: User & {
+            id: UserId
+        }
     }
 }
\ No newline at end of file
-- 
GitLab