Compare commits
No commits in common. "9116a1544e0a210eb68e15fbe4f3cf8b0583653d" and "7a510a53a17a3024fe98383cfd7e2146f1f4f8e1" have entirely different histories.
9116a1544e
...
7a510a53a1
14
front/src/env.d.ts
vendored
14
front/src/env.d.ts
vendored
@ -1,25 +1,11 @@
|
|||||||
/// <reference path="../.astro/types.d.ts" />
|
|
||||||
/// <reference types="astro/client" />
|
|
||||||
|
|
||||||
import PocketBase from 'pocketbase'
|
|
||||||
export interface ImportMetaEnv {
|
export interface ImportMetaEnv {
|
||||||
NODE_ENV: string
|
NODE_ENV: string
|
||||||
APP_URL: string
|
APP_URL: string
|
||||||
|
|
||||||
POCKETBASE_URL: string
|
POCKETBASE_URL: string
|
||||||
|
|
||||||
GOOGLE_API_KEY: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ImportMeta {
|
interface ImportMeta {
|
||||||
readonly env: ImportMetaEnv
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
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
|
|
||||||
})
|
|
@ -1,21 +1,16 @@
|
|||||||
---
|
---
|
||||||
import Layout from 'layouts/Layout.astro'
|
import Layout from 'layouts/Layout.astro';
|
||||||
import PocketBase from 'pocketbase'
|
import { getUser } from 'libs/AuthUtils';
|
||||||
|
|
||||||
|
|
||||||
const pb = Astro.locals.pb as PocketBase
|
const user = await getUser(Astro.cookies);
|
||||||
const auth = pb.authStore
|
|
||||||
const user = auth.model
|
|
||||||
|
|
||||||
if(!auth.isValid){
|
if(!user){
|
||||||
return Astro.redirect("/account/login");
|
return Astro.redirect("/account/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title="Account setting">
|
<Layout title="Account setting">
|
||||||
<h1>Bonjour {user!.name}</h1>
|
<h1>Bonjour {user.name}</h1>
|
||||||
<div>
|
|
||||||
<a href="/account/logout">deconnexion</a>
|
|
||||||
</div>
|
|
||||||
</Layout>
|
</Layout>
|
||||||
|
@ -1,34 +1,32 @@
|
|||||||
---
|
---
|
||||||
import Layout from "layouts/Layout.astro";
|
import Layout from "../../layouts/Layout.astro";
|
||||||
import AstroUtils from "libs/AstroUtils";
|
import PocketBase from 'pocketbase';
|
||||||
import PocketBase from 'pocketbase'
|
import AstroUtils from "../../libs/AstroUtils";
|
||||||
|
import { getUser, login } from "libs/AuthUtils";
|
||||||
|
|
||||||
|
|
||||||
const pb = Astro.locals.pb as PocketBase
|
|
||||||
|
|
||||||
if(pb.authStore.isValid){
|
const usr = await getUser(Astro.cookies)
|
||||||
return Astro.redirect("/account")
|
if (usr) {
|
||||||
|
// return Astro.redirect(route('/', {message: 'Vous êtes déjà connecté !'}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const res = await AstroUtils.wrap(async () => {
|
const res = await AstroUtils.wrap(async () => {
|
||||||
if (Astro.request.method !== 'POST') {
|
if (Astro.request.method !== 'POST') {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// FIXME checké si utilisateur deja connecté
|
|
||||||
const locals = Astro.locals
|
|
||||||
|
|
||||||
const form = await Astro.request.formData();
|
const form = await Astro.request.formData();
|
||||||
const request = {
|
const request = {
|
||||||
user: form.get("username") as string,
|
user: form.get("username") as string,
|
||||||
password: form.get("password") as string
|
password: form.get("password") as string
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
pb.collection('users').authWithPassword(request.user,request.password);
|
await login(Astro.cookies, request)
|
||||||
return Astro.redirect("/account")
|
|
||||||
} catch (error) {
|
return Astro.redirect("/account")
|
||||||
console.log(error)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
---
|
|
||||||
import PocketBase from 'pocketbase'
|
|
||||||
|
|
||||||
|
|
||||||
const pb = Astro.locals.pb as PocketBase
|
|
||||||
|
|
||||||
if(pb.authStore.isValid){
|
|
||||||
pb.authStore.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
return Astro.redirect('/account/login')
|
|
||||||
|
|
||||||
---
|
|
@ -1,16 +1,18 @@
|
|||||||
---
|
---
|
||||||
import Layout from 'layouts/Layout.astro';
|
import PocketBase from 'pocketbase';
|
||||||
import AstroUtils from 'libs/AstroUtils';
|
import Layout from '../../layouts/Layout.astro';
|
||||||
import PocketBase from 'pocketbase'
|
import AstroUtils from '../../libs/AstroUtils';
|
||||||
|
import { getUser, setUser } from 'libs/AuthUtils';
|
||||||
|
import type UserObj from 'models/User';
|
||||||
|
|
||||||
|
const connected = await getUser(Astro.cookies)
|
||||||
|
|
||||||
const pb = Astro.locals.pb as PocketBase
|
if(connected) {
|
||||||
|
return Astro.redirect(route('/'))
|
||||||
if(pb.authStore.isValid){
|
|
||||||
return Astro.redirect("/account")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await AstroUtils.wrap(async () => {
|
|
||||||
|
const res = await AstroUtils.wrap(async () => {
|
||||||
if (Astro.request.method !== 'POST'){
|
if (Astro.request.method !== 'POST'){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -22,12 +24,8 @@ await AstroUtils.wrap(async () => {
|
|||||||
password: form.get("password") as string,
|
password: form.get("password") as string,
|
||||||
passwordConfirm: form.get("passwordConfirm") as string,
|
passwordConfirm: form.get("passwordConfirm") as string,
|
||||||
}
|
}
|
||||||
try{
|
await setUser(Astro.cookies, request);
|
||||||
await pb.collection('users').create(request)
|
|
||||||
return Astro.redirect('account/login')
|
|
||||||
}catch(e){
|
|
||||||
console.log(e);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user