From f9c6646b5cbb94dc8eaefd705d6616cce8270615 Mon Sep 17 00:00:00 2001
From: Divyanshgupta030 <145568562+Divyanshgupta030@users.noreply.github.com>
Date: Tue, 26 Nov 2024 21:32:51 +0530
Subject: [PATCH] update auth to better-auth v1
---
.../components/custom/account-switcher.tsx | 33 +++++++++++---
apps/www/package.json | 2 +-
packages/auth/package.json | 4 +-
packages/auth/tsconfig.json | 2 +-
.../20241126154111_final_auth/migration.sql | 31 +++++++++++++
packages/database/prisma/schema.prisma | 36 +++++++++------
packages/typescript-config/base.json | 2 +-
pnpm-lock.yaml | 44 +++++++++----------
8 files changed, 106 insertions(+), 48 deletions(-)
create mode 100644 packages/database/prisma/migrations/20241126154111_final_auth/migration.sql
diff --git a/apps/www/components/custom/account-switcher.tsx b/apps/www/components/custom/account-switcher.tsx
index 884e986..1ef4127 100644
--- a/apps/www/components/custom/account-switcher.tsx
+++ b/apps/www/components/custom/account-switcher.tsx
@@ -1,6 +1,7 @@
"use client";
import { authClient } from "@/lib/auth-client";
import { Session } from "@repo/auth";
+import Link from "next/link";
import { useRouter } from "next/navigation";
interface Props {
session: Session[] | null;
@@ -8,10 +9,10 @@ interface Props {
}
export default function AccountSwitcher({ session, activeSession }: Props) {
const router = useRouter();
- const onSelect = async (sessionId: string) => {
- console.log(sessionId);
+ const onSelect = async (token: string) => {
+ console.log(token);
const active = await authClient.multiSession.setActive({
- sessionId: sessionId,
+ sessionToken: token,
});
console.log(active);
@@ -29,20 +30,27 @@ export default function AccountSwitcher({ session, activeSession }: Props) {
},
});
};
+
if (!activeSession || !session) {
return
loading sessions
;
}
+ const handleCurrentSignOut = async () => {
+ await authClient.multiSession.revoke({
+ sessionToken: activeSession?.session.token
+ });
+ window.location.reload();
+ };
return (
+
+ Logout
+
+
+
+ Add Account
+
+
);
}
diff --git a/apps/www/package.json b/apps/www/package.json
index 8c0a290..b0fd902 100644
--- a/apps/www/package.json
+++ b/apps/www/package.json
@@ -28,7 +28,7 @@
"@repo/db": "workspace:*",
"@repo/types": "workspace:*",
"@tabler/icons-react": "^3.21.0",
- "better-auth": "0.8.1-beta.1",
+ "better-auth": "1.0.5",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"contentlayer2": "^0.5.3",
diff --git a/packages/auth/package.json b/packages/auth/package.json
index 7f56722..f570ab5 100644
--- a/packages/auth/package.json
+++ b/packages/auth/package.json
@@ -10,9 +10,9 @@
"author": "",
"license": "ISC",
"dependencies": {
- "@repo/typescript-config": "workspace:*",
"@repo/db": "workspace:*",
- "better-auth": "0.8.6-beta.4",
+ "@repo/typescript-config": "workspace:*",
+ "better-auth": "1.0.5",
"better-call": "0.2.14-beta.3",
"oslo": "^1.2.1"
}
diff --git a/packages/auth/tsconfig.json b/packages/auth/tsconfig.json
index 449b2f8..8269794 100644
--- a/packages/auth/tsconfig.json
+++ b/packages/auth/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "@repo/typescript-config/base.json",
"include": ["src", "*.ts"],
- "exclude": ["node_modules"]
+ "exclude": ["node_modules"],
}
diff --git a/packages/database/prisma/migrations/20241126154111_final_auth/migration.sql b/packages/database/prisma/migrations/20241126154111_final_auth/migration.sql
new file mode 100644
index 0000000..c45f943
--- /dev/null
+++ b/packages/database/prisma/migrations/20241126154111_final_auth/migration.sql
@@ -0,0 +1,31 @@
+/*
+ Warnings:
+
+ - You are about to drop the column `expiresAt` on the `account` table. All the data in the column will be lost.
+ - A unique constraint covering the columns `[token]` on the table `session` will be added. If there are existing duplicate values, this will fail.
+ - Added the required column `createdAt` to the `account` table without a default value. This is not possible if the table is not empty.
+ - Added the required column `updatedAt` to the `account` table without a default value. This is not possible if the table is not empty.
+ - Added the required column `createdAt` to the `session` table without a default value. This is not possible if the table is not empty.
+ - Added the required column `token` to the `session` table without a default value. This is not possible if the table is not empty.
+ - Added the required column `updatedAt` to the `session` table without a default value. This is not possible if the table is not empty.
+
+*/
+-- AlterTable
+ALTER TABLE "account" DROP COLUMN "expiresAt",
+ADD COLUMN "accessTokenExpiresAt" TIMESTAMP(3),
+ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL,
+ADD COLUMN "refreshTokenExpiresAt" TIMESTAMP(3),
+ADD COLUMN "scope" TEXT,
+ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;
+
+-- AlterTable
+ALTER TABLE "session" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL,
+ADD COLUMN "token" TEXT NOT NULL,
+ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;
+
+-- AlterTable
+ALTER TABLE "verification" ADD COLUMN "updatedAt" TIMESTAMP(3),
+ALTER COLUMN "createdAt" DROP NOT NULL;
+
+-- CreateIndex
+CREATE UNIQUE INDEX "session_token_key" ON "session"("token");
diff --git a/packages/database/prisma/schema.prisma b/packages/database/prisma/schema.prisma
index 5fb6c0c..dbcf610 100644
--- a/packages/database/prisma/schema.prisma
+++ b/packages/database/prisma/schema.prisma
@@ -17,8 +17,8 @@ model User {
image String?
createdAt DateTime
updatedAt DateTime
- accounts Account[]
sessions Session[]
+ accounts Account[]
@@unique([email])
@@map("user")
@@ -27,35 +27,44 @@ model User {
model Session {
id String @id
expiresAt DateTime
+ token String
+ createdAt DateTime
+ updatedAt DateTime
ipAddress String?
userAgent String?
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
+ @@unique([token])
@@map("session")
}
model Account {
- id String @id
- accountId String
- providerId String
- userId String
- user User @relation(fields: [userId], references: [id], onDelete: Cascade)
- accessToken String?
- refreshToken String?
- idToken String?
- expiresAt DateTime?
- password String?
+ id String @id
+ accountId String
+ providerId String
+ userId String
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)
+ accessToken String?
+ refreshToken String?
+ idToken String?
+ accessTokenExpiresAt DateTime?
+ refreshTokenExpiresAt DateTime?
+ scope String?
+ password String?
+ createdAt DateTime
+ updatedAt DateTime
@@map("account")
}
model Verification {
- id String @id
+ id String @id
identifier String
value String
expiresAt DateTime
- createdAt DateTime
+ createdAt DateTime?
+ updatedAt DateTime?
@@map("verification")
}
@@ -65,5 +74,4 @@ model Trigger {
name String
email String
emailVerified Boolean
-
}
diff --git a/packages/typescript-config/base.json b/packages/typescript-config/base.json
index b465b48..179a7fc 100644
--- a/packages/typescript-config/base.json
+++ b/packages/typescript-config/base.json
@@ -14,6 +14,6 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
- "target": "ES2022"
+ "target": "ES2022",
}
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4dc93d3..b188a50 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -422,8 +422,8 @@ importers:
specifier: ^3.21.0
version: 3.22.0(react@19.0.0-rc-02c0e824-20241028)
better-auth:
- specifier: 0.8.1-beta.1
- version: 0.8.1-beta.1
+ specifier: 1.0.5
+ version: 1.0.5
class-variance-authority:
specifier: ^0.7.0
version: 0.7.0
@@ -510,8 +510,8 @@ importers:
specifier: workspace:*
version: link:../typescript-config
better-auth:
- specifier: 0.8.6-beta.4
- version: 0.8.6-beta.4
+ specifier: 1.0.5
+ version: 1.0.5
better-call:
specifier: 0.2.14-beta.3
version: 0.2.14-beta.3
@@ -3468,14 +3468,14 @@ packages:
better-auth@0.8.1-beta.1:
resolution: {integrity: sha512-IFBOPOoRooZoJ883UTA5AjpEiTcFCQNecKj5LJytYTv1E+g/F1AibwGMp7jLq2DJJSfuBdtcCn32Fc0qHroSZQ==}
- better-auth@0.8.6-beta.4:
- resolution: {integrity: sha512-Gw9BokQg1O0m5WxZocblb5kUAN1SPa/vyCKoJ5+3b7KNA2CH3Lr0ERsXQPlaZg8QLihJMxDcKpGWXXVxXTeQXw==}
+ better-auth@1.0.5:
+ resolution: {integrity: sha512-LjpPqpRNXdDp6+9USuk/kgd7db2Vs1oJuWfr0rhlcmpMK4rfz7yA1GxomBozVDpKwZHPA8fUpgm1r7NDjvLYUg==}
better-call@0.2.14-beta.3:
resolution: {integrity: sha512-lA54ETanzM0xUZnQt6lm3BdTr4gVDyAe1DNpCQSTYUnwnGGOmQG8ob0FYivVd0XHsHQK68r01GB5c6cUuu4llQ==}
- better-call@0.2.15-beta.4:
- resolution: {integrity: sha512-EHL7KkYPxaDNDD6ojHOTECA6yTsw7Ff5Mn3s3Dm35xWSrMm2BSAIzyK44KjBBmaozcDkust8lEFoja3ueMetxw==}
+ better-call@0.3.3-beta.2:
+ resolution: {integrity: sha512-pk+dXQGICPphvMKxl1VOWUUDXF4AYC4DQhLFLSBwS/pfgryhfnx1yJhGqApJzy8eJi5aN5+uLSWDGzKJaMuCGQ==}
binary-extensions@2.3.0:
resolution: {integrity: sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==}
@@ -9885,14 +9885,14 @@ snapshots:
transitivePeerDependencies:
- encoding
- better-auth@0.8.6-beta.4:
+ better-auth@1.0.5:
dependencies:
'@better-fetch/fetch': 1.1.12
'@noble/ciphers': 0.6.0
'@noble/hashes': 1.5.0
'@simplewebauthn/browser': 10.0.0
'@simplewebauthn/server': 10.0.1
- better-call: 0.2.15-beta.4
+ better-call: 0.3.3-beta.2
consola: 3.2.3
defu: 6.1.4
jose: 5.9.6
@@ -9913,7 +9913,7 @@ snapshots:
uncrypto: 0.1.3
zod: 3.23.8
- better-call@0.2.15-beta.4:
+ better-call@0.3.3-beta.2:
dependencies:
'@better-fetch/fetch': 1.1.12
rou3: 0.5.1
@@ -10592,8 +10592,8 @@ snapshots:
'@typescript-eslint/parser': 7.18.0(eslint@8.57.1)(typescript@5.6.3)
eslint: 8.57.1
eslint-import-resolver-node: 0.3.9
- eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1)
- eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1)
+ eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@8.57.1)
+ eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1)
eslint-plugin-jsx-a11y: 6.10.2(eslint@8.57.1)
eslint-plugin-react: 7.37.2(eslint@8.57.1)
eslint-plugin-react-hooks: 5.0.0(eslint@8.57.1)
@@ -10612,8 +10612,8 @@ snapshots:
'@typescript-eslint/parser': 7.18.0(eslint@8.57.1)(typescript@5.6.3)
eslint: 8.57.1
eslint-import-resolver-node: 0.3.9
- eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1)
- eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1)
+ eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@8.57.1)
+ eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1)
eslint-plugin-jsx-a11y: 6.10.2(eslint@8.57.1)
eslint-plugin-react: 7.37.2(eslint@8.57.1)
eslint-plugin-react-hooks: 5.0.0(eslint@8.57.1)
@@ -10664,19 +10664,19 @@ snapshots:
- eslint-import-resolver-webpack
- supports-color
- eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1):
+ eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@8.57.1):
dependencies:
'@nolyfill/is-core-module': 1.0.39
debug: 4.3.7
enhanced-resolve: 5.17.1
eslint: 8.57.1
- eslint-module-utils: 2.12.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1)
+ eslint-module-utils: 2.12.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1)
fast-glob: 3.3.2
get-tsconfig: 4.8.1
is-bun-module: 1.2.1
is-glob: 4.0.3
optionalDependencies:
- eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1)
+ eslint-plugin-import: 2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1)
transitivePeerDependencies:
- '@typescript-eslint/parser'
- eslint-import-resolver-node
@@ -10703,14 +10703,14 @@ snapshots:
transitivePeerDependencies:
- supports-color
- eslint-module-utils@2.12.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1):
+ eslint-module-utils@2.12.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1):
dependencies:
debug: 3.2.7
optionalDependencies:
'@typescript-eslint/parser': 7.18.0(eslint@8.57.1)(typescript@5.6.3)
eslint: 8.57.1
eslint-import-resolver-node: 0.3.9
- eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1)
+ eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@8.57.1)
transitivePeerDependencies:
- supports-color
@@ -10749,7 +10749,7 @@ snapshots:
- eslint-import-resolver-webpack
- supports-color
- eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1):
+ eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1):
dependencies:
'@rtsao/scc': 1.1.0
array-includes: 3.1.8
@@ -10760,7 +10760,7 @@ snapshots:
doctrine: 2.1.0
eslint: 8.57.1
eslint-import-resolver-node: 0.3.9
- eslint-module-utils: 2.12.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1)
+ eslint-module-utils: 2.12.0(@typescript-eslint/parser@7.18.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1)
hasown: 2.0.2
is-core-module: 2.15.1
is-glob: 4.0.3