Compare commits

...

2 Commits

Author SHA1 Message Date
41ed285326 rm user model
Some checks failed
Build Docker Image / run (pull_request) Failing after 29s
2024-04-26 16:34:36 +02:00
2b11a223cd rm auth util 2024-04-26 16:33:05 +02:00
2 changed files with 0 additions and 126 deletions

View File

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

View File

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