diff --git a/front/src/env.d.ts b/front/src/env.d.ts
index 0a69926..d858407 100644
--- a/front/src/env.d.ts
+++ b/front/src/env.d.ts
@@ -1,11 +1,25 @@
+///
+///
+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
+ }
+}
diff --git a/front/src/middleware/index.ts b/front/src/middleware/index.ts
new file mode 100644
index 0000000..89e6322
--- /dev/null
+++ b/front/src/middleware/index.ts
@@ -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
+})
diff --git a/front/src/pages/account/index.astro b/front/src/pages/account/index.astro
index 4b1e795..39a434b 100644
--- a/front/src/pages/account/index.astro
+++ b/front/src/pages/account/index.astro
@@ -1,16 +1,21 @@
---
-import Layout from 'layouts/Layout.astro';
-import { getUser } from 'libs/AuthUtils';
+import Layout from 'layouts/Layout.astro'
+import PocketBase from 'pocketbase'
-const user = await getUser(Astro.cookies);
+const pb = Astro.locals.pb as PocketBase
+const auth = pb.authStore
+const user = auth.model
-if(!user){
+if(!auth.isValid){
return Astro.redirect("/account/login");
}
---
- Bonjour {user.name}
+ Bonjour {user!.name}
+
diff --git a/front/src/pages/account/login.astro b/front/src/pages/account/login.astro
index d8251eb..6f3cd39 100644
--- a/front/src/pages/account/login.astro
+++ b/front/src/pages/account/login.astro
@@ -1,32 +1,34 @@
---
-import Layout from "../../layouts/Layout.astro";
-import PocketBase from 'pocketbase';
-import AstroUtils from "../../libs/AstroUtils";
-import { getUser, login } from "libs/AuthUtils";
+import Layout from "layouts/Layout.astro";
+import AstroUtils from "libs/AstroUtils";
+import PocketBase from 'pocketbase'
+const pb = Astro.locals.pb as PocketBase
-const usr = await getUser(Astro.cookies)
-if (usr) {
- // return Astro.redirect(route('/', {message: 'Vous êtes déjà connecté !'}))
+if(pb.authStore.isValid){
+ return Astro.redirect("/account")
}
-
const res = await AstroUtils.wrap(async () => {
if (Astro.request.method !== 'POST') {
return
}
+ // FIXME checké si utilisateur deja connecté
+ const locals = Astro.locals
+
const form = await Astro.request.formData();
const request = {
user: form.get("username") as string,
password: form.get("password") as string
}
-
- await login(Astro.cookies, request)
-
- return Astro.redirect("/account")
-
+ try {
+ pb.collection('users').authWithPassword(request.user,request.password);
+ return Astro.redirect("/account")
+ } catch (error) {
+ console.log(error)
+ }
})
---
diff --git a/front/src/pages/account/logout.astro b/front/src/pages/account/logout.astro
new file mode 100644
index 0000000..28472f1
--- /dev/null
+++ b/front/src/pages/account/logout.astro
@@ -0,0 +1,13 @@
+---
+import PocketBase from 'pocketbase'
+
+
+const pb = Astro.locals.pb as PocketBase
+
+if(pb.authStore.isValid){
+ pb.authStore.clear()
+}
+
+return Astro.redirect('/account/login')
+
+---
\ No newline at end of file
diff --git a/front/src/pages/account/register.astro b/front/src/pages/account/register.astro
index 3fa79b1..f016d0f 100644
--- a/front/src/pages/account/register.astro
+++ b/front/src/pages/account/register.astro
@@ -1,18 +1,16 @@
---
-import PocketBase from 'pocketbase';
-import Layout from '../../layouts/Layout.astro';
-import AstroUtils from '../../libs/AstroUtils';
-import { getUser, setUser } from 'libs/AuthUtils';
-import type UserObj from 'models/User';
+import Layout from 'layouts/Layout.astro';
+import AstroUtils from 'libs/AstroUtils';
+import PocketBase from 'pocketbase'
-const connected = await getUser(Astro.cookies)
-if(connected) {
- return Astro.redirect(route('/'))
+const pb = Astro.locals.pb as PocketBase
+
+if(pb.authStore.isValid){
+ return Astro.redirect("/account")
}
-
-const res = await AstroUtils.wrap(async () => {
+await AstroUtils.wrap(async () => {
if (Astro.request.method !== 'POST'){
return
}
@@ -24,8 +22,12 @@ const res = await AstroUtils.wrap(async () => {
password: form.get("password") as string,
passwordConfirm: form.get("passwordConfirm") as string,
}
- await setUser(Astro.cookies, request);
-
+ try{
+ await pb.collection('users').create(request)
+ return Astro.redirect('account/login')
+ }catch(e){
+ console.log(e);
+ }
})
---