Now validating users against the db

This commit is contained in:
Nicolai Ort 2021-08-14 10:15:59 +02:00
parent 6d417fac1b
commit cd9400fec3
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
2 changed files with 19 additions and 3 deletions

View File

@ -10,6 +10,7 @@
"migrate": "knex migrate:latest"
},
"dependencies": {
"argon2": "^0.28.2",
"dotenv": "^10.0.0",
"fastify": "^3.20.1",
"fastify-basic-auth": "^2.1.0",

View File

@ -1,6 +1,7 @@
const fastify = require('fastify')({ logger: true })
var uniqid = require('uniqid');
require('dotenv').config()
require('dotenv').config();
const argon2 = require('argon2');
let config = {
domain: process.env.DOMAIN || "localhost:3000",
@ -244,10 +245,24 @@ function checkKnownProviders(target) {
}
async function validate(username, password, req, reply) {
console.log(username)
if (username !== 'admin' || password !== 'admin') {
if (!username || !password) {
return new Error('Sorry only authorized users can do that.')
}
const user = await knex.select('name', 'password')
.from('users')
.where('name', '=', username)
.limit(1);
if (user.length == 0) {
return new Error('Sorry m8, looks like you are not on the inivtation list');
}
password = await argon2.hash(password);
if (password != user[0].password) {
return new Error('Wrong credentials');
}
}
// Run the server!