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