-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const config = { | ||
jwtKey: 'MRePUBWaNW7wvNKMcOCkGlo6ZipKs4HKiRq2yAYV33FroszPKV0kOsnbtaIM', | ||
databaseUrl: 'mongodb://localhost:27017/myproject' | ||
}; | ||
|
||
module.exports = config; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: '3.3' | ||
|
||
services: | ||
mongo: | ||
image: mongo:3.4.10 | ||
expose: | ||
- 27017 | ||
ports: | ||
- "27017:27017" | ||
volumes: | ||
- eventslocaldb:/data/db | ||
|
||
volumes: | ||
eventslocaldb: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const path = require('path'); | ||
const hapi = require('hapi'); | ||
const mongoose = require('mongoose'); | ||
const _ = require('lodash'); | ||
|
||
const config = require(path.resolve('config/config')); | ||
|
||
mongoose.connect(config.databaseUrl); | ||
|
||
require(path.resolve('./models/users')); | ||
|
||
const User = mongoose.model('User'); | ||
|
||
const init = async () => { | ||
// Create a server with a host and port | ||
const server = hapi.server({ | ||
port: 3000 | ||
}); | ||
|
||
await server.register([require('vision'), require('inert'), require('lout'), require('hapi-auth-jwt2')]); | ||
|
||
server.auth.strategy('jwt', 'jwt', | ||
{ | ||
key: config.jwtKey, | ||
validate: async function (decoded, request) { | ||
try { | ||
const user = await User | ||
.findOne({email: decoded.email}) | ||
.exec(); | ||
if (!user) { | ||
return {isValid: false}; | ||
} | ||
request.user = user; | ||
return {isValid: true}; | ||
} catch (e) { | ||
return {isValid: false}; | ||
} | ||
}, | ||
verifyOptions: {algorithms: ['HS256']} | ||
}); | ||
|
||
server.auth.default('jwt'); | ||
|
||
let routes = []; | ||
const authenticationRoutes = require(path.resolve('routes/authentication')); | ||
const profileRoutes = require(path.resolve('routes/profile')); | ||
const eventsRoutes = require(path.resolve('routes/events')); | ||
|
||
routes.push(authenticationRoutes); | ||
routes.push(profileRoutes); | ||
routes.push(eventsRoutes); | ||
|
||
routes = _.flatMapDeep(routes, (route) => { | ||
return route | ||
}); | ||
|
||
// Add the routes | ||
server.route(routes); | ||
|
||
await server.start(); | ||
return server; | ||
}; | ||
|
||
|
||
init().then(server => { | ||
console.log('Server running at:', server.info.uri); | ||
}).catch(error => { | ||
console.log(error); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const UserEventSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
location: { | ||
type: String, | ||
required: true | ||
} | ||
}); | ||
|
||
module.exports = UserEventSchema; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const mongoose = require('mongoose'); | ||
const crypto = require('crypto'); | ||
const path = require('path'); | ||
|
||
const UserEventSchema = require(path.resolve('models/user_event_schema')) | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
name: { | ||
type: String | ||
}, | ||
email: { | ||
type: String, | ||
unique: true | ||
}, | ||
password: { | ||
type: String | ||
}, | ||
salt: { | ||
type: String | ||
}, | ||
events: [UserEventSchema] | ||
}); | ||
|
||
UserSchema.pre('save', function (next) { | ||
if (this.password && this.isModified('password')) { | ||
this.salt = crypto.randomBytes(16).toString('base64'); | ||
this.password = this.hashPassword(this.password) | ||
} | ||
next(); | ||
}); | ||
|
||
UserSchema.methods.hashPassword = function (password) { | ||
if (this.salt && password) { | ||
return crypto.pbkdf2Sync(password, this.salt, 10000, 64, 'sha1').toString('base64'); | ||
} else { | ||
return password; | ||
} | ||
}; | ||
|
||
module.exports = mongoose.model('User', UserSchema); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.