import basePath from "@cocalc/backend/base-path" ; import { v4 } from "uuid" ; import passwordHash from "@cocalc/backend/auth/password-hash" ; import getPool from "@cocalc/database/pool" ; import { expireTime } from "@cocalc/database/pool/util" ; export const COOKIE_NAME = `${ basePath . length <= 1 ? "" : encodeURIComponent ( basePath ) }remember_me` ; export async function createRememberMeCookie ( account_id ) { const session_id = v4 ( ) ; const hash_session_id = passwordHash ( session_id ) ; const x = hash_session_id . split ( "$" ) ; const value = [ x [ 0 ] , x [ 1 ] , x [ 2 ] , session_id ] . join ( "$" ) ; const ttl_s = 24 * 3600 * 30 ; const pool = getPool ( ) ; await pool . query ( "INSERT INTO remember_me (hash, expire, account_id) VALUES($1::TEXT, $2::TIMESTAMP, $3::UUID)" , [ hash_session_id . slice ( 0 , 127 ) , expireTime ( ttl_s ) , account_id ] ) ; return { value , ttl_s } ; } export async function deleteRememberMe ( hash ) { const pool = getPool ( ) ; await pool . query ( "DELETE FROM remember_me WHERE hash=$1::TEXT" , [ hash . slice ( 0 , 127 ) , ] ) ; } export async function deleteAllRememberMe ( account_id ) { const pool = getPool ( ) ; await pool . query ( "DELETE FROM remember_me WHERE account_id=$1::UUID" , [ account_id , ] ) ; }