Compare commits

..

7 commits

6 changed files with 19 additions and 25 deletions

View file

@ -35,7 +35,8 @@
"pwa", "pwa",
"api", "api",
"ux", "ux",
"route" "route",
"frontend"
], ],
"[ignore]": { "[ignore]": {
"editor.defaultFormatter": "foxundermoon.shell-format" "editor.defaultFormatter": "foxundermoon.shell-format"

View file

@ -18,7 +18,7 @@
"$fresh/": "https://deno.land/x/fresh@1.6.8/", "$fresh/": "https://deno.land/x/fresh@1.6.8/",
"$std/": "https://deno.land/std@0.208.0/", "$std/": "https://deno.land/std@0.208.0/",
"@cohabit/cohamail/": "./packages/@cohabit__cohamail@0.2.1/", "@cohabit/cohamail/": "./packages/@cohabit__cohamail@0.2.1/",
"@cohabit/ressources_manager/": "./packages/@cohabit__ressources_manager@0.1.0/", "@cohabit/ressources_manager/": "./packages/@cohabit__ressources_manager@0.1.2/",
"@jotsr/delayed": "jsr:@jotsr/delayed@^2.1.1", "@jotsr/delayed": "jsr:@jotsr/delayed@^2.1.1",
"@jotsr/smart-css-bundler": "jsr:@jotsr/smart-css-bundler@^0.3.0", "@jotsr/smart-css-bundler": "jsr:@jotsr/smart-css-bundler@^0.3.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2", "@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
@ -38,7 +38,7 @@
"compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
"workspaces": [ "workspaces": [
"packages/@cohabit__cohamail@0.2.1", "packages/@cohabit__cohamail@0.2.1",
"packages/@cohabit__ressources_manager@0.1.0" "packages/@cohabit__ressources_manager@0.1.2"
], ],
"unstable": ["kv"] "unstable": ["kv"]
} }

View file

@ -3,6 +3,7 @@ import { cssBundler } from '@jotsr/smart-css-bundler/fresh'
export default defineConfig({ export default defineConfig({
plugins: [ plugins: [
cssBundler(['./src/stylesheets/main.css'], { bundleSubDir: 'css' }), cssBundler(['./src/stylesheets/main.css'] //TODO fix bundler out paths , { bundleSubDir: 'css' }
),
], ],
}) })

View file

