Skip to content
Permalink
Browse files
Basic authentication with bcrypt
  • Loading branch information
ryklovae committed Feb 2, 2023
1 parent 8949e65 commit 945f7ba3525b4864c241ec4004378bb958b768e3
Show file tree
Hide file tree
Showing 10 changed files with 1,186 additions and 13 deletions.
20 db.js
@@ -0,0 +1,20 @@
// database ORM
const Sequelize = require("sequelize");

const sequelize = new Sequelize(
'mysql',
'root',
'codio',
{
host: 'localhost',
dialect: 'mysql'
}
);

sequelize.authenticate().then(() => {
console.log('Connection has been established successfully.');
}).catch((error) => {
console.error('Unable to connect to the database: ', error);
});

module.exports = sequelize;
@@ -1,3 +1,5 @@
// Articles

const fetchPromise = fetch('https://kiwipanel-gravitycrater-3000.codio-box.uk/api/v1/articles/2', {
method: 'PUT',
headers: {
@@ -7,8 +9,6 @@ const fetchPromise = fetch('https://kiwipanel-gravitycrater-3000.codio-box.uk/ap
});
fetchPromise.then(res => res.json()).then(res => console.log(res))



const fetchPromise = fetch('https://kiwipanel-gravitycrater-3000.codio-box.uk/api/v1/articles', {
method: 'GET'
});
@@ -18,4 +18,20 @@ fetchPromise.then(res => res.json()).then(res => console.log(res))
const fetchPromise = fetch('https://kiwipanel-gravitycrater-3000.codio-box.uk/api/v1/articles/1', {
method: 'DELETE'
});
fetchPromise.then(res => res.json()).then(res => console.log(res))
fetchPromise.then(res => res.json()).then(res => console.log(res))

// Users

const fetchPromise = fetch('https://kiwipanel-gravitycrater-3000.codio-box.uk/api/v1/users', {
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({username: "user", email: 'user@name.com', password: 'asd'})
});
fetchPromise.then(res => res.json()).then(res => console.log(res))

const fetchPromise = fetch('https://kiwipanel-gravitycrater-3000.codio-box.uk/api/v1/users/1', {
method: 'DELETE'
});
fetchPromise.then(res => res.json()).then(res => console.log(res))
@@ -4,9 +4,13 @@ const app = new Koa();

const special = require('./routes/special.js')
const articles = require('./routes/articles.js');
const users = require('./routes/users.js');

const sequelize = require('./db');

app.use(special.routes());
app.use(articles.routes());
app.use(users.routes());

let port = process.env.PORT || 3000;

@@ -0,0 +1,54 @@

const sequelize = require('./db');
const { Sequelize, DataTypes } = require("sequelize");

const Article = sequelize.define("articles", {
ID: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
title: {
type: DataTypes.STRING,
allowNull: false
},
allText: {
type: DataTypes.TEXT,
allowNull: false
},
summary: {
type: DataTypes.TEXT,
},
dateCreated: {
type: DataTypes.DATE,
},
dateModified: {
type: DataTypes.DATE,
},
imageURL: {
type: DataTypes.TEXT,
},
published: {
type: DataTypes.TINYINT,
},
authorID: {
type: DataTypes.INTEGER,
}
}, {
timestamps: false
});

sequelize.sync().then(() => {
console.log('Article table created successfully!');
}).catch((error) => {
console.error('Unable to create table : ', error);
});

Article.bulkCreate([
{ title: "Title 1", allText: "Text 1" },
{ title: "Title 2", allText: "Text 2" },
{ title: "Title 3", allText: "Text 3" },
{ title: "Title 4", allText: "Text 4" },
{ title: "Title 5", allText: "Text 5" },
]);
@@ -1,11 +1,65 @@
const db = require('../helpers/database');
const articleViews = require('./articleViews');

const { Sequelize, DataTypes } = require("sequelize");

const sequelize = require('../db');

// https://www.digitalocean.com/community/tutorials/how-to-use-sequelize-with-node-js-and-mysql
// https://medium.com/@ahmedcharef/sequelize-orm-with-node-js-koa-mysql-foreign-keys-a3097ab91a8
const Article = sequelize.define("articles", {
ID: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
title: {
type: DataTypes.STRING,
allowNull: false
},
allText: {
type: DataTypes.TEXT,
allowNull: false
},
summary: {
type: DataTypes.TEXT,
},
dateCreated: {
type: DataTypes.DATE,
},
dateModified: {
type: DataTypes.DATE,
},
imageURL: {
type: DataTypes.TEXT,
},
published: {
type: DataTypes.TINYINT,
},
authorID: {
type: DataTypes.INTEGER,
}
}, {
timestamps: false
});

sequelize.sync().then(() => {
console.log('Article table created successfully!');
}).catch((error) => {
console.error('Unable to create table : ', error);
});

//get a single article by its id
exports.getById = async function getById (id) {
let query = "SELECT * FROM articles WHERE ID = ?";
/*let query = "SELECT * FROM articles WHERE ID = ?";
let values = [id];
let data = await db.run_query(query, values);
let data = await db.run_query(query, values);*/
let data = Article.findAll({
where: {
ID: id
}
});

// update views
await articleViews.updateViews(id);
@@ -16,8 +70,12 @@ exports.getById = async function getById (id) {
//list all the articles in the database
exports.getAll = async function getAll (page, limit, order) {
// TODO: use page, limit, order to give pagination
let query = "SELECT * FROM articles;";
/*let query = "SELECT * FROM articles;";
let data = await db.run_query(query);
return data;*/

let data = Article.findAll()

return data;
}

@@ -30,16 +88,27 @@ exports.add = async function add (article) {

//update an article in the database
exports.update = async function update (id, article) {
let query = "UPDATE articles SET ? WHERE ID = ?";
/*let query = "UPDATE articles SET ? WHERE ID = ?";
let data = await db.run_query(query, [article, id]);*/

let data = await Article.update(article, {
where: {
ID: id
}
});

let data = await db.run_query(query, [article, id]);
return article;
return data;
}

//delete an article from the database
exports.deleteArticle = async function deleteArticle (id) {
let query = "DELETE FROM articles WHERE ID = ?;";
/*let query = "DELETE FROM articles WHERE ID = ?;";
let data = await db.run_query(query, id);*/

let data = await db.run_query(query, id);
let data = await Article.destroy({
where: {
ID: id
}
});
return data;
}
@@ -1,8 +1,110 @@
const db = require('../helpers/database');
const bcrypt = require('bcrypt');
const saltRounds = 10;

const { Sequelize, DataTypes } = require("sequelize");

const sequelize = require('../db');
const User = sequelize.define("users", {
ID: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
firstName: {
type: DataTypes.STRING,
},
lastName: {
type: DataTypes.STRING,
},
username: {
type: DataTypes.STRING,
},
about: {
type: DataTypes.TEXT,
},
dateRegistered: {
type: DataTypes.DATE,
},
password: {
type: DataTypes.STRING(255),
},
passwordSalt: {
type: DataTypes.STRING,
},
email: {
type: DataTypes.STRING,
allowNull: false
},
avatarURL: {
type: DataTypes.STRING,
}
}, {
timestamps: false
});

sequelize.sync().then(() => {
console.log('User table created successfully!');
}).catch((error) => {
console.error('Unable to create table : ', error);
});

//get a single user by the (unique) username
exports.findByUsername = async function findByUsername(username) {
const query = "SELECT * FROM users WHERE username = ?;";
const user = await db.run_query(query, username);
return user;
}

//get all users
exports.getAll = async function getAll() {
let data = User.findAll()
return data;
}

//create a user
exports.add = async function add(user) {
const hash = bcrypt.hashSync(user.password, saltRounds);
user.password = hash;
console.log(user);
try {

const data = await User.create(user);
return data;
}
catch (err) {
console.log(err);
return err;
}
}

exports.updatePassword = async function updatePassword() {

const hash = bcrypt.hashSync("asd", saltRounds);

let data = await User.update({ password: hash }, {
where: {
ID: [1, 2, 3, 4, 6, 7, 8, 9]
}
});
return data;

}

//delete a user - not working - foreign key constraint
exports.deleteUser = async function deleteUser(id) {
try {

let data = await User.destroy({
where: {
ID: id
}});
return data;
}
catch (err) {
console.log(err);
return err;
}

}

0 comments on commit 945f7ba

Please sign in to comment.