Compare commits

...

7 commits

6 changed files with 19 additions and 25 deletions

View file

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

View file

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

View file

@ -3,6 +3,7 @@ import { cssBundler } from '@jotsr/smart-css-bundler/fresh'
export default defineConfig({
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 = {
email: string
passkey: boolean
passkey: 'on' | undefined
}
async function connect(event: Event) {
@ -46,7 +46,7 @@ async function connect(event: Event) {
try {
// User disable passkey
if (!fields.passkey) {
if (fields.passkey !== 'on') {
throw new Error('User refused passkey')
}

View file

@ -1,10 +1,10 @@
import 'npm:iterator-polyfill'
// 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 { 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 { sleep } from '@jotsr/delayed'
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
}
export const handler: Handlers = {
export const handler: SessionHandlers = {
async POST(request, ctx) {
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`
// save token to session
const session = SessionStore.getFromRequest(request)
session?.flash<MagicLinkInfos>(`MAGIC_LINK__${token}`, {
ctx.state.session.flash<MagicLinkInfos>(`MAGIC_LINK__${token}`, {
email,
remoteId: remoteId(ctx),
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 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
if (token === null) {
return respondApi('error', 'no token provided', 400)
}
// 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
@ -96,7 +89,7 @@ export const handler: Handlers = {
// check remote id (same user/machine that has query the token)
if (entry.remoteId === remoteId(ctx)) {
const user = await getUserByMail(entry.email)
session.set('user', user)
ctx.state.session.set('user', user)
if (redirect) {
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 { User } from '@cohabit/ressources_manager/src/models/mod.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 dbExist = await exists(dbPath)
export const db = await Db.init(dbPath)
//! Temp for demo only
if (!sessionStorage.getItem('db-loaded') && !existsSync(dbPath)) {
if (!dbExist) {
console.log('=============LOAD DB=============')
// Load groups to DB
@ -37,6 +38,4 @@ if (!sessionStorage.getItem('db-loaded') && !existsSync(dbPath)) {
db.ressource.user.set([user])
}
sessionStorage.setItem('db-loaded', 'true')
}