mirror of
https://github.com/ArthurDanjou/arthome.git
synced 2026-01-14 12:14:33 +01:00
Working
This commit is contained in:
@@ -7,7 +7,7 @@ export default defineEventHandler(async (event) => {
|
||||
.where(
|
||||
and(
|
||||
eq(tables.categories.id, id),
|
||||
eq(tables.categories.userId, user.id),
|
||||
eq(tables.categories.userId, user.user.id),
|
||||
),
|
||||
)
|
||||
return { statusCode: 200 }
|
||||
|
||||
@@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
|
||||
.where(
|
||||
and(
|
||||
eq(tables.categories.id, id),
|
||||
eq(tables.categories.userId, user.id),
|
||||
eq(tables.categories.userId, user.user.id),
|
||||
),
|
||||
)
|
||||
return { statusCode: 200 }
|
||||
|
||||
@@ -6,7 +6,7 @@ export default defineEventHandler(async (event) => {
|
||||
const user = await getUserSession(event)
|
||||
const body = await useValidatedBody(event, CreateCategorySchema)
|
||||
await useDrizzle().insert(tables.categories).values({
|
||||
userId: user.id,
|
||||
userId: user.user.id,
|
||||
...body,
|
||||
})
|
||||
return { statusCode: 200 }
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { user, session } = await requireUserSession(event)
|
||||
const { user } = await requireUserSession(event)
|
||||
|
||||
if (!user.avatar) {
|
||||
return sendNoContent(event, 204)
|
||||
|
||||
@@ -23,7 +23,7 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
const avatar = await hubBlob().put(filename, file, {
|
||||
addRandomSuffix: false,
|
||||
prefix: 'avatars/',
|
||||
prefix: 'avatars',
|
||||
})
|
||||
|
||||
const updatedUser = {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = await requireUserSession(event)
|
||||
return useDrizzle().query.users.findFirst({
|
||||
where: eq(tables.users.id, user.id),
|
||||
where: eq(tables.users.id, user.user.id),
|
||||
with: {
|
||||
categories: {
|
||||
with: {
|
||||
|
||||
@@ -11,7 +11,10 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
await updateUser(user.id, updatedUser)
|
||||
await replaceUserSession(event, updatedUser)
|
||||
await setUserSession(event, {
|
||||
id: user.id,
|
||||
user: updatedUser,
|
||||
})
|
||||
|
||||
return sendNoContent(event, 204)
|
||||
})
|
||||
@@ -11,8 +11,8 @@ CREATE TABLE IF NOT EXISTS "categories" (
|
||||
"icon" text DEFAULT 'i-ph:circle-wavy-question-duotone',
|
||||
"color" text DEFAULT 'gray',
|
||||
"user_id" integer NOT NULL,
|
||||
"created_at" timestamp (3) DEFAULT now(),
|
||||
"updated_at" timestamp (3)
|
||||
"created_at" timestamp(0) with time zone DEFAULT now(),
|
||||
"updated_at" timestamp(0) with time zone DEFAULT now()
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "tabs" (
|
||||
@@ -23,8 +23,8 @@ CREATE TABLE IF NOT EXISTS "tabs" (
|
||||
"color" text DEFAULT 'gray',
|
||||
"link" text DEFAULT '',
|
||||
"category_id" integer NOT NULL,
|
||||
"created_at" timestamp (3) DEFAULT now(),
|
||||
"updated_at" timestamp (3)
|
||||
"created_at" timestamp(0) with time zone DEFAULT now(),
|
||||
"updated_at" timestamp(0) with time zone DEFAULT now()
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "users" (
|
||||
@@ -42,8 +42,8 @@ CREATE TABLE IF NOT EXISTS "users" (
|
||||
"language" text DEFAULT 'en-EN',
|
||||
"location" text DEFAULT 'unknown',
|
||||
"subscription" "subscription" DEFAULT 'free',
|
||||
"created_at" timestamp (3) DEFAULT now(),
|
||||
"updated_at" timestamp (3),
|
||||
"created_at" timestamp(0) with time zone DEFAULT now(),
|
||||
"updated_at" timestamp(0) with time zone DEFAULT now(),
|
||||
CONSTRAINT "users_username_unique" UNIQUE("username"),
|
||||
CONSTRAINT "users_email_unique" UNIQUE("email"),
|
||||
CONSTRAINT "users_github_id_unique" UNIQUE("github_id"),
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"id": "37cfa6b0-d0e9-4999-9ead-06b419388528",
|
||||
"id": "21470761-4c33-4588-be9b-4927bbcbfe2c",
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
@@ -50,16 +50,17 @@
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamp (3)",
|
||||
"type": "timestamp(0) with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"default": "now()"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamp (3)",
|
||||
"type": "timestamp(0) with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
"notNull": false,
|
||||
"default": "now()"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
@@ -134,16 +135,17 @@
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamp (3)",
|
||||
"type": "timestamp(0) with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"default": "now()"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamp (3)",
|
||||
"type": "timestamp(0) with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
"notNull": false,
|
||||
"default": "now()"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
@@ -262,16 +264,17 @@
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamp (3)",
|
||||
"type": "timestamp(0) with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"default": "now()"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamp (3)",
|
||||
"type": "timestamp(0) with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
"notNull": false,
|
||||
"default": "now()"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
{
|
||||
"idx": 0,
|
||||
"version": "7",
|
||||
"when": 1725282814515,
|
||||
"tag": "0000_cloudy_lifeguard",
|
||||
"when": 1725302227098,
|
||||
"tag": "0000_noisy_randall_flagg",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
|
||||
@@ -3,28 +3,6 @@ export default oauthGitHubEventHandler({
|
||||
emailRequired: true,
|
||||
},
|
||||
async onSuccess(event, { user: oauthUser, tokens }) {
|
||||
const userSession = await getUserSession(event)
|
||||
|
||||
// If the user is already signed in, link the account
|
||||
if (userSession?.id) {
|
||||
const user = await findUserById(userSession.id)
|
||||
|
||||
if (user) {
|
||||
await updateUser(userSession.id, {
|
||||
githubId: oauthUser.id,
|
||||
githubToken: tokens.access_token,
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
id: userSession.id,
|
||||
user: userSession,
|
||||
githubId: oauthUser.id,
|
||||
})
|
||||
|
||||
return sendRedirect(event, '/')
|
||||
}
|
||||
}
|
||||
|
||||
// If the user is not signed in, search for an existing user with that GitHub ID
|
||||
// If it exists, sign in as that user and refresh the token
|
||||
let user = await findUserByGitHubId(oauthUser.id)
|
||||
@@ -35,7 +13,7 @@ export default oauthGitHubEventHandler({
|
||||
githubToken: tokens.access_token,
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
await replaceUserSession(event, {
|
||||
id: user.id,
|
||||
user,
|
||||
})
|
||||
@@ -76,9 +54,9 @@ export default oauthGitHubEventHandler({
|
||||
subscription: 'free',
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
await replaceUserSession(event, {
|
||||
id: createdUser.id,
|
||||
user: createdUser,
|
||||
user: createdUser[0],
|
||||
})
|
||||
|
||||
return sendRedirect(event, '/')
|
||||
|
||||
@@ -4,28 +4,6 @@ export default oauthGoogleEventHandler({
|
||||
scope: ['email', 'profile'],
|
||||
},
|
||||
async onSuccess(event, { user: oauthUser, tokens }) {
|
||||
const userSession = await getUserSession(event)
|
||||
|
||||
// If the user is already signed in, link the account
|
||||
if (userSession?.id) {
|
||||
const user = await findUserById(userSession.id)
|
||||
|
||||
if (user) {
|
||||
await updateUser(userSession.id, {
|
||||
googleId: oauthUser.sub,
|
||||
googleToken: tokens.access_token,
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
id: userSession.id,
|
||||
user: userSession,
|
||||
googleId: oauthUser.sub,
|
||||
})
|
||||
|
||||
return sendRedirect(event, '/')
|
||||
}
|
||||
}
|
||||
|
||||
// If the user is not signed in, search for an existing user with that Google ID
|
||||
// If it exists, sign in as that user and refresh the token
|
||||
let user = await findUserByGoogleId(oauthUser.sub)
|
||||
@@ -36,7 +14,7 @@ export default oauthGoogleEventHandler({
|
||||
googleToken: tokens.access_token,
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
await replaceUserSession(event, {
|
||||
id: user.id,
|
||||
user,
|
||||
})
|
||||
@@ -77,9 +55,9 @@ export default oauthGoogleEventHandler({
|
||||
subscription: 'free',
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
await replaceUserSession(event, {
|
||||
id: createdUser.id,
|
||||
user: createdUser,
|
||||
user: createdUser[0],
|
||||
})
|
||||
|
||||
return sendRedirect(event, '/')
|
||||
|
||||
@@ -3,8 +3,8 @@ import { serial, timestamp } from 'drizzle-orm/pg-core'
|
||||
* A centralized list of standardized Drizzle ORM schema field definitions to prevent duplication errors
|
||||
*/
|
||||
|
||||
export const createdAt = timestamp('created_at', { mode: 'date', precision: 3 }).defaultNow()
|
||||
export const updatedAt = timestamp('updated_at', { mode: 'date', precision: 3 }).$onUpdate(() => new Date())
|
||||
export const createdAt = timestamp('created_at', { mode: 'string', withTimezone: true, precision: 0 }).defaultNow()
|
||||
export const updatedAt = timestamp('updated_at', { mode: 'string', withTimezone: true, precision: 0 }).defaultNow().$onUpdateFn(() => sql`(current_timestamp)`)
|
||||
export const id = serial('id').primaryKey()
|
||||
|
||||
export const timestamps = {
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
import type { SQL } from 'drizzle-orm'
|
||||
import type { UserInsert } from '~~/server/utils/db'
|
||||
|
||||
export async function findUserById(userId: number) {
|
||||
return useDrizzle()
|
||||
.query
|
||||
.users
|
||||
.findFirst({
|
||||
where: eq(tables.users.id, userId),
|
||||
})
|
||||
}
|
||||
|
||||
export async function findUserByGitHubId(githubId: number) {
|
||||
return useDrizzle()
|
||||
.query
|
||||
@@ -41,18 +32,21 @@ export async function createUser(user: UserInsert) {
|
||||
return useDrizzle()
|
||||
.insert(tables.users)
|
||||
.values(user)
|
||||
.onConflictDoNothing()
|
||||
.returning()
|
||||
}
|
||||
|
||||
export async function updateUser(userId: number, user: Partial<UserInsert>) {
|
||||
await useDrizzle()
|
||||
.update(tables.users)
|
||||
.set(user)
|
||||
.set({
|
||||
...user,
|
||||
})
|
||||
.where(eq(tables.users.id, userId))
|
||||
}
|
||||
|
||||
export async function deleteProfilePicture(avatar: string) {
|
||||
if (avatar.startsWith('avatars/')) {
|
||||
if (avatar.startsWith('avatars')) {
|
||||
await hubBlob().del(avatar)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user