Skip to content
Open

done #59

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
96 changes: 48 additions & 48 deletions controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1const axios = require('axios')
const axios = require('axios')
const mysql = require('mysql')
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
Expand All @@ -14,7 +14,7 @@ const saltRounds = 10

// bcrypt.hash(password, saltRounds, function(err, hash) {
// sql = mysql.format(sql, [ username, hash ])

// pool.query(sql, (err, result) => {
// if (err) {
// if (err.code === 'ER_DUP_ENTRY') return res.status(409).send('Username is taken')
Expand All @@ -26,58 +26,58 @@ const saltRounds = 10
// }

const login = (req, res) => {
const { username, password } = req.body
const { username, password } = req.body

axios(`https://${process.env.AUTH0_DOMAIN}/oauth/token`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
data: {
grant_type: 'password',
username: username,
password: password,
audience: process.env.AUTH0_IDENTITY,
connection: 'Username-Password-Authentication',
client_id: process.env.AUTH0_CLIENT_ID,
client_secret: process.env.AUTH0_CLIENT_SECRET
}
})
.then(response => {
const { access_token } = response.data
res.json({
access_token
})
})
.catch(e => {
res.send(e)
})
axios(`https://${process.env.AUTH0_DOMAIN}/oauth/token`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
data: {
grant_type: 'password',
username: username,
password: password,
audience: process.env.AUTH0_IDENTITY,
connection: 'Username-Password-Authentication',
client_id: process.env.AUTH0_CLIENT_ID,
client_secret: process.env.AUTH0_CLIENT_SECRET
}
})
.then(response => {
const { access_token } = response.data
res.json({
access_token
})
})
.catch(e => {
res.send(e)
})

// let sql = "SELECT * FROM usersCredentials WHERE username = ?"
// sql = mysql.format(sql, [ username ])
// let sql = "SELECT * FROM usersCredentials WHERE username = ?"
// sql = mysql.format(sql, [ username ])

// pool.query(sql, (err, rows) => {
// if (err) return handleSQLError(res, err)
// if (!rows.length) return res.status(404).send('No matching users')
// pool.query(sql, (err, rows) => {
// if (err) return handleSQLError(res, err)
// if (!rows.length) return res.status(404).send('No matching users')

// const hash = rows[0].password
// bcrypt.compare(password, hash)
// .then(result => {
// if (!result) return res.status(400).send('Invalid password')
// const hash = rows[0].password
// bcrypt.compare(password, hash)
// .then(result => {
// if (!result) return res.status(400).send('Invalid password')

// const data = { ...rows[0] }
// data.password = 'REDACTED'
// const data = { ...rows[0] }
// data.password = 'REDACTED'

// const token = jwt.sign(data, 'secret')
// res.json({
// msg: 'Login successful',
// token
// })
// })
// })
// const token = jwt.sign(data, 'secret')
// res.json({
// msg: 'Login successful',
// token
// })
// })
// })
}

module.exports = {
// signup,
login
}
// signup,
login
}
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ app.use('/users', usersRouter)
app.use('/auth', authRouter)

app.get('/', (req, res) => {
res.send('Welcome to our server!')
res.send('Welcome to our server!')
})

app.listen(port, () => {
console.log(`Web server is listening on port ${port}!`);
});
console.log(`Web server is listening on port ${port}!`);
});
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "311_wk2_day2_express-continued",
"version": "1.0.0",
"description": "",
"description": "PLEASE NOTE THAT PRIOR TO THIS ASSIGNMENT, YOU NEED TO HAVE FINISHED THE SECTION TITLED \"Authentication with Auth0\" IN THE PRE-HOMEWORK",
"main": "index.js",
"scripts": {
"start": "nodemon ./index.js"
Expand All @@ -21,5 +21,13 @@
"jwks-rsa": "^1.6.0",
"mysql": "^2.17.1",
"nodemon": "^1.18.11"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/DavidCastillo4/311_wk6_day2_authentication.git"
},
"bugs": {
"url": "https://github.com/DavidCastillo4/311_wk6_day2_authentication/issues"
},
"homepage": "https://github.com/DavidCastillo4/311_wk6_day2_authentication#readme"
}
8 changes: 4 additions & 4 deletions routers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const router = express.Router()

router.get('/', usersController.getAllUsers)

router.get('/:id', usersController.getUserById)
router.get('/:id', checkJwt, usersController.getUserById)

router.post('/', usersController.createUser)
router.post('/', checkJwt, usersController.createUser)

router.put('/:id', usersController.updateUserById)
router.put('/:id', checkJwt, usersController.updateUserById)

router.delete('/:first_name', usersController.deleteUserByFirstName)
router.delete('/:first_name', checkJwt, usersController.deleteUserByFirstName)

module.exports = router
30 changes: 15 additions & 15 deletions sql/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ require('dotenv').config()
const mysql = require('mysql')

class Connection {
constructor() {
if (!this.pool) {
console.log('creating mysql connection...')
this.pool = mysql.createPool({
connectionLimit: 100,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DEFAULT_SCHEMA
})
constructor() {
if (!this.pool) {
console.log('creating mysql connection...')
this.pool = mysql.createPool({
connectionLimit: 100,
host: '35.223.117.232',
user: 'root',
password: '123456',
database: 'Admin'
})

return this.pool
}
return this.pool
}

return this.pool
}
return this.pool
}
}

const instance = new Connection()

module.exports = instance;
module.exports = instance;