import { Pool } from 'pg';
import { hash } from 'bcrypt'

export default defineEventHandler(async (event) => {
    try {
        const pool = new Pool({
            user: "de-user10",
            host: 'de-db.ptk.itiscaf.ru',
            database: 'de-user10',
            port: 5441,
            password: 'fGz0uG18uK'
        })

        const body = await readBody(event);

        let { login, password} = body;

        const hash_password = hash(password, 10);

        const res = await pool.query('SELECT * FROM users WHERE login = $1 RETURNING id', 
            [login]
        );
        if(res.rows[0]){
            const user = res.rows[0]

            setCookie(event, 'online', JSON.stringify({user}))
        }
    } catch (error) {
        console.log('server signUp error', error);
        
    }


})