import getPool from "@cocalc/database/pool" ; import { isValidUUID } from "@cocalc/util/misc" ; import passwordHash , { verifyPassword , } from "@cocalc/backend/auth/password-hash" ; export default async function setPassword ( account_id , current_password , new_password ) { if ( ! isValidUUID ( account_id ) ) { throw Error ( "account_id is not valid" ) ; } const pool = getPool ( ) ; const { rows } = await pool . query ( "SELECT password_hash FROM accounts WHERE account_id=$1" , [ account_id ] ) ; if ( rows . length == 0 ) { throw Error ( "No such account" ) ; } const { password_hash } = rows [ 0 ] ; if ( password_hash ) { if ( ! verifyPassword ( current_password , password_hash ) ) { throw Error ( "Current password is incorrect." ) ; } } await pool . query ( "UPDATE accounts SET password_hash=$1 WHERE account_id=$2" , [ passwordHash ( new_password ) , account_id , ] ) ; }