Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sonicemma/emma-atanasova
deploy
- Loading branch information
Showing
16 changed files
with
7,295 additions
and
1 deletion.
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 @@ | ||
node_modules/ |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
# back-end | ||
# back-end | ||
# emma-atanasova |
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,3 @@ | ||
module.exports = { | ||
jwtSecret: process.env.JWT_SECRET || "password", | ||
}; |
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,21 @@ | ||
const express = require('express'); | ||
const helmet = require('helmet'); | ||
const cors = require('cors'); | ||
|
||
const authRouter = require ('../auth/auth-router'); | ||
const songRouter = require('../songs/songs-router'); | ||
|
||
const server = express(); | ||
|
||
server.use(express.json()); | ||
server.use(helmet()); | ||
server.use(cors()); | ||
|
||
server.use('/api/auth', authRouter); | ||
server.use('/api/songs', songRouter); | ||
|
||
server.get('/', (req, res) => { | ||
res.json({api: 'works'}); | ||
}); | ||
|
||
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,22 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const secrets = require('../api/secrets.js'); | ||
|
||
module.exports = (req, res, next) => { | ||
const token = req.headers.authorization; | ||
|
||
const secret = secrets.jwtSecret; | ||
|
||
if(token) { | ||
jwt.verify(token, secret, (error, decodedToken) => { | ||
if (error) { | ||
res.status(401).json({ message: 'nice try'}) | ||
} else { | ||
req.decodedToken = decodedToken; | ||
next(); | ||
} | ||
}); | ||
} else { | ||
res.status(400).json({message:'error'}); | ||
} | ||
}; |
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,58 @@ | ||
const router = require('express').Router(); | ||
const bcrypt = require('bcryptjs'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const Users = require('../users/users-model'); | ||
const secrets = require('../api/secrets.js'); | ||
|
||
function generateToken(user) { | ||
const payload = { | ||
userId: user.id, | ||
username: user.email, | ||
}; | ||
const secret = secrets.jwtSecret; | ||
const options = { | ||
expiresIn: '1d', | ||
}; | ||
return jwt.sign(payload, secret, options); | ||
} | ||
|
||
router.post('/register', (req, res) => { | ||
let user = req.body; | ||
|
||
const rounds = process.env.HASH_ROUNDS || 8; | ||
|
||
const hash = bcrypt.hashSync(user.password, rounds); | ||
|
||
user.password = hash; | ||
|
||
Users.add(user) | ||
.then(saved => { | ||
res.status(201).json(saved); | ||
}) | ||
.catch(error => { | ||
res.status(500).json({ message: error.message}); | ||
}); | ||
}); | ||
|
||
router.post('/login', (req, res) => { | ||
let { email, password } = req.body; | ||
|
||
Users.findBy({email}) | ||
.then(([user]) => { | ||
if (user && bcrypt.compareSync(password, user.password)) { | ||
const token = generateToken(user); | ||
|
||
res.status(200).json({message: 'works', token}); | ||
} else { | ||
res.status(401).json({message: 'wrong'}); | ||
} | ||
}) | ||
.catch(err => { | ||
res.status(500).json({message: err.message}); | ||
}); | ||
}); | ||
|
||
module.exports = router; | ||
|
||
module.exports = router; |
Binary file not shown.
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,5 @@ | ||
const knex = require('knex'); | ||
|
||
const knexConfig = require('../knexfile'); | ||
|
||
module.exports = knex(knexConfig.development); |
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,28 @@ | ||
exports.up = function(knex) { | ||
return knex.schema | ||
.createTable('users', tbl => { | ||
tbl.increments(); | ||
tbl.string('email', 128).notNullable().unique(); | ||
tbl.string('password', 128).notNullable(); | ||
}) | ||
|
||
.createTable('songs', tbl => { | ||
tbl.increments(); | ||
tbl.string('song_info').notNullable(); | ||
tbl.string('song_id').notNullable(); | ||
}) | ||
|
||
.createTable('saveSong', tbl => { | ||
tbl.increments(); | ||
tbl.string('song_id').notNullable().references('song_id').inTable('song').onDelete('CASCADE').onUpdate('CASCADE'); | ||
tbl.string('song_info').notNullable().references('song_info').inTable('song').onDelete('CASCADE').onUpdate('CASCADE'); | ||
}) | ||
}; | ||
|
||
exports.down = function(knex) { | ||
return knex.schema | ||
.dropTableIfExists('users') | ||
.dropTableIfExists('songs') | ||
.dropTableIfExists('saveSong'); | ||
}; | ||
|
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,7 @@ | ||
const server = require('./api/server'); | ||
|
||
const port = process.env.PORT || 5000; | ||
|
||
server.listen(port, () => { | ||
console.log(`Listening on port ${port}`); | ||
}); |
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,12 @@ | ||
module.exports = { | ||
development: { | ||
client: 'sqlite3', | ||
connection: { filename: './database/data.db3' }, | ||
useNullAsDefault: true, | ||
migrations: { | ||
directory: './database/migrations', | ||
tableName: 'dbmigrations', | ||
}, | ||
seeds: { directory: './database/seeds' }, | ||
}, | ||
}; |
Oops, something went wrong.