feat: gestion-utilisateur #1
14
front/src/env.d.ts
vendored
14
front/src/env.d.ts
vendored
@ -1,11 +1,25 @@
|
||||
/// <reference path="../.astro/types.d.ts" />
|
||||
/// <reference types="astro/client" />
|
||||
|
||||
import PocketBase from 'pocketbase'
|
||||
export interface ImportMetaEnv {
|
||||
NODE_ENV: string
|
||||
APP_URL: string
|
||||
|
||||
POCKETBASE_URL: string
|
||||
|
||||
GOOGLE_API_KEY: string
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv
|
||||
}
|
||||
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||
declare namespace App {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface Locals {
|
||||
pb: PocketBase
|
||||
}
|
||||
}
|
||||
|
27
front/src/middleware/index.ts
Normal file
27
front/src/middleware/index.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import PocketBase from 'pocketbase'
|
||||
|
||||
import { defineMiddleware } from 'astro/middleware'
|
||||
import { getEnv } from 'libs/Env'
|
||||
|
||||
export const onRequest = defineMiddleware(async ({ locals, request }: any, next: () => any) => {
|
||||
locals.pb = new PocketBase(getEnv('POCKETBASE_URL','http://localhost:8080'))
|
||||
|
||||
// load the store data from the request cookie string
|
||||
locals.pb.authStore.loadFromCookie(request.headers.get('cookie') || '')
|
||||
|
||||
try {
|
||||
// get an up-to-date auth store state by verifying and refreshing the loaded auth model (if any)
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
locals.pb.authStore.isValid && await locals.pb.collection('users').authRefresh()
|
||||
} catch (_) {
|
||||
// clear the auth store on failed refresh
|
||||
locals.pb.authStore.clear()
|
||||
}
|
||||
|
||||
const response = await next()
|
||||
|
||||
// send back the default 'pb_auth' cookie to the client with the latest store state
|
||||
response.headers.append('set-cookie', locals.pb.authStore.exportToCookie())
|
||||
|
||||
return response
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user