Compare commits
8 Commits
2cc3219b30
...
fix-SSR-wi
Author | SHA1 | Date | |
---|---|---|---|
02e84ed9d6 | |||
06bdeff188 | |||
7a510a53a1 | |||
063660db9d | |||
55740b6219 | |||
459c1252e0 | |||
cd234f5b37 | |||
76755baa1d |
9
front/.vscode/settings.json
vendored
Normal file
9
front/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"editor.quickSuggestions": {
|
||||
"strings": "on"
|
||||
},
|
||||
"tailwindCSS.includeLanguages": {
|
||||
"astro": "html"
|
||||
},
|
||||
"typescript.tsdk": "node_modules/typescript/lib"
|
||||
}
|
@ -1,30 +1,29 @@
|
||||
import { defineConfig } from 'astro/config';
|
||||
import node from '@astrojs/node';
|
||||
import tailwind from "@astrojs/tailwind";
|
||||
import node from '@astrojs/node'
|
||||
import tailwind from '@astrojs/tailwind'
|
||||
import { defineConfig } from 'astro/config'
|
||||
|
||||
import react from "@astrojs/react";
|
||||
import react from '@astrojs/react'
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
// integrations: [tailwind(), test, routing(), version(), buildInfos()],
|
||||
compressHTML: true,
|
||||
build: {
|
||||
assets: 'assets',
|
||||
inlineStylesheets: 'auto'
|
||||
},
|
||||
server: {
|
||||
host: true,
|
||||
port: 3000
|
||||
},
|
||||
trailingSlash: 'never',
|
||||
output: 'server',
|
||||
adapter: node({
|
||||
mode: 'standalone'
|
||||
}),
|
||||
integrations: [tailwind(), react()],
|
||||
vite: {
|
||||
optimizeDeps: {
|
||||
include: ['leaflet']
|
||||
}
|
||||
}
|
||||
});
|
||||
compressHTML: true,
|
||||
build: {
|
||||
assets: 'assets',
|
||||
inlineStylesheets: 'auto'
|
||||
},
|
||||
server: {
|
||||
host: true,
|
||||
port: 3000
|
||||
},
|
||||
trailingSlash: 'never',
|
||||
output: 'server',
|
||||
adapter: node({
|
||||
mode: 'standalone'
|
||||
}),
|
||||
integrations: [tailwind(), react()],
|
||||
vite: {
|
||||
optimizeDeps: {
|
||||
include: ['leaflet']
|
||||
}
|
||||
}
|
||||
})
|
||||
|
746
front/package-lock.json
generated
746
front/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -3,7 +3,6 @@
|
||||
"type": "module",
|
||||
"version": "0.0.1",
|
||||
"private": "true",
|
||||
"private": "true",
|
||||
"scripts": {
|
||||
"dev": "astro dev",
|
||||
"start": "node ./dist/server/entry.mjs",
|
||||
@ -28,10 +27,11 @@
|
||||
"react-leaflet": "^4.2.1",
|
||||
"simple-icons-astro": "^11.12.0",
|
||||
"tailwindcss": "^3.4.3",
|
||||
"typescript": "^5.2.2"
|
||||
"typescript": "^5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@astrojs/check": "^0",
|
||||
"@astrojs/ts-plugin": "^1.6.1",
|
||||
"@types/leaflet": "^1.9.12",
|
||||
"@types/node": "^20",
|
||||
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
||||
|
15
front/src/env.d.ts
vendored
15
front/src/env.d.ts
vendored
@ -1,12 +1,14 @@
|
||||
/// <reference path="../.astro/types.d.ts" />
|
||||
/// <reference types="astro/client" />
|
||||
|
||||
|
||||
import PocketBase from 'pocketbase'
|
||||
export interface ImportMetaEnv {
|
||||
NODE_ENV: string
|
||||
APP_URL: string
|
||||
|
||||
POCKETBASEURL: string
|
||||
POCKETBASE_URL: string
|
||||
|
||||
GOOGLE_API_KEY: string
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
@ -14,9 +16,10 @@ interface ImportMeta {
|
||||
}
|
||||
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||
declare namespace App {
|
||||
/**
|
||||
* Middlewares variables
|
||||
*/
|
||||
interface Locals {}
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface Locals {
|
||||
pb: PocketBase
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { AstroCookies } from 'astro'
|
||||
import type { AstroCookies } from 'astro'
|
||||
import type UserObj from 'models/User'
|
||||
import PocketBase from 'pocketbase'
|
||||
import UserObj from 'models/User'
|
||||
import { getEnv } from 'libs/Env'
|
||||
|
||||
|
||||
const pb = new PocketBase(getEnv('POCKET_BASE','https://pb-tweb.cb85.fr')) // XXX: 'https://pb-tweb.cb85.fr'
|
||||
const pb = new PocketBase(getEnv('POCKETBASE_URL','https://pb-tweb.cb85.fr')) // XXX: 'https://pb-tweb.cb85.fr'
|
||||
|
||||
export async function clearUser(cookies: AstroCookies): Promise<void> {
|
||||
const sessionID = cookies.get('session')?.value
|
||||
@ -18,7 +18,7 @@ export async function clearUser(cookies: AstroCookies): Promise<void> {
|
||||
})
|
||||
}
|
||||
|
||||
export async function login(cookies: AstroCookies, user: {user: string, password: string}): boolean {
|
||||
export async function login(cookies: AstroCookies, user: {user: string, password: string}): Promise<boolean> {
|
||||
|
||||
const authData = await pb.collection('users').authWithPassword(user.user, user.password)
|
||||
|
||||
@ -33,7 +33,7 @@ export async function login(cookies: AstroCookies, user: {user: string, password
|
||||
httpOnly: true,
|
||||
path: '/',
|
||||
secure: secure,
|
||||
sameSite: 'Strict',
|
||||
sameSite: 'strict',
|
||||
maxAge: 365000,
|
||||
})
|
||||
return true
|
||||
@ -44,15 +44,40 @@ export async function login(cookies: AstroCookies, user: {user: string, password
|
||||
export async function getUser(cookies: AstroCookies): Promise<UserObj | null> {
|
||||
const sessionID = cookies.get('session')?.value
|
||||
|
||||
const bpAuth = pb.authStore
|
||||
|
||||
if(!sessionID){
|
||||
return
|
||||
return null
|
||||
}
|
||||
if(!pb.authStore.isValid){
|
||||
return
|
||||
if(!bpAuth.isValid){
|
||||
return null
|
||||
}
|
||||
if(!bpAuth){
|
||||
return null
|
||||
}
|
||||
|
||||
console.log(pb.authStore.model)
|
||||
return pb.authStore.model
|
||||
console.log(bpAuth.model)
|
||||
|
||||
if(!bpAuth.model){
|
||||
return null
|
||||
}
|
||||
|
||||
const output: UserObj = {
|
||||
id: bpAuth.model.id as string,
|
||||
collectionId: bpAuth.model.collectionId as string,
|
||||
collectionName: bpAuth.model.collectionName as string,
|
||||
created: bpAuth.model.created as string,
|
||||
updated: bpAuth.model.updated as string,
|
||||
avatar: bpAuth.model.avatar as string,
|
||||
username: bpAuth.model.username as string,
|
||||
email: bpAuth.model.email as string,
|
||||
emailVisibility: false,
|
||||
name: bpAuth.model.name as string,
|
||||
password: undefined,
|
||||
passwordConfirm: undefined,
|
||||
}
|
||||
|
||||
return output
|
||||
|
||||
}
|
||||
|
||||
@ -76,7 +101,7 @@ export async function setUser(cookies: AstroCookies, user: UserObj): Promise<voi
|
||||
httpOnly: true,
|
||||
path: '/',
|
||||
secure: secure,
|
||||
sameSite: 'Strict',
|
||||
sameSite: 'strict',
|
||||
maxAge: 365000,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
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
|
||||
})
|
@ -1,19 +1,19 @@
|
||||
|
||||
|
||||
export interface PBData{
|
||||
id: string | null
|
||||
collectionId: string | null
|
||||
collectionName: string | null
|
||||
created: string | null // TODO: passé ca en date auto
|
||||
updated: string | null // TODO: passé ca en date auto
|
||||
id?: string | null
|
||||
collectionId?: string | null
|
||||
collectionName?: string | null
|
||||
created?: string | null // TODO: passé ca en date auto
|
||||
updated?: string | null // TODO: passé ca en date auto
|
||||
}
|
||||
|
||||
export default interface UserObj extends PBData{
|
||||
avatar: string | null
|
||||
avatar?: string | null
|
||||
username: string
|
||||
email: string
|
||||
emailVisibility: false
|
||||
password: string | null
|
||||
passwordConfirm: string | null
|
||||
emailVisibility?: boolean
|
||||
password?: string | undefined
|
||||
passwordConfirm?: string | undefined
|
||||
name: string | null
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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")
|
||||
|
||||
|
@ -1,32 +1,33 @@
|
||||
---
|
||||
import PocketBase from 'pocketbase';
|
||||
import Layout from '../../layouts/Layout.astro';
|
||||
import AstroUtils from '../../libs/AstroUtils';
|
||||
import { getUser, setUser } from 'libs/AuthUtils';
|
||||
import 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
|
||||
}
|
||||
const form = await Astro.request.formData()
|
||||
const request : UserObj = {
|
||||
const request = {
|
||||
username: form.get("username") as string,
|
||||
name: form.get("name") as string,
|
||||
email: form.get("email") as string,
|
||||
password: form.get("password") as string,
|
||||
passwordConfirm: form.get("passwordConfirm") as string,
|
||||
emailVisibility: false
|
||||
}
|
||||
await setUser(Astro.cookies, request);
|
||||
|
||||
try{
|
||||
await pb.collection('users').create(request)
|
||||
return Astro.redirect('account/login')
|
||||
}catch(e){
|
||||
console.log(e);
|
||||
}
|
||||
})
|
||||
---
|
||||
|
||||
|
@ -5,4 +5,7 @@
|
||||
// hide an issue with typescript
|
||||
"noUnusedLocals": false
|
||||
},
|
||||
"ts-node": {
|
||||
"esm": true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user