Skip to content

Samuel imafidon #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions auth/authHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

const Users = require("./auth.model");
const bcrypt = require("bcryptjs");
const jwt = require('jsonwebtoken');

const register = async (req, res,next) => {
let user = req.body;
const hash = bcrypt.hashSync(user.password, 10);
user.password = hash;

try {
const saved = await Users.create(user);
res.status(201).json(saved);
} catch(e) {
next({status:500, message:"Couldnt register user, "+e.message})
}
};

const login = async (req, res,next) => {
let { username, password } = req.body;
try {
const user = await Users.findBy({ username }).first()

if (user && bcrypt.compareSync(password, user.password)) {
const {id} = user;
const token = getToken({username})
res.status(200).json({
id,
token,
});
}
else res.status(401).json({ message: "Invalid Credentials" });

} catch(e) {
next({status:500,message:"Login failed, "+e.message})
}


};
function getToken(username) {
const payload = {
username
}
const secret = process.env.JWT_SECRET
const options = {
expiresIn: '1d'
}
return jwt.sign(payload, secret, options)
};
module.exports = {
login,
register
}
11 changes: 11 additions & 0 deletions auth/authRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');

const {register, login} = require("./authHelpers")

const router = express.Router();


router.post("/register",register)
router.post("/login",login)

module.exports = router;
Binary file modified data/database_file.db3
Binary file not shown.
16 changes: 4 additions & 12 deletions data/db-config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
const knex = require('knex');

const configOptions = require('../knexfile').development;

//const configOptions = require('../knexfile').development;

// const configOptions = process.env.NODE_ENV === "production" ? require('../knexfile').production : require('../knexfile').development;
require('dotenv').config()

// const configOptions = process.env.NODE_ENV === "" || "development" ? require('../knexfile').development : require('../knexfile').production;

module.exports = knex(configOptions);

//done
const knex = require('knex');
const configOptions = require('../knexfile');
module.exports = knex(configOptions[process.env.DB_ENV]);
8 changes: 4 additions & 4 deletions data/models/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ function getStoriesByID(id){
function addStories(stories) {
return db('stories')
.insert(stories)
.then(([id]) => this.get(id));
.then(([id]) => getStoriesByID(id));
}

function updateStories(id, changes) {
return db('stories')
.where('id', id)
.update(changes)
.then(count => (count > 0 ? this.get(id) : null));
.then(count => (count > 0 ? getStoriesByID(id) : null));
}

function removeStories(id) {
Expand All @@ -55,14 +55,14 @@ function getStoriesByIDAuth(id){
function addStoriesAuth(stories) {
return db('auth-stories')
.insert(stories)
.then(([id]) => this.get(id));
.then(([id]) => getStoriesByID(id));
}

function updateStoriesAuth(id, changes) {
return db('auth-stories')
.where('id', id)
.update(changes)
.then(count => (count > 0 ? this.get(id) : null));
.then(count => (count > 0 ? getStoriesByID(id) : null));
}

function removeStoriesAuth(id) {
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('dotenv').config()

const server = require('./server.js');

const PORT = process.env.PORT || 5200;
const PORT = process.env.PORT;

server.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
Expand Down
Loading