Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
165 additions
and
8 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules/ | ||
|
||
config.js | ||
|
||
.env |
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,30 @@ | ||
const {Validator, ValidationError} = require('jsonschema'); | ||
const v = new Validator(); | ||
|
||
|
||
exports.makeKoaValidator = schema => { | ||
return async (ctx, next) => { | ||
// define the controller here | ||
// using the passed-in schema to validate against | ||
|
||
const validationOptions = { | ||
throwError: true, | ||
allowUnknownAttributes: false | ||
}; | ||
|
||
const body = ctx.request.body; | ||
|
||
try { | ||
v.validate(body, schema, validationOptions); | ||
await next(); | ||
} catch (error) { | ||
if (error instanceof ValidationError) { | ||
ctx.body = error; | ||
ctx.status = 400; | ||
} else { | ||
throw 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
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
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
Some generated files are not rendered by default. Learn more.
Oops, something went wrong.
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
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
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
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,35 @@ | ||
module.exports = { | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"id": "/article", | ||
"title": "Article", | ||
"description": "An article in the blog", | ||
"type": "object", | ||
"properties": { | ||
"title": { | ||
"description": "Main title of the blog article", | ||
"type": "string" | ||
}, | ||
"allText": { | ||
"description": "Body text of the blog article", | ||
"type": "string" | ||
}, | ||
"summary": { | ||
"description": "Optional short text summary of article", | ||
"type": "string" | ||
}, | ||
"imageURL": { | ||
"description": "URL for main image to show in article", | ||
"type": "uri" | ||
}, | ||
"published": { | ||
"description": "Is the article published or not", | ||
"type": "boolean" | ||
}, | ||
"authorID": { | ||
"description": "User ID of the article author", | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
}, | ||
"required": ["title", "allText", "authorID"] | ||
} |
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,34 @@ | ||
module.exports = { | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"id": "/user", | ||
"title": "User", | ||
"description": "A user of the blog", | ||
"type": "object", | ||
"properties": { | ||
"username": { | ||
"description": "Username", | ||
"type": "string" | ||
}, | ||
"email": { | ||
"description": "User's email", | ||
"type": "string" | ||
}, | ||
"firstName": { | ||
"description": "User's first name", | ||
"type": "string" | ||
}, | ||
"lastName": { | ||
"description": "User's last name", | ||
"type": "string" | ||
}, | ||
"password": { | ||
"description": "User's password", | ||
"type": "string" | ||
}, | ||
"about": { | ||
"description": "Information about the user", | ||
"type": "string", | ||
}, | ||
}, | ||
"required": ["username", "email", "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 @@ | ||
var JwtStrategy = require('passport-jwt').Strategy, | ||
ExtractJwt = require('passport-jwt').ExtractJwt; | ||
|
||
var opts = {} | ||
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken(); | ||
opts.secretOrKey = 'secret'; | ||
|
||
|
||
passport.use(new JwtStrategy(opts, function(jwt_payload, done) { | ||
User.findOne({id: jwt_payload.sub}, function(err, user) { | ||
if (err) { | ||
return done(err, false); | ||
} | ||
if (user) { | ||
return done(null, user); | ||
} else { | ||
return done(null, false); | ||
// or you could create a new account | ||
} | ||
}); | ||
})); |