Skip to content

Commit

Permalink
Saving data in Users Table worked
Browse files Browse the repository at this point in the history
1. Added bcrypt to hash password before storing in the DB.
2. Used express json middleware to parse request body data to json object.
  • Loading branch information
enigmaticmahesh committed Aug 29, 2022
1 parent 7e55b5c commit 9f34b93
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const authRoutes = require("./routes/auth.route");

const app = express();
app.use(cors());
app.use(express.json());

setupDatabase();

Expand Down
27 changes: 22 additions & 5 deletions controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
const sqliteWrapper = require("../db");
const bcrypt = require("bcrypt");
const { sqliteWrapper } = require("../db");

exports.login = (req, res) => {
res.status(200).json({
message: "Logging in User",
});
};

exports.register = (req, res) => {
res.status(200).json({
message: "Register User",
});
exports.register = async (req, res) => {
const { email, password } = req.body;
try {
const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(password, salt);

const createUserQuery = `INSERT INTO users VALUES (NULL, "${email}", "${hashedPassword}")`;
const method = "run";
const user = await sqliteWrapper(createUserQuery, method);
res.status(200).json({
message: "Register User",
user,
});
} catch (error) {
console.log(error);
res.status(500).json({
message: "Error while registering. Please, try again",
error,
});
}
};
2 changes: 1 addition & 1 deletion db.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const createTables = async () => {
await sqliteWrapper(createUsersTableQuery, method);
await sqliteWrapper(createApartmentsTableQuery, method);
} catch (error) {
console.log("Error while creating Users Table: ", error);
console.log("Error while creating Tables: ", error);
}
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dev": "nodemon"
},
"dependencies": {
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"express": "^4.18.1",
"sqlite3": "^5.0.11"
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

bcrypt@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/bcrypt/-/bcrypt-5.0.1.tgz#f1a2c20f208e2ccdceea4433df0c8b2c54ecdf71"
integrity sha512-9BTgmrhZM2t1bNuDtrtIMVSmmxZBrJ71n8Wg+YgdjHuIWYF7SjjmCPZFB+/5i/o/PIeRpwVJR3P+NrpIItUjqw==
dependencies:
"@mapbox/node-pre-gyp" "^1.0.0"
node-addon-api "^3.1.0"

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
Expand Down Expand Up @@ -820,6 +828,11 @@ negotiator@0.6.3, negotiator@^0.6.2:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

node-addon-api@^3.1.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==

node-addon-api@^4.2.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-4.3.0.tgz#52a1a0b475193e0928e98e0426a0d1254782b77f"
Expand Down

0 comments on commit 9f34b93

Please sign in to comment.