feat: gestion-utilisateur #1

Merged
Clement merged 37 commits from feat/gestion-utilisateur into master 2024-05-20 10:48:35 +00:00
3 changed files with 35 additions and 27 deletions
Showing only changes of commit 02e84ed9d6 - Show all commits

View File

@ -1,16 +1,18 @@
--- ---
import Layout from 'layouts/Layout.astro'; import Layout from 'layouts/Layout.astro'
import { getUser } from 'libs/AuthUtils'; 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"); return Astro.redirect("/account/login");
} }
--- ---
<Layout title="Account setting"> <Layout title="Account setting">
<h1>Bonjour {user.name}</h1> <h1>Bonjour {user!.name}</h1>
</Layout> </Layout>

View File

@ -1,29 +1,33 @@
--- ---
import Layout from "../../layouts/Layout.astro"; import Layout from "layouts/Layout.astro";
import PocketBase from 'pocketbase'; import AstroUtils from "libs/AstroUtils";
import AstroUtils from "../../libs/AstroUtils"; import PocketBase from 'pocketbase'
import { getUser, login } from "libs/AuthUtils";
const pb = Astro.locals.pb as PocketBase
const usr = await getUser(Astro.cookies) if(pb.authStore.isValid){
if (usr) { return Astro.redirect("/account")
// 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 {
await login(Astro.cookies, request) await locals.pb.collection('users').authWithPassword(request.user,request.password);
} catch (error) {
console.log(error)
}
return Astro.redirect("/account") return Astro.redirect("/account")

View File

@ -1,18 +1,16 @@
--- ---
import PocketBase from 'pocketbase'; import Layout from 'layouts/Layout.astro';
import Layout from '../../layouts/Layout.astro'; import AstroUtils from 'libs/AstroUtils';
import AstroUtils from '../../libs/AstroUtils'; import PocketBase from 'pocketbase'
import { getUser, setUser } from 'libs/AuthUtils';
import type UserObj from 'models/User';
const connected = await getUser(Astro.cookies)
if(connected) { const pb = Astro.locals.pb as PocketBase
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
} }
@ -24,8 +22,12 @@ const res = 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,
} }
await setUser(Astro.cookies, request); try{
await pb.collection('users').create(request)
return Astro.redirect('account/login')
}catch(e){
console.log(e);
}
}) })
--- ---