Compare commits
No commits in common. "41ed28532682d75380ea6392fc9ce5c1b97d3404" and "9116a1544e0a210eb68e15fbe4f3cf8b0583653d" have entirely different histories.
41ed285326
...
9116a1544e
107
front/src/libs/AuthUtils.ts
Normal file
107
front/src/libs/AuthUtils.ts
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
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,
|
||||||
|
})
|
||||||
|
}
|
19
front/src/models/User/index.ts
Normal file
19
front/src/models/User/index.ts
Normal file
@ -0,0 +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
|
||||||
|
}
|
||||||
|
|
||||||
|
export default interface UserObj extends PBData{
|
||||||
|
avatar?: string | null
|
||||||
|
username: string
|
||||||
|
email: string
|
||||||
|
emailVisibility?: boolean
|
||||||
|
password?: string | undefined
|
||||||
|
passwordConfirm?: string | undefined
|
||||||
|
name: string | null
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user