refactor(api): ♻️ use cxt session instead of getting it from request

This commit is contained in:
Julien Oculi 2024-06-20 13:17:12 +02:00
parent d8b79987ca
commit c6758721ec

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))