Basic auth
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import postgres from 'postgres';
|
||||
import * as dotenv from 'dotenv';
|
||||
|
||||
dotenv.config({ path: '.env' });
|
||||
|
||||
const sql = postgres({
|
||||
host: process.env.POSTGRES_HOST,
|
||||
port: parseInt(process.env.POSTGRES_PORT!),
|
||||
database: process.env.POSTGRES_DB,
|
||||
username: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD
|
||||
});
|
||||
|
||||
export default sql;
|
||||
@@ -0,0 +1,28 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
import sql from '$lib/db/db.server';
|
||||
|
||||
export async function createUser(username: string, password: string): Promise<void> {
|
||||
const password_hash: string = await bcrypt.hash(password, 12);
|
||||
|
||||
const response = await sql`
|
||||
INSERT INTO users (username, password_hash, perms)
|
||||
VALUES (${username}, ${password_hash}, 2);
|
||||
`;
|
||||
console.log(response);
|
||||
}
|
||||
|
||||
export async function checkUserCreds(username: string, password: string): Promise<number> {
|
||||
const [user] = await sql`
|
||||
SELECT password_hash, perms
|
||||
FROM users
|
||||
WHERE username = ${username}
|
||||
`;
|
||||
|
||||
if (!user) {
|
||||
return -1;
|
||||
}
|
||||
if (await bcrypt.compare(password, user.password_hash)) {
|
||||
return user['perms'];
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
export async function createUser(username: string, password: string): Promise<void> {
|
||||
const sql = `INSERT INTO users (username, password, perms)
|
||||
VALUES ($username, $password, 0)`;
|
||||
|
||||
const hash = await bcrypt.hash(password, 12);
|
||||
}
|
||||
Reference in New Issue
Block a user