feat: gestion-utilisateur #14

Closed
Clement wants to merge 38 commits from feat/gestion-utilisateur into master
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 { 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");
}
---
<Layout title="Account setting">
<h1>Bonjour {user.name}</h1>
<h1>Bonjour {user!.name}</h1>
</Layout>

View File

@ -1,29 +1,33 @@
---
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)
try {
await locals.pb.collection('users').authWithPassword(request.user,request.password);
} catch (error) {
console.log(error)
}
return Astro.redirect("/account")

View File

@ -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);
}
})
---