-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bringing inlogin/registration from notes and build weeks. going to im…
…plement and get working then bring passport and see whats different
- Loading branch information
Carlos
committed
Mar 10, 2020
1 parent
5942ad1
commit 5b20e89
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |