Skip to content

Commit

Permalink
bringing inlogin/registration from notes and build weeks. going to im…
Browse files Browse the repository at this point in the history
…plement and get working then bring passport and see whats different
  • Loading branch information
Carlos committed Mar 10, 2020
1 parent 5942ad1 commit 5b20e89
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
14 changes: 14 additions & 0 deletions middleware/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//validateMiddlware for login/register
function validateMiddlware(req, res, next) {
const { username, password } = req.body;

if (!username || !password) {
res.satus(401).json({ error: "Invalid credentials for login" });
} else {
next();
}
}

module.exports = {
validateMiddlware
};
File renamed without changes.
42 changes: 42 additions & 0 deletions routers/auth-router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const express = require("express");
const bcrypt = require("bcryptjs");
const usersModel = require("../modules/user-model")
// const { validateMiddleware } = require("")
// const authenticationModel = require("")
// const generateToken = require("")

const router = express.Router();

// CREATE NEW USER POST
router.post("/register", async (req, res, next) => {
try {
const user = usersModel.add(req.body);
rerturn res.status(201).json({ message: "welcome new created user0", user });
} catch (err) {
console.log(err)
next(err)
}
})
// LOGIN POST
router.post("/login", validateMiddleware, async (req, res, next) => {
try {
let { username, password } = req.body;
const user = await authenticationModel.userAccount(email);
const passwordValid = await bcrypt.compare(password, user.password);
// if user and password are good then you get token
console.log(passwordValid, user)
if (user && password) {
const token = generateToken(user)
res.status(200).json({ message: `Bienvenidos ${user.username}!`, token });
} else {
res.status(401).json({
error: "Invalid Credentials"
});
}
} catch (err) {
console.log(err)
next(err)
}
})

module.exports = server;
42 changes: 42 additions & 0 deletions routers/users-router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const express = require("express");
// userModel import

const router = express.Router();

//GET USER
router.get("/", async (req, res, next) => {
try {
const users = await userModel.find();
res.status(200).json(users);
} catch (err) {
next(err);
}
});
//GET USER ID
router.get("/:id", async (req, res, next) => {
try {
const userId = await userModel
.findById(req.params.id)
.where("id", id)
.first();
return res.status(201).json(newUser);
} catch (err) {
next(err);
}
});
//ADD NEW USER
router.post("/", async (req, res, next) => {
try {
const [id] = await db("blogdb").insert(req.body);
const newUser = await db("blogdb")
.where("id", id)
.first();
return res.status(201).json(newUser);
} catch (err) {
next(err);
}
console.log(newUser);
});
// edit user
//delete user
module.exports = router;

0 comments on commit 5b20e89

Please sign in to comment.