Starting to put back the server part. Currently the registration and login are almost ready.
This commit is contained in:
parent
e8b521f122
commit
83ddaf19d4
|
|
@ -1,36 +0,0 @@
|
||||||
<template>
|
|
||||||
<div ref="container" aria-expanded="false" class="group flex h-screen overflow-hidden">
|
|
||||||
<div class="z-50 sm:hidden inline-block w-full h-auto border-b border-light-35 dark:border-dark-35">
|
|
||||||
<div class="p-2 border-e border-b border-light-35 dark:border-dark-35 bg-light-0 dark:bg-dark-0 cursor-pointer hover:bg-light-25 dark:hover:bg-dark-25">
|
|
||||||
<Icon icon="radix-icons:hamburger-menu" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="bg-light-0 sm:my-8 sm:py-3 dark:bg-dark-0 top-0 z-40 xl:w-96 sm:w-[15em] w-full border-r border-light-30 dark:border-dark-30 flex flex-col justify-between max-sm:absolute max-sm:-top-0 max-sm:-bottom-0 sm:left-0 max-sm:-left-full max-sm:group-aria-expanded:left-0 max-sm:transition-[left] py-8 max-sm:z-40">
|
|
||||||
<div class="relative bottom-6 flex flex-1 flex-col gap-4 xl:px-6 px-3">
|
|
||||||
<div class="flex justify-between items-center">
|
|
||||||
<NuxtLink class=" text-light-100 dark:text-dark-100 hover:text-opacity-70 max-sm:ps-6" aria-label="Accueil" :to="{ path: '/', force: true }"></NuxtLink>
|
|
||||||
<div class="flex gap-4 items-center">
|
|
||||||
<Tooltip message="Changer de theme" side="left"><ThemeSwitch /></Tooltip>
|
|
||||||
<Tooltip message="Se connecter" side="right">
|
|
||||||
<NuxtLink class="" :to="{ path: '/user/profile', force: true }">
|
|
||||||
<div class="bg-light-20 dark:bg-dark-20 hover:border-opacity-70 flex border p-px border-light-50 dark:border-dark-50">
|
|
||||||
<Icon icon="radix-icons:person" class="w-7 h-7 p-1" />
|
|
||||||
</div>
|
|
||||||
</NuxtLink>
|
|
||||||
</Tooltip>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="flex"></div>
|
|
||||||
<slot></slot>
|
|
||||||
</div>
|
|
||||||
<div class="xl:px-12 px-6 text-start text-xs text-light-60 dark:text-dark-60 relative top-6">
|
|
||||||
<NuxtLink class="hover:underline italic" :to="{ path: '/third-party', force: true }">Mentions légales</NuxtLink>
|
|
||||||
<p>Copyright Peaceultime - 2024</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script setup lang="ts">
|
|
||||||
import { Icon } from '@iconify/vue/dist/iconify.js';
|
|
||||||
</script>
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
<template>
|
<template>
|
||||||
<Label class="my-2">{{ label }}
|
<Label class="my-2 flex flex-1 items-center justify-between flex-col md:flex-row">
|
||||||
|
<span class="pb-1 md:p-0">{{ label }}</span>
|
||||||
<input :placeholder="placeholder" :disabled="disabled"
|
<input :placeholder="placeholder" :disabled="disabled"
|
||||||
class="mx-4 caret-light-50 dark:caret-dark-50 text-light-100 dark:text-dark-100 placeholder:text-light-50 dark:placeholder:text-dark-50
|
class="mx-4 caret-light-50 dark:caret-dark-50 text-light-100 dark:text-dark-100 placeholder:text-light-50 dark:placeholder:text-dark-50
|
||||||
bg-light-20 dark:bg-dark-20 appearance-none outline-none px-3 py-1 focus:shadow-raw transition-[box-shadow] focus:shadow-light-40 dark:focus:shadow-dark-40
|
bg-light-20 dark:bg-dark-20 appearance-none outline-none px-3 py-1 focus:shadow-raw transition-[box-shadow] focus:shadow-light-40 dark:focus:shadow-dark-40
|
||||||
border border-light-35 dark:border-dark-35 hover:border-light-50 dark:hover:border-dark-50 data-[disabled]:bg-light-20 dark:data-[disabled]:bg-dark-20 data-[disabled]:border-light-20 dark:data-[disabled]:border-dark-20"
|
border border-light-35 dark:border-dark-35 hover:border-light-50 dark:hover:border-dark-50 data-[disabled]:bg-light-20 dark:data-[disabled]:bg-dark-20 data-[disabled]:border-light-20 dark:data-[disabled]:border-dark-20"
|
||||||
:type="type" v-model="model" :data-disabled="disabled || undefined">
|
:type="type" v-model="model" :data-disabled="disabled || undefined" v-bind="$attrs">
|
||||||
</Label>
|
</Label>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
import ".dotenv/config";
|
import ".dotenv/config";
|
||||||
import { Database } from "bun:sqlite";
|
import { Database } from "bun:sqlite";
|
||||||
import { drizzle } from "drizzle-orm/bun-sqlite";
|
import { drizzle } from "drizzle-orm/bun-sqlite";
|
||||||
|
import * as schema from '../db/schema';
|
||||||
|
|
||||||
export default function useDatabase()
|
export default function useDatabase()
|
||||||
{
|
{
|
||||||
const sqlite = new Database(process.env.DB_FILE);
|
const sqlite = new Database(useRuntimeConfig().database);
|
||||||
const db = drizzle({ client: sqlite });
|
const db = drizzle({ client: sqlite, schema });
|
||||||
|
|
||||||
db.run("PRAGMA journal_mode = WAL;");
|
db.run("PRAGMA journal_mode = WAL;");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
export default function useToast()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
import type { UserSession, UserSessionComposable } from '~/types/auth'
|
||||||
|
|
||||||
|
const useSessionState = () => useState<UserSession>('nuxt-session', () => ({}))
|
||||||
|
const useAuthReadyState = () => useState('nuxt-auth-ready', () => false)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Composable to get back the user session and utils around it.
|
||||||
|
* @see https://github.com/atinux/nuxt-auth-utils
|
||||||
|
*/
|
||||||
|
export function useUserSession(): UserSessionComposable {
|
||||||
|
const sessionState = useSessionState()
|
||||||
|
const authReadyState = useAuthReadyState()
|
||||||
|
return {
|
||||||
|
ready: computed(() => authReadyState.value),
|
||||||
|
loggedIn: computed(() => Boolean(sessionState.value.user)),
|
||||||
|
user: computed(() => sessionState.value.user || null),
|
||||||
|
session: sessionState,
|
||||||
|
fetch,
|
||||||
|
clear,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetch() {
|
||||||
|
const authReadyState = useAuthReadyState()
|
||||||
|
useSessionState().value = await useRequestFetch()('/api/auth/session', {
|
||||||
|
headers: {
|
||||||
|
Accept: 'text/json',
|
||||||
|
},
|
||||||
|
retry: false,
|
||||||
|
}).catch(() => ({}))
|
||||||
|
if (!authReadyState.value) {
|
||||||
|
authReadyState.value = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clear() {
|
||||||
|
await $fetch('/api/auth/session', { method: 'DELETE' })
|
||||||
|
useSessionState().value = {}
|
||||||
|
useRouter().go(0);
|
||||||
|
}
|
||||||
25
db/schema.ts
25
db/schema.ts
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { relations } from 'drizzle-orm';
|
||||||
import { int, text, sqliteTable, type SQLiteTableExtraConfig, primaryKey, blob } from 'drizzle-orm/sqlite-core';
|
import { int, text, sqliteTable, type SQLiteTableExtraConfig, primaryKey, blob } from 'drizzle-orm/sqlite-core';
|
||||||
|
|
||||||
export const usersTable = sqliteTable("users", {
|
export const usersTable = sqliteTable("users", {
|
||||||
|
|
@ -5,11 +6,12 @@ export const usersTable = sqliteTable("users", {
|
||||||
username: text().notNull().unique(),
|
username: text().notNull().unique(),
|
||||||
email: text().notNull().unique(),
|
email: text().notNull().unique(),
|
||||||
hash: text().notNull().unique(),
|
hash: text().notNull().unique(),
|
||||||
state: int().default(0),
|
state: int().notNull().default(0),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const usersDataTable = sqliteTable("users_data", {
|
export const usersDataTable = sqliteTable("users_data", {
|
||||||
id: int().primaryKey().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
id: int().primaryKey().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
||||||
|
signin: int({ mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const userSessionsTable = sqliteTable("user_sessions", {
|
export const userSessionsTable = sqliteTable("user_sessions", {
|
||||||
|
|
@ -17,8 +19,8 @@ export const userSessionsTable = sqliteTable("user_sessions", {
|
||||||
user_id: int().notNull().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
user_id: int().notNull().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
||||||
timestamp: int({ mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
|
timestamp: int({ mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
|
||||||
}, (table): SQLiteTableExtraConfig => {
|
}, (table): SQLiteTableExtraConfig => {
|
||||||
return {
|
return {
|
||||||
pk: primaryKey({ columns: [ table.id, table.user_id ] }),
|
pk: primaryKey({ columns: [table.id, table.user_id] }),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -30,4 +32,19 @@ export const explorerContentTable = sqliteTable("explorer_content", {
|
||||||
content: blob({ mode: 'buffer' }),
|
content: blob({ mode: 'buffer' }),
|
||||||
navigable: int({ mode: 'boolean' }).default(true),
|
navigable: int({ mode: 'boolean' }).default(true),
|
||||||
private: int({ mode: 'boolean' }).default(false),
|
private: int({ mode: 'boolean' }).default(false),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const usersRelation = relations(usersTable, ({one, many}) => ({
|
||||||
|
data: one(usersDataTable, { fields: [usersTable.id], references: [usersDataTable.id], }),
|
||||||
|
session: many(userSessionsTable),
|
||||||
|
content: many(explorerContentTable),
|
||||||
|
}));
|
||||||
|
export const usersDataRelation = relations(usersDataTable, ({one}) => ({
|
||||||
|
users: one(usersTable, { fields: [usersDataTable.id], references: [usersTable.id], }),
|
||||||
|
}));
|
||||||
|
export const userSessionsRelation = relations(userSessionsTable, ({one}) => ({
|
||||||
|
users: one(usersTable, { fields: [userSessionsTable.user_id], references: [usersTable.id], }),
|
||||||
|
}));
|
||||||
|
export const explorerContentRelation = relations(explorerContentTable, ({one}) => ({
|
||||||
|
users: one(usersTable, { fields: [explorerContentTable.owner], references: [usersTable.id], }),
|
||||||
|
}));
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
-- Current sql file was generated after introspecting the database
|
||||||
|
-- If you want to run this migration please uncomment this code before executing migrations
|
||||||
|
/*
|
||||||
CREATE TABLE `explorer_content` (
|
CREATE TABLE `explorer_content` (
|
||||||
`path` text PRIMARY KEY NOT NULL,
|
`path` text PRIMARY KEY NOT NULL,
|
||||||
`owner` integer NOT NULL,
|
`owner` integer NOT NULL,
|
||||||
|
|
@ -30,6 +33,11 @@ CREATE TABLE `users` (
|
||||||
`state` integer DEFAULT 0
|
`state` integer DEFAULT 0
|
||||||
);
|
);
|
||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE UNIQUE INDEX `users_username_unique` ON `users` (`username`);--> statement-breakpoint
|
CREATE UNIQUE INDEX `users_hash_unique` ON `users` (`hash`);--> statement-breakpoint
|
||||||
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
|
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
|
||||||
CREATE UNIQUE INDEX `users_hash_unique` ON `users` (`hash`);
|
CREATE UNIQUE INDEX `users_username_unique` ON `users` (`username`);--> statement-breakpoint
|
||||||
|
CREATE TABLE `__drizzle_migrations` (
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
DROP TABLE `__drizzle_migrations`;--> statement-breakpoint
|
||||||
|
PRAGMA foreign_keys=OFF;--> statement-breakpoint
|
||||||
|
CREATE TABLE `__new_users` (
|
||||||
|
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
|
`username` text NOT NULL,
|
||||||
|
`email` text NOT NULL,
|
||||||
|
`hash` text NOT NULL,
|
||||||
|
`state` integer DEFAULT 0 NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
INSERT INTO `__new_users`("id", "username", "email", "hash", "state") SELECT "id", "username", "email", "hash", "state" FROM `users`;--> statement-breakpoint
|
||||||
|
DROP TABLE `users`;--> statement-breakpoint
|
||||||
|
ALTER TABLE `__new_users` RENAME TO `users`;--> statement-breakpoint
|
||||||
|
PRAGMA foreign_keys=ON;--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX `users_username_unique` ON `users` (`username`);--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX `users_hash_unique` ON `users` (`hash`);--> statement-breakpoint
|
||||||
|
ALTER TABLE `users_data` ADD `signin` integer NOT NULL;
|
||||||
|
|
@ -1,64 +1,65 @@
|
||||||
{
|
{
|
||||||
|
"id": "00000000-0000-0000-0000-000000000000",
|
||||||
|
"prevId": "",
|
||||||
"version": "6",
|
"version": "6",
|
||||||
"dialect": "sqlite",
|
"dialect": "sqlite",
|
||||||
"id": "ddf5d5b3-bf1e-4d8d-89cb-230f8e90137a",
|
|
||||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
|
||||||
"tables": {
|
"tables": {
|
||||||
"explorer_content": {
|
"explorer_content": {
|
||||||
"name": "explorer_content",
|
"name": "explorer_content",
|
||||||
"columns": {
|
"columns": {
|
||||||
"path": {
|
"path": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "path",
|
"name": "path",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": true,
|
"primaryKey": true,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"owner": {
|
"owner": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "owner",
|
"name": "owner",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"title": {
|
"title": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "title",
|
"name": "title",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"type": {
|
"type": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "type",
|
"name": "type",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"content": {
|
"content": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "content",
|
"name": "content",
|
||||||
"type": "blob",
|
"type": "blob",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": false,
|
"notNull": false
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"navigable": {
|
"navigable": {
|
||||||
|
"default": true,
|
||||||
|
"autoincrement": false,
|
||||||
"name": "navigable",
|
"name": "navigable",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": false,
|
"notNull": false
|
||||||
"autoincrement": false,
|
|
||||||
"default": true
|
|
||||||
},
|
},
|
||||||
"private": {
|
"private": {
|
||||||
|
"default": false,
|
||||||
|
"autoincrement": false,
|
||||||
"name": "private",
|
"name": "private",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": false,
|
"notNull": false
|
||||||
"autoincrement": false,
|
|
||||||
"default": false
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
"indexes": {},
|
"indexes": {},
|
||||||
"foreignKeys": {
|
"foreignKeys": {
|
||||||
"explorer_content_owner_users_id_fk": {
|
"explorer_content_owner_users_id_fk": {
|
||||||
|
|
@ -75,7 +76,6 @@
|
||||||
"onUpdate": "cascade"
|
"onUpdate": "cascade"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"compositePrimaryKeys": {},
|
|
||||||
"uniqueConstraints": {},
|
"uniqueConstraints": {},
|
||||||
"checkConstraints": {}
|
"checkConstraints": {}
|
||||||
},
|
},
|
||||||
|
|
@ -83,25 +83,34 @@
|
||||||
"name": "user_sessions",
|
"name": "user_sessions",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"user_id": {
|
"user_id": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "user_id",
|
"name": "user_id",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"timestamp": {
|
"timestamp": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "timestamp",
|
"name": "timestamp",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {
|
||||||
|
"user_sessions_id_user_id_pk": {
|
||||||
|
"columns": [
|
||||||
|
"id",
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"name": "user_sessions_id_user_id_pk"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"indexes": {},
|
"indexes": {},
|
||||||
|
|
@ -120,15 +129,6 @@
|
||||||
"onUpdate": "cascade"
|
"onUpdate": "cascade"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"compositePrimaryKeys": {
|
|
||||||
"user_sessions_id_user_id_pk": {
|
|
||||||
"columns": [
|
|
||||||
"id",
|
|
||||||
"user_id"
|
|
||||||
],
|
|
||||||
"name": "user_sessions_id_user_id_pk"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"uniqueConstraints": {},
|
"uniqueConstraints": {},
|
||||||
"checkConstraints": {}
|
"checkConstraints": {}
|
||||||
},
|
},
|
||||||
|
|
@ -136,13 +136,14 @@
|
||||||
"name": "users_data",
|
"name": "users_data",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": true,
|
"primaryKey": true,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
"indexes": {},
|
"indexes": {},
|
||||||
"foreignKeys": {
|
"foreignKeys": {
|
||||||
"users_data_id_users_id_fk": {
|
"users_data_id_users_id_fk": {
|
||||||
|
|
@ -159,7 +160,6 @@
|
||||||
"onUpdate": "cascade"
|
"onUpdate": "cascade"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"compositePrimaryKeys": {},
|
|
||||||
"uniqueConstraints": {},
|
"uniqueConstraints": {},
|
||||||
"checkConstraints": {}
|
"checkConstraints": {}
|
||||||
},
|
},
|
||||||
|
|
@ -167,47 +167,48 @@
|
||||||
"name": "users",
|
"name": "users",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
|
"autoincrement": true,
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": true,
|
"primaryKey": true,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": true
|
|
||||||
},
|
},
|
||||||
"username": {
|
"username": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "username",
|
"name": "username",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"email": {
|
"email": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "email",
|
"name": "email",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"hash": {
|
"hash": {
|
||||||
|
"autoincrement": false,
|
||||||
"name": "hash",
|
"name": "hash",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true
|
||||||
"autoincrement": false
|
|
||||||
},
|
},
|
||||||
"state": {
|
"state": {
|
||||||
|
"default": 0,
|
||||||
|
"autoincrement": false,
|
||||||
"name": "state",
|
"name": "state",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": false,
|
"notNull": false
|
||||||
"autoincrement": false,
|
|
||||||
"default": 0
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
"indexes": {
|
"indexes": {
|
||||||
"users_username_unique": {
|
"users_hash_unique": {
|
||||||
"name": "users_username_unique",
|
"name": "users_hash_unique",
|
||||||
"columns": [
|
"columns": [
|
||||||
"username"
|
"hash"
|
||||||
],
|
],
|
||||||
"isUnique": true
|
"isUnique": true
|
||||||
},
|
},
|
||||||
|
|
@ -218,16 +219,24 @@
|
||||||
],
|
],
|
||||||
"isUnique": true
|
"isUnique": true
|
||||||
},
|
},
|
||||||
"users_hash_unique": {
|
"users_username_unique": {
|
||||||
"name": "users_hash_unique",
|
"name": "users_username_unique",
|
||||||
"columns": [
|
"columns": [
|
||||||
"hash"
|
"username"
|
||||||
],
|
],
|
||||||
"isUnique": true
|
"isUnique": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"foreignKeys": {},
|
"foreignKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"__drizzle_migrations": {
|
||||||
|
"name": "__drizzle_migrations",
|
||||||
|
"columns": {},
|
||||||
"compositePrimaryKeys": {},
|
"compositePrimaryKeys": {},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {},
|
||||||
"uniqueConstraints": {},
|
"uniqueConstraints": {},
|
||||||
"checkConstraints": {}
|
"checkConstraints": {}
|
||||||
}
|
}
|
||||||
|
|
@ -238,8 +247,5 @@
|
||||||
"schemas": {},
|
"schemas": {},
|
||||||
"tables": {},
|
"tables": {},
|
||||||
"columns": {}
|
"columns": {}
|
||||||
},
|
|
||||||
"internal": {
|
|
||||||
"indexes": {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,252 @@
|
||||||
|
{
|
||||||
|
"version": "6",
|
||||||
|
"dialect": "sqlite",
|
||||||
|
"id": "0cca5664-1a0e-48ef-b70a-966b7a8142c7",
|
||||||
|
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||||
|
"tables": {
|
||||||
|
"explorer_content": {
|
||||||
|
"name": "explorer_content",
|
||||||
|
"columns": {
|
||||||
|
"path": {
|
||||||
|
"name": "path",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"owner": {
|
||||||
|
"name": "owner",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"name": "title",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"name": "type",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"content": {
|
||||||
|
"name": "content",
|
||||||
|
"type": "blob",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"navigable": {
|
||||||
|
"name": "navigable",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"private": {
|
||||||
|
"name": "private",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"explorer_content_owner_users_id_fk": {
|
||||||
|
"name": "explorer_content_owner_users_id_fk",
|
||||||
|
"tableFrom": "explorer_content",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"owner"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "cascade"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"user_sessions": {
|
||||||
|
"name": "user_sessions",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"timestamp": {
|
||||||
|
"name": "timestamp",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"user_sessions_user_id_users_id_fk": {
|
||||||
|
"name": "user_sessions_user_id_users_id_fk",
|
||||||
|
"tableFrom": "user_sessions",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "cascade"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {
|
||||||
|
"user_sessions_id_user_id_pk": {
|
||||||
|
"columns": [
|
||||||
|
"id",
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"name": "user_sessions_id_user_id_pk"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"users_data": {
|
||||||
|
"name": "users_data",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"signin": {
|
||||||
|
"name": "signin",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"users_data_id_users_id_fk": {
|
||||||
|
"name": "users_data_id_users_id_fk",
|
||||||
|
"tableFrom": "users_data",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "cascade"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"users": {
|
||||||
|
"name": "users",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"username": {
|
||||||
|
"name": "username",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"name": "email",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"hash": {
|
||||||
|
"name": "hash",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"state": {
|
||||||
|
"name": "state",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"users_username_unique": {
|
||||||
|
"name": "users_username_unique",
|
||||||
|
"columns": [
|
||||||
|
"username"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
},
|
||||||
|
"users_email_unique": {
|
||||||
|
"name": "users_email_unique",
|
||||||
|
"columns": [
|
||||||
|
"email"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
},
|
||||||
|
"users_hash_unique": {
|
||||||
|
"name": "users_hash_unique",
|
||||||
|
"columns": [
|
||||||
|
"hash"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"views": {},
|
||||||
|
"enums": {},
|
||||||
|
"_meta": {
|
||||||
|
"schemas": {},
|
||||||
|
"tables": {},
|
||||||
|
"columns": {}
|
||||||
|
},
|
||||||
|
"internal": {
|
||||||
|
"indexes": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,8 +5,15 @@
|
||||||
{
|
{
|
||||||
"idx": 0,
|
"idx": 0,
|
||||||
"version": "6",
|
"version": "6",
|
||||||
"when": 1730124775172,
|
"when": 1730822816801,
|
||||||
"tag": "0000_youthful_ma_gnuci",
|
"tag": "0000_lonely_the_renegades",
|
||||||
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 1,
|
||||||
|
"version": "6",
|
||||||
|
"when": 1730826510693,
|
||||||
|
"tag": "0001_lush_selene",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { relations } from "drizzle-orm/relations";
|
||||||
|
import { users, explorerContent, userSessions, usersData } from "./schema";
|
||||||
|
|
||||||
|
export const explorerContentRelations = relations(explorerContent, ({one}) => ({
|
||||||
|
user: one(users, {
|
||||||
|
fields: [explorerContent.owner],
|
||||||
|
references: [users.id]
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const usersRelations = relations(users, ({many}) => ({
|
||||||
|
explorerContents: many(explorerContent),
|
||||||
|
userSessions: many(userSessions),
|
||||||
|
usersData: many(usersData),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const userSessionsRelations = relations(userSessions, ({one}) => ({
|
||||||
|
user: one(users, {
|
||||||
|
fields: [userSessions.userId],
|
||||||
|
references: [users.id]
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const usersDataRelations = relations(usersData, ({one}) => ({
|
||||||
|
user: one(users, {
|
||||||
|
fields: [usersData.id],
|
||||||
|
references: [users.id]
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
import { sqliteTable, AnySQLiteColumn, foreignKey, text, integer, blob, primaryKey, uniqueIndex } from "drizzle-orm/sqlite-core"
|
||||||
|
import { sql } from "drizzle-orm"
|
||||||
|
|
||||||
|
export const explorerContent = sqliteTable("explorer_content", {
|
||||||
|
path: text().primaryKey().notNull(),
|
||||||
|
owner: integer().notNull().references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" } ),
|
||||||
|
title: text().notNull(),
|
||||||
|
type: text().notNull(),
|
||||||
|
content: blob(),
|
||||||
|
navigable: integer().default(true),
|
||||||
|
private: integer().default(false),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const userSessions = sqliteTable("user_sessions", {
|
||||||
|
id: integer().notNull(),
|
||||||
|
userId: integer("user_id").notNull().references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" } ),
|
||||||
|
timestamp: integer().notNull(),
|
||||||
|
},
|
||||||
|
(table) => {
|
||||||
|
return {
|
||||||
|
pk0: primaryKey({ columns: [table.id, table.userId], name: "user_sessions_id_user_id_pk"})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export const usersData = sqliteTable("users_data", {
|
||||||
|
id: integer().primaryKey().notNull().references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" } ),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const users = sqliteTable("users", {
|
||||||
|
id: integer().primaryKey({ autoIncrement: true }).notNull(),
|
||||||
|
username: text().notNull(),
|
||||||
|
email: text().notNull(),
|
||||||
|
hash: text().notNull(),
|
||||||
|
state: integer().default(0),
|
||||||
|
},
|
||||||
|
(table) => {
|
||||||
|
return {
|
||||||
|
hashUnique: uniqueIndex("users_hash_unique").on(table.hash),
|
||||||
|
emailUnique: uniqueIndex("users_email_unique").on(table.email),
|
||||||
|
usernameUnique: uniqueIndex("users_username_unique").on(table.username),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export const drizzleMigrations = sqliteTable("__drizzle_migrations", {
|
||||||
|
});
|
||||||
|
|
||||||
|
|
@ -1,4 +1,59 @@
|
||||||
<template>
|
<template>
|
||||||
<NavBar />
|
<CollapsibleRoot class="flex flex-1 flex-col" v-model="open">
|
||||||
<slot></slot>
|
<div class="z-50 sm:hidden flex w-full items-center justify-between h-12 border-b border-light-35 dark:border-dark-35">
|
||||||
</template>
|
<div class="flex items-center px-2">
|
||||||
|
<CollapsibleTrigger asChild>
|
||||||
|
<Button icon class="ms-2 !bg-transparent group">
|
||||||
|
<Icon class="group-data-[state=open]:hidden" icon="radix-icons:hamburger-menu" />
|
||||||
|
<Icon class="group-data-[state=closed]:hidden" icon="radix-icons:cross-1" />
|
||||||
|
</Button>
|
||||||
|
</CollapsibleTrigger>
|
||||||
|
<NuxtLink class=" text-light-100 dark:text-dark-100 hover:text-opacity-70 max-sm:ps-6" aria-label="Accueil" :to="{ path: '/', force: true }">Accueil</NuxtLink>
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center px-2">
|
||||||
|
<Tooltip message="Changer de theme" side="left"><ThemeSwitch /></Tooltip>
|
||||||
|
<Tooltip message="Se connecter" side="right">
|
||||||
|
<NuxtLink class="" :to="{ path: '/user/login', force: true }">
|
||||||
|
<div class="hover:border-opacity-70 flex">
|
||||||
|
<Icon icon="radix-icons:person" class="w-7 h-7 p-1" />
|
||||||
|
</div>
|
||||||
|
</NuxtLink>
|
||||||
|
</Tooltip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-1 flex-row relative">
|
||||||
|
<CollapsibleContent asChild forceMount>
|
||||||
|
<div class="bg-light-0 sm:my-8 sm:py-3 dark:bg-dark-0 z-40 xl:w-96 sm:w-[15em] w-full border-r border-light-30 dark:border-dark-30 flex flex-col justify-between max-sm:absolute max-sm:-top-0 max-sm:-bottom-0 sm:left-0 max-sm:data-[state=closed]:-left-full max-sm:transition-[left] py-8 max-sm:z-40 max-sm:data-[state=open]:left-0">
|
||||||
|
<div class="relative bottom-6 flex flex-1 flex-col gap-4 xl:px-6 px-3">
|
||||||
|
<div class="flex justify-between items-center max-sm:hidden">
|
||||||
|
<NuxtLink class=" text-light-100 dark:text-dark-100 hover:text-opacity-70 max-sm:ps-6" aria-label="Accueil" :to="{ path: '/', force: true }">
|
||||||
|
<Avatar src="/logo.svg" />
|
||||||
|
</NuxtLink>
|
||||||
|
<div class="flex gap-4 items-center">
|
||||||
|
<Tooltip message="Changer de theme" side="left"><ThemeSwitch /></Tooltip>
|
||||||
|
<Tooltip message="Se connecter" side="right">
|
||||||
|
<NuxtLink class="" :to="{ path: '/user/login', force: true }">
|
||||||
|
<div class="bg-light-20 dark:bg-dark-20 hover:border-opacity-70 flex border p-px border-light-50 dark:border-dark-50">
|
||||||
|
<Icon icon="radix-icons:person" class="w-7 h-7 p-1" />
|
||||||
|
</div>
|
||||||
|
</NuxtLink>
|
||||||
|
</Tooltip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="xl:px-12 px-6 text-start text-xs text-light-60 dark:text-dark-60 relative top-4">
|
||||||
|
<NuxtLink class="hover:underline italic" :to="{ path: '/third-party', force: true }">Mentions légales</NuxtLink>
|
||||||
|
<p>Copyright Peaceultime - 2024</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CollapsibleContent>
|
||||||
|
<slot></slot>
|
||||||
|
</div>
|
||||||
|
</CollapsibleRoot>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { Icon } from '@iconify/vue/dist/iconify.js';
|
||||||
|
|
||||||
|
const open = ref(true);
|
||||||
|
</script>
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
<template>
|
||||||
|
<div class="flex flex-1 items-center justify-center">
|
||||||
|
<div class="w-full md:w-auto h-full border-e border-light-35 dark:border-dark-35 bg-light-20 dark:bg-dark-20 md:p-8 xl:p-16 flex justify-center items-center">
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
|
<div class="hidden md:block flex-auto h-full"></div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
||||||
|
import { Database } from "bun:sqlite";
|
||||||
|
import { drizzle } from "drizzle-orm/bun-sqlite";
|
||||||
|
|
||||||
|
const sqlite = new Database("db.sqlite");
|
||||||
|
const db = drizzle(sqlite);
|
||||||
|
await migrate(db, { migrationsFolder: "./drizzle" });
|
||||||
|
|
@ -116,6 +116,12 @@ export default defineNuxtConfig({
|
||||||
tasks: true,
|
tasks: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
runtimeConfig: {
|
||||||
|
session: {
|
||||||
|
password: '699c46bd-9aaa-4364-ad01-510ee4fe7013'
|
||||||
|
},
|
||||||
|
database: 'db.sqlite'
|
||||||
|
},
|
||||||
security: {
|
security: {
|
||||||
rateLimiter: false,
|
rateLimiter: false,
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
||||||
|
|
@ -7,17 +7,19 @@
|
||||||
"@nuxtjs/color-mode": "^3.5.2",
|
"@nuxtjs/color-mode": "^3.5.2",
|
||||||
"@nuxtjs/tailwindcss": "^6.12.2",
|
"@nuxtjs/tailwindcss": "^6.12.2",
|
||||||
"@vueuse/nuxt": "^11.1.0",
|
"@vueuse/nuxt": "^11.1.0",
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
"drizzle-orm": "^0.35.3",
|
"drizzle-orm": "^0.35.3",
|
||||||
"nuxt": "^3.13.2",
|
"nuxt": "^3.13.2",
|
||||||
"nuxt-security": "^2.0.0",
|
"nuxt-security": "^2.0.0",
|
||||||
"radix-vue": "^1.9.8",
|
"radix-vue": "^1.9.8",
|
||||||
"vue": "latest",
|
"vue": "latest",
|
||||||
"vue-router": "latest"
|
"vue-router": "latest",
|
||||||
|
"zod": "^3.23.8"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "^1.1.12",
|
"@types/bun": "^1.1.12",
|
||||||
"better-sqlite3": "^11.5.0",
|
"better-sqlite3": "^11.5.0",
|
||||||
"bun-types": "^1.1.33",
|
"bun-types": "^1.1.34",
|
||||||
"drizzle-kit": "^0.26.2"
|
"drizzle-kit": "^0.26.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,86 @@
|
||||||
<template>
|
<template>
|
||||||
Login
|
<Head>
|
||||||
</template>
|
<Title>Connexion</Title>
|
||||||
|
</Head>
|
||||||
|
<div class="flex flex-1 flex-col justify-center items-center">
|
||||||
|
<ProseH4>Connexion</ProseH4>
|
||||||
|
<form @submit="() => submit()" class="flex flex-1 flex-col justify-center items-stretch">
|
||||||
|
<TextInput type="text" label="Utilisateur ou email" autocomplete="username" v-model="state.usernameOrEmail"/>
|
||||||
|
<TextInput type="password" label="Mot de passe" autocomplete="current-password" v-model="state.password"/>
|
||||||
|
<Button class="border border-light-35 dark:border-dark-35 self-center" :loading="status === 'pending'">Se connecter</Button>
|
||||||
|
<NuxtLink class="mt-4 text-center block text-sm font-semibold tracking-wide hover:text-accent-blue" :to="{ path: `/user/register`, force: true }">Pas de compte ?</NuxtLink>
|
||||||
|
</form>
|
||||||
|
<Toast :closeable="false" :content="toastMessage" :duration="10000" timer />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import type { ZodError } from 'zod';
|
||||||
|
import { schema, type Login } from '~/schemas/login';
|
||||||
|
|
||||||
|
definePageMeta({
|
||||||
|
layout: 'login',
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = reactive<Login>({
|
||||||
|
usernameOrEmail: '',
|
||||||
|
password: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data: result, status, error, refresh } = await useFetch('/api/auth/login', {
|
||||||
|
body: state,
|
||||||
|
immediate: false,
|
||||||
|
method: 'POST',
|
||||||
|
watch: false,
|
||||||
|
ignoreResponseError: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const toastMessage = ref('');
|
||||||
|
|
||||||
|
async function submit()
|
||||||
|
{
|
||||||
|
if(state.password === "")
|
||||||
|
return;
|
||||||
|
|
||||||
|
const data = schema.safeParse(state);
|
||||||
|
|
||||||
|
if(data.success)
|
||||||
|
{
|
||||||
|
await refresh();
|
||||||
|
|
||||||
|
const login = result.value;
|
||||||
|
if(!login || !login.success)
|
||||||
|
{
|
||||||
|
handleErrors(login?.error ?? error.value!);
|
||||||
|
}
|
||||||
|
else if(status.value === 'success' && login.success)
|
||||||
|
{
|
||||||
|
await navigateTo('/user/profile');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
handleErrors(data.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleErrors(error: Error | ZodError)
|
||||||
|
{
|
||||||
|
if(!error)
|
||||||
|
return;
|
||||||
|
|
||||||
|
status.value = 'error';
|
||||||
|
|
||||||
|
if(error.hasOwnProperty('issues'))
|
||||||
|
{
|
||||||
|
for(const err of (error as ZodError).issues)
|
||||||
|
{
|
||||||
|
toastMessage.value = err.message;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
toastMessage.value = error?.message ?? 'Erreur inconnue.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
@ -1,3 +1,155 @@
|
||||||
<template>
|
<template>
|
||||||
Register
|
<Head>
|
||||||
</template>
|
<Title>Inscription</Title>
|
||||||
|
</Head>
|
||||||
|
<div class="flex flex-1 flex-col justify-center items-center">
|
||||||
|
<ProseH4>Inscription</ProseH4>
|
||||||
|
<form @submit="() => submit()" class="flex flex-1 flex-col justify-center items-stretch p-4">
|
||||||
|
<TextInput type="text" label="Nom d'utilisateur" autocomplete="username" v-model="state.username"/>
|
||||||
|
<TextInput type="email" label="Email" autocomplete="email" v-model="state.email"/>
|
||||||
|
<TextInput type="password" label="Mot de passe" autocomplete="new-password" v-model="state.password"/>
|
||||||
|
<div class="flex flex-col font-light border border-light-35 dark:border-dark-35 px-4 py-2 max-w-96">
|
||||||
|
<span class="">Votre mot de passe doit respecter les critères de sécurité suivants
|
||||||
|
:</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedLength}">Entre 8 et 128
|
||||||
|
caractères</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedLowerUpper}">Au moins
|
||||||
|
une minuscule et une majuscule</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedDigit}">Au moins un
|
||||||
|
chiffre</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedSymbol}">Au moins un
|
||||||
|
caractère spécial parmis la liste suivante:
|
||||||
|
<pre class="text-wrap">! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~</pre>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<TextInput type="password" label="Confirmation du mot de passe" autocomplete="new-password" v-model="confirmPassword"/>
|
||||||
|
<Button class="border border-light-35 dark:border-dark-35 self-center" :loading="status === 'pending'">Se connecter</Button>
|
||||||
|
<NuxtLink class="mt-4 text-center block text-sm font-semibold tracking-wide hover:text-accent-blue" :to="{ path: `/user/register`, force: true }">Pas de compte ?</NuxtLink>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ZodError } from 'zod';
|
||||||
|
import { schema, type Registration } from '~/schemas/registration';
|
||||||
|
|
||||||
|
definePageMeta({
|
||||||
|
layout: 'login',
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = reactive<Registration>({
|
||||||
|
username: '',
|
||||||
|
email: '',
|
||||||
|
password: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const confirmPassword = ref("");
|
||||||
|
|
||||||
|
const checkedLength = computed(() => state.password.length >= 8 && state.password.length <= 128);
|
||||||
|
const checkedLowerUpper = computed(() => state.password.toLowerCase() !== state.password && state.password.toUpperCase() !== state.password);
|
||||||
|
const checkedDigit = computed(() => /[0-9]/.test(state.password));
|
||||||
|
const checkedSymbol = computed(() => " !\"#$%&'()*+,-./:;<=>?@[]^_`{|}~".split("").some(e => state.password.includes(e)));
|
||||||
|
|
||||||
|
const usernameError = ref("");
|
||||||
|
const emailError = ref("");
|
||||||
|
const generalError = ref("");
|
||||||
|
|
||||||
|
const { data: result, status, error, execute } = await useFetch('/api/auth/register', {
|
||||||
|
body: state,
|
||||||
|
immediate: false,
|
||||||
|
method: 'POST',
|
||||||
|
watch: false,
|
||||||
|
ignoreResponseError: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
async function submit()
|
||||||
|
{
|
||||||
|
if(state.password === "" || state.password !== confirmPassword.value)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const data = schema.safeParse(state);
|
||||||
|
|
||||||
|
if(data.success)
|
||||||
|
{
|
||||||
|
await execute()
|
||||||
|
|
||||||
|
const login = result.value;
|
||||||
|
if(!login || !login.success)
|
||||||
|
{
|
||||||
|
handleErrors(login?.error ?? error.value!);
|
||||||
|
}
|
||||||
|
else if(status.value === 'success' && login.success)
|
||||||
|
{
|
||||||
|
await navigateTo('/user/profile');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
handleErrors(data.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleErrors(error: Error | ZodError)
|
||||||
|
{
|
||||||
|
if(error.hasOwnProperty('issues'))
|
||||||
|
{
|
||||||
|
for(const err of (error as ZodError).issues)
|
||||||
|
{
|
||||||
|
if(err.path.includes('username'))
|
||||||
|
{
|
||||||
|
usernameError.value = err.message;
|
||||||
|
}
|
||||||
|
if(err.path.includes('email'))
|
||||||
|
{
|
||||||
|
emailError.value = err.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
generalError.value = error?.message ?? 'Erreur inconnue.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!-- <template>
|
||||||
|
|
||||||
|
<Head>
|
||||||
|
<Title>S'inscrire</Title>
|
||||||
|
</Head>
|
||||||
|
<div class="flex flex-1 justify-center items-center">
|
||||||
|
<div class="p-8 w-[48em] border border-light-35 dark:border-dark-35">
|
||||||
|
<form class="p-4 bg-light-20 dark:bg-dark-20">
|
||||||
|
<ProseH4>Inscription</ProseH4>
|
||||||
|
<InputField type="text" autocomplete="username" v-model="state.username"
|
||||||
|
placeholder="Entrez un nom d'utilisateur" title="Nom d'utilisateur" :error="usernameError"/>
|
||||||
|
<InputField type="text" autocomplete="email" v-model="state.email" placeholder="Entrez une addresse mail"
|
||||||
|
title="Adresse mail" :error="emailError"/>
|
||||||
|
<InputField type="password" autocomplete="new-password" v-model="state.password"
|
||||||
|
placeholder="Entrez un mot de passe" title="Mot de passe"
|
||||||
|
:error="!(checkedLength && checkedLowerUpper && checkedDigit && checkedSymbol)"/>
|
||||||
|
<div class="flex flex-col font-light">
|
||||||
|
<span class="">Votre mot de passe doit respecter les critères de sécurité suivants
|
||||||
|
:</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedLength}">Entre 8 et 128
|
||||||
|
caractères</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedLowerUpper}">Au moins
|
||||||
|
une minuscule et une majuscule</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedDigit}">Au moins un
|
||||||
|
chiffre</span>
|
||||||
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedSymbol}">Au moins un
|
||||||
|
caractère spécial parmis la liste suivante:
|
||||||
|
<pre>! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~</pre>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<InputField type="password" v-model="confirmPassword" placeholder="Confirmer le mot de passe" title="Confirmer le mot de passe" autocomplete="new-password"
|
||||||
|
:error="confirmPassword === '' || confirmPassword === state.password ? '' : 'Les mots de passe saisies ne sont pas identique'"/>
|
||||||
|
<span v-if="generalError" class="text-light-red dark:text-dark-red">{{ generalError }}</span>
|
||||||
|
<button class="m-auto block px-4 py-1 bg-light-20 dark:bg-dark-20 border border-light-40 dark:border-dark-40 hover:border-light-50 dark:hover:border-dark-50 active:relative active:top-[1px]">
|
||||||
|
<div v-if="status === 'pending'" class="loading"></div>
|
||||||
|
<template v-else>S'inscrire</template>
|
||||||
|
</button>
|
||||||
|
<NuxtLink class="mt-4 text-center block text-sm font-semibold tracking-wide hover:italic" :to="{ path: `/user/login`, force: true }">Vous avez déjà un compte ? Se connecter</NuxtLink>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template> -->
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const schema = z.object({
|
||||||
|
usernameOrEmail: z.string({ required_error: "Nom d'utilisateur ou email obligatoire" }),
|
||||||
|
password: z.string({ required_error: "Mot de passe obligatoire" }),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type Login = z.infer<typeof schema>;
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
function securePassword(password: string, ctx: z.RefinementCtx): void {
|
||||||
|
const lowercase = password.toLowerCase();
|
||||||
|
const uppercase = password.toUpperCase();
|
||||||
|
|
||||||
|
if(lowercase === password)
|
||||||
|
{
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message: "Votre mot de passe doit contenir au moins une majuscule",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if(uppercase === password)
|
||||||
|
{
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message: "Votre mot de passe doit contenir au moins une minuscule",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if(!/[0-9]/.test(password))
|
||||||
|
{
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message: "Votre mot de passe doit contenir au moins un chiffre",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if(!" !\"#$%&'()*+,-./:;<=>?@[]^_`{|}~".split("").some(e => password.includes(e)))
|
||||||
|
{
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message: "Votre mot de passe doit contenir au moins un symbole",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const schema = z.object({
|
||||||
|
username: z.string({ required_error: "Nom d'utilisateur obligatoire" }).min(3, "Votre nom d'utilisateur doit contenir au moins 3 caractères").max(32, "Votre nom d'utilisateur doit contenir au plus 32 caractères").superRefine((user, ctx) => {
|
||||||
|
const test = z.string().email().safeParse(user);
|
||||||
|
if(test.success)
|
||||||
|
{
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.invalid_string,
|
||||||
|
validation: 'email',
|
||||||
|
message: "Votre nom d'utilisateur ne peut pas être une addresse mail",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
email: z.string({ required_error: "Email obligatoire" }).email("Adresse mail invalide"),
|
||||||
|
password: z.string({ required_error: "Mot de passe obligatoire" }).min(8, "Votre mot de passe doit contenir au moins 8 caractères").max(128, "Votre mot de passe doit contenir au moins 8 caractères").superRefine(securePassword),
|
||||||
|
data: z.object({
|
||||||
|
|
||||||
|
}).partial().nullish(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type Registration = z.infer<typeof schema>;
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
import { schema } from '~/schemas/login';
|
||||||
|
import type { User, UserExtendedData, UserRawData, UserSession, UserSessionRequired } from '~/types/auth';
|
||||||
|
import { ZodError } from 'zod';
|
||||||
|
import { checkSession, logSession } from '~/server/utils/user';
|
||||||
|
import { usersTable } from '~/db/schema';
|
||||||
|
import { eq, or, sql } from 'drizzle-orm';
|
||||||
|
import type { BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite';
|
||||||
|
|
||||||
|
interface SuccessHandler
|
||||||
|
{
|
||||||
|
success: true;
|
||||||
|
session: UserSession;
|
||||||
|
}
|
||||||
|
interface ErrorHandler
|
||||||
|
{
|
||||||
|
success: false;
|
||||||
|
error: Error | ZodError<{
|
||||||
|
usernameOrEmail: string;
|
||||||
|
password: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
type Return = SuccessHandler | ErrorHandler;
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e): Promise<Return> => {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
const session = await getUserSession(e);
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const checkedSession = await checkSession(e, session);
|
||||||
|
|
||||||
|
if(checkedSession !== undefined)
|
||||||
|
return checkedSession;
|
||||||
|
|
||||||
|
const body = await readValidatedBody(e, schema.safeParse);
|
||||||
|
|
||||||
|
if (!body.success)
|
||||||
|
{
|
||||||
|
await clearUserSession(e);
|
||||||
|
|
||||||
|
setResponseStatus(e, 406);
|
||||||
|
return { success: false, error: body.error };
|
||||||
|
}
|
||||||
|
|
||||||
|
const hash = await Bun.password.hash(body.data.password);
|
||||||
|
const id = db.select({ id: usersTable.id, hash: usersTable.hash }).from(usersTable).where(or(eq(usersTable.username, sql.placeholder('username')), eq(usersTable.email, sql.placeholder('username')))).prepare().get({ username: body.data.usernameOrEmail });
|
||||||
|
|
||||||
|
if(!id || !id.id || !id.hash)
|
||||||
|
{
|
||||||
|
await clearUserSession(e);
|
||||||
|
|
||||||
|
setResponseStatus(e, 401);
|
||||||
|
return { success: false, error: new ZodError([{ code: 'custom', path: ['username'], message: 'Identifiant inconnu' }]) };
|
||||||
|
}
|
||||||
|
|
||||||
|
const valid = await Bun.password.verify(body.data.password, id.hash);
|
||||||
|
|
||||||
|
if(!valid)
|
||||||
|
{
|
||||||
|
await clearUserSession(e);
|
||||||
|
|
||||||
|
setResponseStatus(e, 401);
|
||||||
|
return { success: false, error: new ZodError([{ code: 'custom', path: ['password'], message: 'Mot de passe incorrect' }]) };
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = db.query.usersTable.findFirst({
|
||||||
|
columns: {
|
||||||
|
id: true,
|
||||||
|
email: true,
|
||||||
|
username: true,
|
||||||
|
state: true,
|
||||||
|
},
|
||||||
|
with: {
|
||||||
|
data: true,
|
||||||
|
},
|
||||||
|
where: (table) => eq(table.id, sql.placeholder('id'))
|
||||||
|
}).prepare().get({ id: id.id });
|
||||||
|
|
||||||
|
if(!user)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 401);
|
||||||
|
return { success: false, error: new Error('Données utilisateur introuvable') };
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await logSession(e, await setUserSession(e, {
|
||||||
|
user: {
|
||||||
|
...user.data,
|
||||||
|
email: user.email,
|
||||||
|
username: user.username,
|
||||||
|
state: user.state,
|
||||||
|
}
|
||||||
|
}) as UserSessionRequired);
|
||||||
|
|
||||||
|
setResponseStatus(e, 201);
|
||||||
|
return { success: true, session: data };
|
||||||
|
}
|
||||||
|
catch(err: any)
|
||||||
|
{
|
||||||
|
console.error(err);
|
||||||
|
|
||||||
|
await clearUserSession(e);
|
||||||
|
return { success: false, error: err as Error };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
import { count, eq, sql } from 'drizzle-orm';
|
||||||
|
import { ZodError, type ZodIssue } from 'zod';
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
import { usersDataTable, usersTable } from '~/db/schema';
|
||||||
|
import { schema } from '~/schemas/registration';
|
||||||
|
import { checkSession, logSession } from '~/server/utils/user';
|
||||||
|
import type { UserSession, UserSessionRequired } from '~/types/auth';
|
||||||
|
|
||||||
|
interface SuccessHandler
|
||||||
|
{
|
||||||
|
success: true;
|
||||||
|
session: UserSession;
|
||||||
|
}
|
||||||
|
interface ErrorHandler
|
||||||
|
{
|
||||||
|
success: false;
|
||||||
|
error: Error | ZodError<{
|
||||||
|
username: string;
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
type Return = SuccessHandler | ErrorHandler;
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e): Promise<Return> => {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
const session = await getUserSession(e);
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const checkedSession = await checkSession(e, session);
|
||||||
|
|
||||||
|
if(checkedSession !== undefined)
|
||||||
|
return checkedSession;
|
||||||
|
|
||||||
|
const body = await readValidatedBody(e, schema.safeParse);
|
||||||
|
|
||||||
|
if (!body.success)
|
||||||
|
{
|
||||||
|
await clearUserSession(e);
|
||||||
|
|
||||||
|
setResponseStatus(e, 406);
|
||||||
|
return { success: false, error: body.error };
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkUsername = db.select({ count: count() }).from(usersTable).where(eq(usersTable.username, sql.placeholder('username'))).prepare().get({ username: body.data.username });
|
||||||
|
const checkEmail = db.select({ count: count() }).from(usersTable).where(eq(usersTable.email, sql.placeholder('email'))).prepare().get({ email: body.data.email });
|
||||||
|
|
||||||
|
const errors: ZodIssue[] = [];
|
||||||
|
if(!checkUsername || checkUsername.count !== 0)
|
||||||
|
errors.push({ code: 'custom', path: ['username'], message: "Ce nom d'utilisateur est déjà utilisé" });
|
||||||
|
if(!checkEmail || checkEmail.count !== 0)
|
||||||
|
errors.push({ code: 'custom', path: ['email'], message: "Cette adresse mail est déjà utilisée" });
|
||||||
|
|
||||||
|
if(errors.length > 0)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 406);
|
||||||
|
return { success: false, error: new ZodError(errors) };
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const hash = await Bun.password.hash(body.data.password);
|
||||||
|
db.insert(usersTable).values({ username: sql.placeholder('username'), email: sql.placeholder('email'), hash: sql.placeholder('hash'), state: sql.placeholder('state') }).prepare().run({ username: body.data.username, email: body.data.email, hash, state: 0 });
|
||||||
|
const id = db.select({ id: usersTable.id }).from(usersTable).where(eq(usersTable.username, sql.placeholder('username'))).prepare().get({ username: body.data.username });
|
||||||
|
|
||||||
|
if(!id || !id.id)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 406);
|
||||||
|
return { success: false, error: new Error('Erreur de création de compte') };
|
||||||
|
}
|
||||||
|
|
||||||
|
db.insert(usersDataTable).values({ id: sql.placeholder('id') }).prepare().run({ id: id.id });
|
||||||
|
|
||||||
|
logSession(e, await setUserSession(e, { user: { id: id.id, username: body.data.username, email: body.data.email, state: 0, signin: new Date() } }) as UserSessionRequired);
|
||||||
|
|
||||||
|
setResponseStatus(e, 201);
|
||||||
|
return { success: true, session };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(err: any)
|
||||||
|
{
|
||||||
|
console.error(err);
|
||||||
|
|
||||||
|
await clearUserSession(e);
|
||||||
|
return { success: false, error: err as Error };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { eventHandler } from 'h3';
|
||||||
|
import { clearUserSession } from '~/server/utils/session';
|
||||||
|
|
||||||
|
export default eventHandler(async (event) => {
|
||||||
|
await clearUserSession(event);
|
||||||
|
|
||||||
|
return { loggedOut: true };
|
||||||
|
})
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { eventHandler } from 'h3'
|
||||||
|
import { getUserSession, sessionHooks } from '~/server/utils/session'
|
||||||
|
import type { UserSessionRequired } from '~/types/auth'
|
||||||
|
|
||||||
|
export default eventHandler(async (event) => {
|
||||||
|
const session = await getUserSession(event)
|
||||||
|
|
||||||
|
if (session.user) {
|
||||||
|
await sessionHooks.callHookParallel('fetch', session as UserSessionRequired, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
return session
|
||||||
|
})
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
import { ProjectSearch } from '~/types/api';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const query = getQuery(e);
|
||||||
|
|
||||||
|
const where = ["f.type != $type"];
|
||||||
|
const criteria: Record<string, any> = { $type: "Folder" };
|
||||||
|
|
||||||
|
if(query && query.owner !== undefined)
|
||||||
|
{
|
||||||
|
where.push("owner = $owner");
|
||||||
|
criteria["$owner"] = query.owner;
|
||||||
|
}
|
||||||
|
if(query && query.name !== undefined)
|
||||||
|
{
|
||||||
|
where.push("name = $name");
|
||||||
|
criteria["$name"] = query.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const content = db.query(`SELECT p.*, u.username, COUNT(f.path) as pages FROM explorer_projects p LEFT JOIN users u ON p.owner = u.id LEFT JOIN explorer_files f ON f.project = p.id WHERE ${where.join(" AND ")} GROUP BY p.id`).all(criteria) as ProjectSearch[];
|
||||||
|
|
||||||
|
if(content.length > 0)
|
||||||
|
{
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const project = getRouterParam(e, "projectId");
|
||||||
|
|
||||||
|
const where = ["id = $id"];
|
||||||
|
const criteria: Record<string, any> = { $id: project };
|
||||||
|
|
||||||
|
if (!!project) {
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const content = db.query(`SELECT * FROM explorer_projects WHERE ${where.join(" and ")}`).get(criteria) as Project;
|
||||||
|
|
||||||
|
if (content) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const project = getRouterParam(e, "projectId");
|
||||||
|
|
||||||
|
const where = ["project = $project"];
|
||||||
|
const criteria: Record<string, any> = { $project: project };
|
||||||
|
|
||||||
|
if (!!project) {
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const content = db.query(`SELECT * FROM explorer_projects WHERE ${where.join(" and ")}`).all(criteria) as Project[];
|
||||||
|
|
||||||
|
if (content.length > 0) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const project = getRouterParam(e, "projectId");
|
||||||
|
|
||||||
|
const where = ["project = $project"];
|
||||||
|
const criteria: Record<string, any> = { $project: project };
|
||||||
|
|
||||||
|
if (!!project) {
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const content = db.query(`SELECT * FROM explorer_projects WHERE ${where.join(" and ")}`).all(criteria) as Project[];
|
||||||
|
|
||||||
|
if (content.length > 0) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
import type { File } from '~/types/api';
|
||||||
|
|
||||||
|
export default defineCachedEventHandler(async (e) => {
|
||||||
|
const project = getRouterParam(e, "projectId");
|
||||||
|
const query = getQuery(e);
|
||||||
|
|
||||||
|
if(!project)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const where = ["project = $project"];
|
||||||
|
const criteria: Record<string, any> = { $project: project };
|
||||||
|
|
||||||
|
if(query && query.path !== undefined)
|
||||||
|
{
|
||||||
|
where.push("path = $path");
|
||||||
|
criteria["$path"] = query.path;
|
||||||
|
}
|
||||||
|
if(query && query.title !== undefined)
|
||||||
|
{
|
||||||
|
where.push("title = $title");
|
||||||
|
criteria["$title"] = query.title;
|
||||||
|
}
|
||||||
|
if(query && query.type !== undefined)
|
||||||
|
{
|
||||||
|
where.push("type = $type");
|
||||||
|
criteria["$type"] = query.type;
|
||||||
|
}
|
||||||
|
if (query && query.search !== undefined)
|
||||||
|
{
|
||||||
|
where.push("path LIKE $search");
|
||||||
|
criteria["$search"] = query.search;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(where.length > 1)
|
||||||
|
{
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const content = db.query(`SELECT * FROM explorer_files WHERE ${where.join(" and ")}`).all(criteria) as File[];
|
||||||
|
|
||||||
|
if(content.length > 0)
|
||||||
|
{
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
}, {
|
||||||
|
maxAge: 60*60*24,
|
||||||
|
getKey: (e) => `${getRouterParam(e, "projectId")}-${JSON.stringify(getQuery(e))}`
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
import type { CommentedFile, CommentSearch, File } from '~/types/api';
|
||||||
|
|
||||||
|
export default defineCachedEventHandler(async (e) => {
|
||||||
|
const project = getRouterParam(e, "projectId");
|
||||||
|
const path = decodeURIComponent(getRouterParam(e, "path") ?? '');
|
||||||
|
|
||||||
|
if(!project)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!path)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const where = ["project = $project", "path = $path"];
|
||||||
|
const criteria: Record<string, any> = { $project: project, $path: path };
|
||||||
|
|
||||||
|
if(where.length > 1)
|
||||||
|
{
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const content = db.query(`SELECT * FROM explorer_files WHERE ${where.join(" and ")}`).get(criteria) as File;
|
||||||
|
|
||||||
|
if(content !== undefined)
|
||||||
|
{
|
||||||
|
const comments = db.query(`SELECT comment.*, user.username FROM explorer_comments comment LEFT JOIN users user ON comment.user_id = user.id WHERE ${where.join(" and ")}`).all(criteria) as CommentSearch[];
|
||||||
|
|
||||||
|
return { ...content, comments } as CommentedFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
return;
|
||||||
|
}, {
|
||||||
|
maxAge: 60*60*24,
|
||||||
|
getKey: (e) => `file-${getRouterParam(e, "projectId")}-${getRouterParam(e, "path")}`
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
import { Navigation } from '~/types/api';
|
||||||
|
|
||||||
|
type NavigatioNExtension = Navigation & { owner: number, navigable: boolean };
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const project = getRouterParam(e, "projectId");
|
||||||
|
const { user } = await getUserSession(e);
|
||||||
|
|
||||||
|
if(!project)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const content = db.query(`SELECT "path", "title", "type", "order", "private", "navigable", "owner" FROM explorer_files WHERE project = ?1`).all(project!).sort((a: any, b: any) => a.path.length - b.path.length) as NavigatioNExtension[];
|
||||||
|
|
||||||
|
if(content.length > 0)
|
||||||
|
{
|
||||||
|
const navigation: Navigation[] = [];
|
||||||
|
|
||||||
|
for(const idx in content)
|
||||||
|
{
|
||||||
|
const item = content[idx];
|
||||||
|
if(!!item.private && (user?.id ?? -1) !== item.owner || !item.navigable)
|
||||||
|
{
|
||||||
|
delete content[idx];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = item.path.includes('/') ? item.path.substring(0, item.path.lastIndexOf('/')) : undefined;
|
||||||
|
|
||||||
|
if(parent && !content.find(e => e && e.path === parent))
|
||||||
|
{
|
||||||
|
delete content[idx];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(const item of content.filter(e => !!e))
|
||||||
|
{
|
||||||
|
addChild(navigation, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return navigation;
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
});
|
||||||
|
|
||||||
|
function addChild(arr: Navigation[], e: Navigation): void
|
||||||
|
{
|
||||||
|
const parent = arr.find(f => e.path.startsWith(f.path));
|
||||||
|
|
||||||
|
if(parent)
|
||||||
|
{
|
||||||
|
if(!parent.children)
|
||||||
|
parent.children = [];
|
||||||
|
|
||||||
|
addChild(parent.children, e);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
arr.push({ title: e.title, path: e.path, type: e.type, order: e.order, private: e.private });
|
||||||
|
arr.sort((a, b) => {
|
||||||
|
if(a.order && b.order)
|
||||||
|
return a.order - b.order;
|
||||||
|
return a.title.localeCompare(b.title);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
import type { Tag } from '~/types/api';
|
||||||
|
|
||||||
|
export default defineCachedEventHandler(async (e) => {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
const project = getRouterParam(e, "projectId");
|
||||||
|
const tag = decodeURIComponent(getRouterParam(e, "tag") ?? '');
|
||||||
|
|
||||||
|
if(!project)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!tag)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const where = ["project = $project", "tag = $tag"];
|
||||||
|
const criteria: Record<string, any> = { $project: project, $tag: tag };
|
||||||
|
|
||||||
|
const db = useDatabase();
|
||||||
|
const content = db.query(`SELECT * FROM explorer_tags WHERE ${where.join(" and ")}`).get(criteria) as Tag;
|
||||||
|
|
||||||
|
if(content !== undefined)
|
||||||
|
{
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
}
|
||||||
|
catch(err)
|
||||||
|
{
|
||||||
|
console.error(err);
|
||||||
|
setResponseStatus(e, 500);
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
maxAge: 60*60*24,
|
||||||
|
getKey: (e) => `tag-${getRouterParam(e, "projectId")}-${getRouterParam(e, "tag")}`
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
import useDatabase from '~/composables/useDatabase';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (e) => {
|
||||||
|
const query = getQuery(e);
|
||||||
|
|
||||||
|
if (query.search) {
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const projects = db.query(`SELECT p.*, u.username, COUNT(f.path) as pages FROM explorer_projects p LEFT JOIN users u ON p.owner = u.id LEFT JOIN explorer_files f ON f.project = p.id WHERE name LIKE ?1 AND f.type != "Folder" GROUP BY p.id`).all(query.search) as ProjectSearch[];
|
||||||
|
const files = db.query(`SELECT f.*, u.username, count(c.path) as comments FROM explorer_files f LEFT JOIN users u ON f.owner = u.id LEFT JOIN explorer_comments c ON c.project = f.project AND c.path = f.path WHERE title LIKE ?1 AND private = 0 AND type != "Folder" GROUP BY f.project, f.path`).all(query.search) as FileSearch[];
|
||||||
|
const users = db.query(`SELECT id, username FROM users WHERE username LIKE ?1`).all(query.search) as UserSearch[];
|
||||||
|
|
||||||
|
return {
|
||||||
|
projects,
|
||||||
|
files,
|
||||||
|
users
|
||||||
|
} as Search;
|
||||||
|
}
|
||||||
|
|
||||||
|
setResponseStatus(e, 404);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
import useDatabase from "~/composables/useDatabase";
|
||||||
|
import type { User } from "~/types/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler((e) => {
|
||||||
|
const id = getRouterParam(e, 'id');
|
||||||
|
|
||||||
|
if(!id)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
return db.query(`SELECT id, username, email, state, d.* FROM users u LEFT JOIN users_data d ON u.id = d.user_id WHERE u.id = ?1`).get(id) as User;
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
import useDatabase from "~/composables/useDatabase";
|
||||||
|
import type { CommentSearch } from "~/types/api";
|
||||||
|
|
||||||
|
export default defineEventHandler((e) => {
|
||||||
|
const id = getRouterParam(e, 'id');
|
||||||
|
|
||||||
|
if(!id)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
return db.query(`SELECT * FROM explorer_comments WHERE user_id = ?1`).all(id) as CommentSearch[];
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
import useDatabase from "~/composables/useDatabase";
|
||||||
|
import type { ProjectSearch } from "~/types/api";
|
||||||
|
|
||||||
|
export default defineEventHandler((e) => {
|
||||||
|
const id = getRouterParam(e, 'id');
|
||||||
|
|
||||||
|
if(!id)
|
||||||
|
{
|
||||||
|
setResponseStatus(e, 400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
return db.query(`SELECT p.*, count(f.path) as pages FROM explorer_projects p LEFT JOIN explorer_files f ON p.id = f.project WHERE p.owner = ?1`).all(id) as Omit<ProjectSearch, 'username'>[];
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
import useDatabase from "~/composables/useDatabase";
|
||||||
|
import { userSessionsTable as sessions } from "~/db/schema";
|
||||||
|
import { eq, and } from "drizzle-orm";
|
||||||
|
|
||||||
|
const monthAsMs = 60 * 60 * 24 * 30 * 1000;
|
||||||
|
|
||||||
|
export default defineNitroPlugin(() => {
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
sessionHooks.hook('fetch', async (session, event) => {
|
||||||
|
const result = await db.query.userSessionsTable.findFirst({
|
||||||
|
columns: {
|
||||||
|
timestamp: true,
|
||||||
|
},
|
||||||
|
where: and(eq(sessions.id, session.id as unknown as number), eq(sessions.user_id, session.user.id))
|
||||||
|
});
|
||||||
|
|
||||||
|
if(!result)
|
||||||
|
{
|
||||||
|
await clearUserSession(event);
|
||||||
|
throw createError({ statusCode: 401, message: 'Unauthorized' });
|
||||||
|
}
|
||||||
|
else if(result && result.timestamp && result.timestamp.getTime() < Date.now() - monthAsMs)
|
||||||
|
{
|
||||||
|
await clearUserSession(event);
|
||||||
|
throw createError({ statusCode: 401, message: 'Session has expired' });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await db.update(sessions).set({
|
||||||
|
timestamp: new Date(),
|
||||||
|
}).where(and(eq(sessions.id, session.id as unknown as number), eq(sessions.user_id, session.user.id)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
sessionHooks.hook('clear', async (session, event) => {
|
||||||
|
if(session.id && session.user)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await db.delete(sessions).where(and(eq(sessions.id, session.id as unknown as number), eq(sessions.user_id, session.user.id)));
|
||||||
|
}
|
||||||
|
catch(e) { }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,164 @@
|
||||||
|
import useDatabase from "~/composables/useDatabase";
|
||||||
|
import { extname, basename } from 'node:path';
|
||||||
|
import type { File, FileType, Tag } from '~/types/api';
|
||||||
|
import type { CanvasColor, CanvasContent } from "~/types/canvas";
|
||||||
|
|
||||||
|
const typeMapping: Record<string, FileType> = {
|
||||||
|
".md": "Markdown",
|
||||||
|
".canvas": "Canvas"
|
||||||
|
};
|
||||||
|
|
||||||
|
export default defineTask({
|
||||||
|
meta: {
|
||||||
|
name: 'sync',
|
||||||
|
description: 'Synchronise the project 1 with Obsidian',
|
||||||
|
},
|
||||||
|
async run(event) {
|
||||||
|
/* try {
|
||||||
|
const tree = await $fetch('https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/git/trees/master', {
|
||||||
|
method: 'get',
|
||||||
|
headers: {
|
||||||
|
accept: 'application/json',
|
||||||
|
},
|
||||||
|
params: {
|
||||||
|
recursive: true,
|
||||||
|
per_page: 1000,
|
||||||
|
}
|
||||||
|
}) as any;
|
||||||
|
|
||||||
|
const files: File[] = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e: any) => {
|
||||||
|
if(e.type === 'tree')
|
||||||
|
{
|
||||||
|
const title = basename(e.path);
|
||||||
|
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
|
||||||
|
const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/');
|
||||||
|
return {
|
||||||
|
path: path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
|
||||||
|
order: order && order[1] ? order[1] : 50,
|
||||||
|
title: order && order[2] ? order[2] : title,
|
||||||
|
type: 'Folder',
|
||||||
|
content: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const extension = extname(e.path);
|
||||||
|
const title = basename(e.path, extension);
|
||||||
|
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
|
||||||
|
const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/');
|
||||||
|
const content = (await $fetch(`https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/raw/${encodeURIComponent(e.path)}`));
|
||||||
|
|
||||||
|
return {
|
||||||
|
path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
|
||||||
|
order: order && order[1] ? order[1] : 50,
|
||||||
|
title: order && order[2] ? order[2] : title,
|
||||||
|
type: (typeMapping[extension] ?? 'File'),
|
||||||
|
content: reshapeContent(content as string, typeMapping[extension] ?? 'File')
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
let tags: Tag[] = [];
|
||||||
|
const tagFile = files.find(e => e.path === "tags");
|
||||||
|
|
||||||
|
if(tagFile)
|
||||||
|
{
|
||||||
|
const parsed = useMarkdown()(tagFile.content);
|
||||||
|
const titles = parsed.children.filter(e => e.type === 'element' && e.tagName.match(/h\d/));
|
||||||
|
for(let i = 0; i < titles.length; i++)
|
||||||
|
{
|
||||||
|
const start = titles[i].position?.start.offset ?? 0;
|
||||||
|
const end = titles.length === i + 1 ? tagFile.content.length : titles[i + 1].position.start.offset - 1;
|
||||||
|
tags.push({ tag: titles[i].properties.id, description: tagFile.content.substring(titles[i].position?.end.offset + 1, end) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
const oldFiles = db.prepare(`SELECT * FROM explorer_files WHERE project = ?1`).all('1') as File[];
|
||||||
|
const removeFiles = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`);
|
||||||
|
db.transaction((data: File[]) => data.forEach(e => removeFiles.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path)));
|
||||||
|
removeFiles.finalize();
|
||||||
|
|
||||||
|
const oldTags = db.prepare(`SELECT * FROM explorer_tags WHERE project = ?1`).all('1') as Tag[];
|
||||||
|
const removeTags = db.prepare(`DELETE FROM explorer_tags WHERE project = ?1 AND tag = ?2`);
|
||||||
|
db.transaction((data: Tag[]) => data.forEach(e => removeTags.run('1', e.tag)))(oldTags.filter(e => !tags.find(f => f.tag = e.tag)));
|
||||||
|
removeTags.finalize();
|
||||||
|
|
||||||
|
const insertFiles = db.prepare(`INSERT INTO explorer_files("project", "path", "owner", "title", "order", "type", "content") VALUES (1, $path, 1, $title, $order, $type, $content)`);
|
||||||
|
const updateFiles = db.prepare(`UPDATE explorer_files SET content = $content WHERE project = 1 AND path = $path`);
|
||||||
|
db.transaction((content) => {
|
||||||
|
for (const item of content) {
|
||||||
|
let order = item.order;
|
||||||
|
|
||||||
|
if (typeof order === 'string')
|
||||||
|
order = parseInt(item.order, 10);
|
||||||
|
|
||||||
|
if (isNaN(order))
|
||||||
|
order = 999;
|
||||||
|
|
||||||
|
if(oldFiles.find(e => item.path === e.path))
|
||||||
|
updateFiles.run({ $path: item.path, $content: item.content });
|
||||||
|
else
|
||||||
|
insertFiles.run({ $path: item.path, $title: item.title, $type: item.type, $content: item.content, $order: order });
|
||||||
|
}
|
||||||
|
})(files);
|
||||||
|
|
||||||
|
insertFiles.finalize();
|
||||||
|
updateFiles.finalize();
|
||||||
|
|
||||||
|
const insertTags = db.prepare(`INSERT INTO explorer_tags("project", "tag", "description") VALUES (1, $tag, $description)`);
|
||||||
|
const updateTags = db.prepare(`UPDATE explorer_tags SET description = $description WHERE project = 1 AND tag = $tag`);
|
||||||
|
db.transaction((content) => {
|
||||||
|
for (const item of content) {
|
||||||
|
if (oldTags.find(e => item.tag === e.tag))
|
||||||
|
updateTags.run({ $tag: item.tag, $description: item.description });
|
||||||
|
else
|
||||||
|
insertTags.run({ $tag: item.tag, $description: item.description });
|
||||||
|
}
|
||||||
|
})(tags);
|
||||||
|
|
||||||
|
insertTags.finalize();
|
||||||
|
updateTags.finalize();
|
||||||
|
|
||||||
|
useStorage('cache').clear();
|
||||||
|
|
||||||
|
return { result: true };
|
||||||
|
}
|
||||||
|
catch(e)
|
||||||
|
{
|
||||||
|
return { result: false };
|
||||||
|
} */
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
function reshapeContent(content: string, type: FileType): string | null
|
||||||
|
{
|
||||||
|
switch(type)
|
||||||
|
{
|
||||||
|
case "Markdown":
|
||||||
|
case "File":
|
||||||
|
return content;
|
||||||
|
case "Canvas":
|
||||||
|
const data = JSON.parse(content) as CanvasContent;
|
||||||
|
data.edges.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
|
||||||
|
data.nodes.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
|
||||||
|
return JSON.stringify(data);
|
||||||
|
default:
|
||||||
|
case 'Folder':
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function getColor(color: string): CanvasColor
|
||||||
|
{
|
||||||
|
const colors: Record<string, string> = {
|
||||||
|
'1': 'red',
|
||||||
|
'2': 'orange',
|
||||||
|
'3': 'yellow',
|
||||||
|
'4': 'green',
|
||||||
|
'5': 'cyan',
|
||||||
|
'6': 'purple',
|
||||||
|
};
|
||||||
|
if(colors.hasOwnProperty(color))
|
||||||
|
return { class: colors[color] };
|
||||||
|
else
|
||||||
|
return { hex: color };
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
{
|
{
|
||||||
"extends": "../.nuxt/tsconfig.server.json"
|
// https://nuxt.com/docs/guide/concepts/typescript
|
||||||
}
|
"extends": "../.nuxt/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"types": ["bun-types"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,110 @@
|
||||||
|
import type { H3Event, SessionConfig } from 'h3'
|
||||||
|
import { useSession, createError } from 'h3'
|
||||||
|
import { defu } from 'defu'
|
||||||
|
import { createHooks } from 'hookable'
|
||||||
|
import { useRuntimeConfig } from '#imports'
|
||||||
|
import type { UserSession, UserSessionRequired } from '~/types/auth'
|
||||||
|
|
||||||
|
export interface SessionHooks {
|
||||||
|
/**
|
||||||
|
* Called when fetching the session from the API
|
||||||
|
* - Add extra properties to the session
|
||||||
|
* - Throw an error if the session could not be verified (with a database for example)
|
||||||
|
*/
|
||||||
|
fetch: (session: UserSessionRequired, event: H3Event) => void | Promise<void>
|
||||||
|
/**
|
||||||
|
* Called before clearing the session
|
||||||
|
*/
|
||||||
|
clear: (session: UserSession, event: H3Event) => void | Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const sessionHooks = createHooks<SessionHooks>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the user session from the current request
|
||||||
|
* @param event The Request (h3) event
|
||||||
|
* @returns The user session
|
||||||
|
*/
|
||||||
|
export async function getUserSession(event: H3Event) {
|
||||||
|
const session = await _useSession(event);
|
||||||
|
|
||||||
|
if(!session.data || !session.data.id)
|
||||||
|
{
|
||||||
|
await session.update(defu({ id: session.id }, session.data));
|
||||||
|
}
|
||||||
|
|
||||||
|
return session.data;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set a user session
|
||||||
|
* @param event The Request (h3) event
|
||||||
|
* @param data User session data, please only store public information since it can be decoded with API calls
|
||||||
|
* @see https://github.com/atinux/nuxt-auth-utils
|
||||||
|
*/
|
||||||
|
export async function setUserSession(event: H3Event, data: UserSession) {
|
||||||
|
const session = await _useSession(event)
|
||||||
|
|
||||||
|
await session.update(defu(data, session.data))
|
||||||
|
|
||||||
|
return session.data
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace a user session
|
||||||
|
* @param event The Request (h3) event
|
||||||
|
* @param data User session data, please only store public information since it can be decoded with API calls
|
||||||
|
*/
|
||||||
|
export async function replaceUserSession(event: H3Event, data: UserSession) {
|
||||||
|
const session = await _useSession(event)
|
||||||
|
|
||||||
|
await session.clear()
|
||||||
|
await session.update(data)
|
||||||
|
|
||||||
|
return session.data
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the user session and removing the session cookie
|
||||||
|
* @param event The Request (h3) event
|
||||||
|
* @returns true if the session was cleared
|
||||||
|
*/
|
||||||
|
export async function clearUserSession(event: H3Event) {
|
||||||
|
const session = await _useSession(event)
|
||||||
|
|
||||||
|
await sessionHooks.callHookParallel('clear', session.data, event)
|
||||||
|
await session.clear()
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Require a user session, throw a 401 error if the user is not logged in
|
||||||
|
* @param event
|
||||||
|
* @param opts Options to customize the error message and status code
|
||||||
|
* @param opts.statusCode The status code to use for the error (defaults to 401)
|
||||||
|
* @param opts.message The message to use for the error (defaults to Unauthorized)
|
||||||
|
* @see https://github.com/atinux/nuxt-auth-utils
|
||||||
|
*/
|
||||||
|
export async function requireUserSession(event: H3Event, opts: { statusCode?: number, message?: string } = {}): Promise<UserSessionRequired> {
|
||||||
|
const userSession = await getUserSession(event)
|
||||||
|
|
||||||
|
if (!userSession.user) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: opts.statusCode || 401,
|
||||||
|
message: opts.message || 'Unauthorized',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return userSession as UserSessionRequired
|
||||||
|
}
|
||||||
|
|
||||||
|
let sessionConfig: SessionConfig
|
||||||
|
|
||||||
|
function _useSession(event: H3Event) {
|
||||||
|
if (!sessionConfig) {
|
||||||
|
const runtimeConfig = useRuntimeConfig(event)
|
||||||
|
|
||||||
|
sessionConfig = runtimeConfig.session;
|
||||||
|
}
|
||||||
|
return useSession<UserSession>(event, sessionConfig)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { eq, sql, and } from "drizzle-orm";
|
||||||
|
import useDatabase from "~/composables/useDatabase";
|
||||||
|
import { userSessionsTable } from "~/db/schema";
|
||||||
|
import type { Return } from "~/types/api";
|
||||||
|
import type { UserSession, UserSessionRequired } from "~/types/auth";
|
||||||
|
|
||||||
|
export function checkSession(e: H3Event<EventRequestHandler>, session: UserSession): Return | undefined
|
||||||
|
{
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
if(session.id && session.user?.id)
|
||||||
|
{
|
||||||
|
const sessionId = db.select({ user_id: userSessionsTable.user_id }).from(userSessionsTable).where(and(eq(userSessionsTable.id, sql.placeholder('id')), eq(userSessionsTable.user_id, sql.placeholder('user_id')))).prepare().get({ id: session.id, user_id: session.user.id })
|
||||||
|
|
||||||
|
if(sessionId && sessionId.user_id === session.user?.id)
|
||||||
|
{
|
||||||
|
return { success: true, session };
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
clearUserSession(e);
|
||||||
|
|
||||||
|
setResponseStatus(e, 406);
|
||||||
|
return { success: false, error: new Error('Vous êtes déjà connecté') };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function logSession(e: H3Event<EventRequestHandler>, session: UserSessionRequired): UserSessionRequired
|
||||||
|
{
|
||||||
|
const db = useDatabase();
|
||||||
|
|
||||||
|
db.insert(userSessionsTable).values({ id: sql.placeholder('id'), user_id: sql.placeholder('user_id'), timestamp: sql.placeholder('timestamp') }).prepare().execute({ id: session.id, user_id: session.user.id, timestamp: new Date()});
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
{
|
{
|
||||||
// https://nuxt.com/docs/guide/concepts/typescript
|
// https://nuxt.com/docs/guide/concepts/typescript
|
||||||
"extends": "./.nuxt/tsconfig.json"
|
"extends": "./.nuxt/tsconfig.json",
|
||||||
}
|
"compilerOptions": {
|
||||||
|
"types": ["bun-types"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
export interface SuccessHandler
|
||||||
|
{
|
||||||
|
success: true;
|
||||||
|
session: UserSession;
|
||||||
|
}
|
||||||
|
export interface ErrorHandler
|
||||||
|
{
|
||||||
|
success: false;
|
||||||
|
error: Error | ZodError;
|
||||||
|
}
|
||||||
|
export type Return = SuccessHandler | ErrorHandler;
|
||||||
|
|
||||||
|
export interface Project {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
owner: number;
|
||||||
|
home: string;
|
||||||
|
summary: string;
|
||||||
|
}
|
||||||
|
export interface Navigation {
|
||||||
|
title: string;
|
||||||
|
path: string;
|
||||||
|
type: string;
|
||||||
|
order: number;
|
||||||
|
private: boolean;
|
||||||
|
children?: Navigation[];
|
||||||
|
}
|
||||||
|
export type FileMetadata = Record<string, boolean | string | number>;
|
||||||
|
export type FileType = 'Markdown' | 'Canvas' | 'File' | 'Folder';
|
||||||
|
export interface File {
|
||||||
|
project: number;
|
||||||
|
path: string;
|
||||||
|
owner: number;
|
||||||
|
title: string;
|
||||||
|
order: number;
|
||||||
|
type: FileType;
|
||||||
|
content: string;
|
||||||
|
navigable: boolean;
|
||||||
|
private: boolean;
|
||||||
|
metadata: FileMetadata;
|
||||||
|
}
|
||||||
|
export interface Comment {
|
||||||
|
project: number;
|
||||||
|
path: number;
|
||||||
|
user_id: number;
|
||||||
|
sequence: number;
|
||||||
|
position: number;
|
||||||
|
length: number;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
export interface User {
|
||||||
|
id: number;
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
export interface Tag {
|
||||||
|
tag: string;
|
||||||
|
project: number;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type ProjectSearch = Project &
|
||||||
|
{
|
||||||
|
pages: number;
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
export type FileSearch = Omit<File, 'content'> &
|
||||||
|
{
|
||||||
|
comments: number;
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
export type CommentSearch = Comment &
|
||||||
|
{
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
export type UserSearch = User &
|
||||||
|
{
|
||||||
|
}
|
||||||
|
export type CommentedFile = File &
|
||||||
|
{
|
||||||
|
comments: CommentSearch[];
|
||||||
|
}
|
||||||
|
export interface Search {
|
||||||
|
projects: ProjectSearch[];
|
||||||
|
files: FileSearch[];
|
||||||
|
users: UserSearch[];
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
import type { ComputedRef, Ref } from 'vue'
|
||||||
|
|
||||||
|
import 'vue-router';
|
||||||
|
declare module 'vue-router'
|
||||||
|
{
|
||||||
|
interface RouteMeta
|
||||||
|
{
|
||||||
|
requiresAuth?: boolean;
|
||||||
|
guestsGoesTo?: string;
|
||||||
|
usersGoesTo?: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
import 'nuxt';
|
||||||
|
declare module 'nuxt'
|
||||||
|
{
|
||||||
|
interface RuntimeConfig
|
||||||
|
{
|
||||||
|
session: SessionConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserRawData {
|
||||||
|
id: number;
|
||||||
|
username: string;
|
||||||
|
email: string;
|
||||||
|
state: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserExtendedData {
|
||||||
|
signin: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type User = UserRawData & UserExtendedData;
|
||||||
|
|
||||||
|
export interface UserSession {
|
||||||
|
user?: User;
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserSessionRequired extends UserSession {
|
||||||
|
user: User;
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserSessionComposable {
|
||||||
|
/**
|
||||||
|
* Computed indicating if the auth session is ready
|
||||||
|
*/
|
||||||
|
ready: ComputedRef<boolean>
|
||||||
|
/**
|
||||||
|
* Computed indicating if the user is logged in.
|
||||||
|
*/
|
||||||
|
loggedIn: ComputedRef<boolean>
|
||||||
|
/**
|
||||||
|
* The user object if logged in, null otherwise.
|
||||||
|
*/
|
||||||
|
user: ComputedRef<User | null>
|
||||||
|
/**
|
||||||
|
* The session object.
|
||||||
|
*/
|
||||||
|
session: Ref<UserSession>
|
||||||
|
/**
|
||||||
|
* Fetch the user session from the server.
|
||||||
|
*/
|
||||||
|
fetch: () => Promise<void>
|
||||||
|
/**
|
||||||
|
* Clear the user session and remove the session cookie.
|
||||||
|
*/
|
||||||
|
clear: () => Promise<void>
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
export interface CanvasContent {
|
||||||
|
nodes: CanvasNode[];
|
||||||
|
edges: CanvasEdge[];
|
||||||
|
groups: CanvasGroup[];
|
||||||
|
}
|
||||||
|
export type CanvasColor = {
|
||||||
|
class?: string;
|
||||||
|
} & {
|
||||||
|
hex?: string;
|
||||||
|
}
|
||||||
|
export interface CanvasNode {
|
||||||
|
type: 'group' | 'text';
|
||||||
|
id: string;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
color?: CanvasColor;
|
||||||
|
label?: string;
|
||||||
|
text?: any;
|
||||||
|
};
|
||||||
|
export interface CanvasEdge {
|
||||||
|
id: string;
|
||||||
|
fromNode: string;
|
||||||
|
fromSide: 'bottom' | 'top' | 'left' | 'right';
|
||||||
|
toNode: string;
|
||||||
|
toSide: 'bottom' | 'top' | 'left' | 'right';
|
||||||
|
color?: CanvasColor;
|
||||||
|
label?: string;
|
||||||
|
};
|
||||||
|
export interface CanvasGroup {
|
||||||
|
name: string;
|
||||||
|
nodes: string[];
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue