Compare commits
4 Commits
fix-SSR-wi
...
41ed285326
Author | SHA1 | Date | |
---|---|---|---|
41ed285326 | |||
2b11a223cd | |||
9116a1544e | |||
5f642a6aa0 |
@ -1,107 +0,0 @@
|
||||
import type { AstroCookies } from 'astro'
|
||||
import type UserObj from 'models/User'
|
||||
import PocketBase from 'pocketbase'
|
||||
import { getEnv } from 'libs/Env'
|
||||
|
||||
|
||||
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
|
||||
|
||||
if(!sessionID){
|
||||
return
|
||||
}
|
||||
|
||||
cookies.delete('session',{
|
||||
path: '/'
|
||||
})
|
||||
}
|
||||
|
||||
export async function login(cookies: AstroCookies, user: {user: string, password: string}): Promise<boolean> {
|
||||
|
||||
const authData = await pb.collection('users').authWithPassword(user.user, user.password)
|
||||
|
||||
let secure = true
|
||||
|
||||
if (getEnv('NODE_ENV', 'production') !== 'production') {
|
||||
secure = false
|
||||
}
|
||||
|
||||
if(authData){
|
||||
cookies.set('session', authData.token,{
|
||||
httpOnly: true,
|
||||
path: '/',
|
||||
secure: secure,
|
||||
sameSite: 'strict',
|
||||
maxAge: 365000,
|
||||
})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export async function getUser(cookies: AstroCookies): Promise<UserObj | null> {
|
||||
const sessionID = cookies.get('session')?.value
|
||||
|
||||
const bpAuth = pb.authStore
|
||||
|
||||
if(!sessionID){
|
||||
return null
|
||||
}
|
||||
if(!bpAuth.isValid){
|
||||
return null
|
||||
}
|
||||
if(!bpAuth){
|
||||
return null
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
export async function setUser(cookies: AstroCookies, user: UserObj): Promise<void>{
|
||||
|
||||
const record = await pb.collection('users').create(user)
|
||||
|
||||
console.log(record)
|
||||
|
||||
const session = pb.authStore.token
|
||||
|
||||
console.log(session)
|
||||
|
||||
let secure = true
|
||||
|
||||
if (getEnv('NODE_ENV', 'production') !== 'production') {
|
||||
secure = false
|
||||
}
|
||||
|
||||
cookies.set('session', session,{
|
||||
httpOnly: true,
|
||||
path: '/',
|
||||
secure: secure,
|
||||
sameSite: 'strict',
|
||||
maxAge: 365000,
|
||||
})
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
export default interface UserObj extends PBData{
|
||||
avatar?: string | null
|
||||
username: string
|
||||
email: string
|
||||
emailVisibility?: boolean
|
||||
password?: string | undefined
|
||||
passwordConfirm?: string | undefined
|
||||
name: string | null
|
||||
}
|
@ -15,4 +15,7 @@ if(!auth.isValid){
|
||||
|
||||
<Layout title="Account setting">
|
||||
<h1>Bonjour {user!.name}</h1>
|
||||
<div>
|
||||
<a href="/account/logout">deconnexion</a>
|
||||
</div>
|
||||
</Layout>
|
||||
|
@ -24,13 +24,11 @@ const res = await AstroUtils.wrap(async () => {
|
||||
}
|
||||
|
||||
try {
|
||||
await locals.pb.collection('users').authWithPassword(request.user,request.password);
|
||||
pb.collection('users').authWithPassword(request.user,request.password);
|
||||
return Astro.redirect("/account")
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
return Astro.redirect("/account")
|
||||
|
||||
})
|
||||
---
|
||||
|
||||
|
13
front/src/pages/account/logout.astro
Normal file
13
front/src/pages/account/logout.astro
Normal file
@ -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')
|
||||
|
||||
---
|
Reference in New Issue
Block a user