@ -35,7 +35,7 @@ export default function LoginForm() {
type LoginFormFields = { type LoginFormFields = {
email: string email: string
passkey: boolean passkey: 'on' | undefined
} }
async function connect(event: Event) { async function connect(event: Event) {
@ -46,7 +46,7 @@ async function connect(event: Event) {
try { try {
// User disable passkey // User disable passkey
if (!fields.passkey) { if (fields.passkey !== 'on') {
throw new Error('User refused passkey') throw new Error('User refused passkey')
} }

View file

@ -1,10 +1,10 @@
import 'npm:iterator-polyfill' import 'npm:iterator-polyfill'
// Polyfill AsyncIterator // Polyfill AsyncIterator
import { FreshContext, Handlers } from '$fresh/server.ts' import { FreshContext } from '$fresh/server.ts'
import { Contact, type Mail, send } from '@cohabit/cohamail/mod.ts' import { Contact, type Mail, send } from '@cohabit/cohamail/mod.ts'
import { magicLinkTemplate } from '@cohabit/cohamail/templates/mod.ts' import { magicLinkTemplate } from '@cohabit/cohamail/templates/mod.ts'
import { SessionStore } from '../../../src/session/mod.ts' import { SessionHandlers } from '../../../src/session/mod.ts'
import { respondApi } from '../../../src/utils.ts' import { respondApi } from '../../../src/utils.ts'
import { sleep } from '@jotsr/delayed' import { sleep } from '@jotsr/delayed'
import { User } from '@cohabit/ressources_manager/src/models/mod.ts' import { User } from '@cohabit/ressources_manager/src/models/mod.ts'
@ -25,7 +25,7 @@ export async function getUserByMail(email: string): Promise<User | undefined> {
return user return user
} }
export const handler: Handlers = { export const handler: SessionHandlers = {
async POST(request, ctx) { async POST(request, ctx) {
const { email } = await request.json() as { email: string } const { email } = await request.json() as { email: string }
@ -42,8 +42,7 @@ export const handler: Handlers = {
`${ctx.url.origin}/api/magiclink?token=${token}&redirect=/profil` `${ctx.url.origin}/api/magiclink?token=${token}&redirect=/profil`
// save token to session // save token to session
const session = SessionStore.getFromRequest(request) ctx.state.session.flash<MagicLinkInfos>(`MAGIC_LINK__${token}`, {
session?.flash<MagicLinkInfos>(`MAGIC_LINK__${token}`, {
email, email,
remoteId: remoteId(ctx), remoteId: remoteId(ctx),
timestamp: Date.now(), timestamp: Date.now(),
@ -70,22 +69,16 @@ export const handler: Handlers = {
) )
} }
}, },
async GET(request, ctx) { async GET(_request, ctx) {
const token = ctx.url.searchParams.get('token') const token = ctx.url.searchParams.get('token')
const redirect = ctx.url.searchParams.get('redirect') const redirect = ctx.url.searchParams.get('redirect')
const session = SessionStore.getFromRequest(request)
// no session datas
if (session === undefined) {
return respondApi('error', 'no session datas', 401)
}
// no token // no token
if (token === null) { if (token === null) {
return respondApi('error', 'no token provided', 400) return respondApi('error', 'no token provided', 400)
} }
// wrong or timeout token // wrong or timeout token
const entry = session.get<MagicLinkInfos>(`MAGIC_LINK__${token}`) const entry = ctx.state.session.get<MagicLinkInfos>(`MAGIC_LINK__${token}`)
const lifespan = Date.now() - 10 * 60 * 1_000 // ten minutes const lifespan = Date.now() - 10 * 60 * 1_000 // ten minutes
@ -96,7 +89,7 @@ export const handler: Handlers = {
// check remote id (same user/machine that has query the token) // check remote id (same user/machine that has query the token)
if (entry.remoteId === remoteId(ctx)) { if (entry.remoteId === remoteId(ctx)) {
const user = await getUserByMail(entry.email) const user = await getUserByMail(entry.email)
session.set('user', user) ctx.state.session.set('user', user)
if (redirect) { if (redirect) {
return Response.redirect(new URL(redirect, ctx.basePath)) return Response.redirect(new URL(redirect, ctx.basePath))

View file

@ -5,14 +5,15 @@ import groups from './mock/groups.json' with { type: 'json' }
import users from './mock/users.json' with { type: 'json' } import users from './mock/users.json' with { type: 'json' }
import { User } from '@cohabit/ressources_manager/src/models/mod.ts' import { User } from '@cohabit/ressources_manager/src/models/mod.ts'
import { MailAddress } from '@cohabit/ressources_manager/types.ts' import { MailAddress } from '@cohabit/ressources_manager/types.ts'
import { existsSync } from '$std/fs/exists.ts' import { exists } from '$std/fs/exists.ts'
const dbPath = './_fresh/db.sqlite' const dbPath = './_fresh/db.sqlite'
const dbExist = await exists(dbPath)
export const db = await Db.init(dbPath) export const db = await Db.init(dbPath)
//! Temp for demo only //! Temp for demo only
if (!sessionStorage.getItem('db-loaded') && !existsSync(dbPath)) { if (!dbExist) {
console.log('=============LOAD DB=============') console.log('=============LOAD DB=============')
// Load groups to DB // Load groups to DB
@ -37,6 +38,4 @@ if (!sessionStorage.getItem('db-loaded') && !existsSync(dbPath)) {
db.ressource.user.set([user]) db.ressource.user.set([user])
} }
sessionStorage.setItem('db-loaded', 'true')
} }