Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. var Kinvey = require('kinvey-node-sdk');
  2. var CacheMiddleware = require('kinvey-node-sdk/dist/export').CacheMiddleware;
  3. var Storage = require('kinvey-node-sdk/dist/export').Storage;
  4. var express = require('express');
  5. var bodyParser = require('body-parser');
  6. var service = express();
  7.  
  8. // Create a dumb storage adapter that does not actually store any data.
  9. class DumbStorageAdapter {
  10. find(collection) {
  11. return Promise.resolve([]);
  12. }
  13.  
  14. findById(collection, id) {
  15. return Promise.reject(new NotFoundError(`An entity with _id = ${id} was not found in the ${collection}`
  16. + ` collection on the ${this.name} memory database.`));
  17. }
  18.  
  19. save(collection, entities) {
  20. return Promise.resolve(entities);
  21. }
  22.  
  23. removeById(collection, id) {
  24. return Promise.reject(new NotFoundError(`An entity with _id = ${id} was not found in the ${collection}`
  25. + ` collection on the ${this.name} memory database.`));
  26. }
  27.  
  28. clear() {
  29. return Promise.resolve(null);
  30. }
  31.  
  32. static load(name) {
  33. return new DumbStorageAdapter(name);
  34. }
  35. }
  36.  
  37. // Create a custom storage class that uses the DumbStorageAdapter
  38. class CustomStorage extends Storage {
  39. loadAdapter() {
  40. return Promise.resolve()
  41. .then(() => DumbStorageAdapter.load(this.name))
  42. .then((adapter) => {
  43. if (isDefined(adapter) === false) {
  44. throw new Error('Unable to load a storage adapter.');
  45. }
  46.  
  47. return adapter;
  48. });
  49. }
  50. }
  51.  
  52. // Create a custom cache middleware that uses the CustomStorage
  53. class CustomCacheMiddleware extends CacheMiddleware {
  54. loadStorage(name) {
  55. return new CustomStorage(name);
  56. }
  57. }
  58.  
  59. // Update the Kinvey CacheRack with our CustomCacheMiddleware
  60. Kinvey.CacheRack.reset();
  61. Kinvey.CacheRack.use(new CustomCacheMiddleware());
  62.  
  63. // Setup the service
  64. service.use(bodyParser.json()); // for parsing application/json
  65.  
  66. // Service endpoints
  67. service.post('/login', function (req, res) {
  68. Kinvey.User.loginWithMIC(req.body.redirectUri, Kinvey.AuthorizationGrant.AuthorizationCodeAPI, {
  69. username: req.body.username,
  70. password: req.body.password
  71. })
  72. .then(function(user) {
  73. res.send(user);
  74. })
  75. .catch(function(error) {
  76. res.status(error.code || 500).send({
  77. message: error.message,
  78. debug: error.debug
  79. });
  80. })
  81. });
  82.  
  83. Kinvey.initialize({
  84. appKey: '<appKey>',
  85. appSecret: '<appSecret>'
  86. })
  87. .then(function() {
  88. // Start the service
  89. service.listen(3000, function () {
  90. console.log('Kinvey MIC proxy service is listening on port 3000...')
  91. });
  92. })
  93. .catch(function(error) {
  94. console.log(error);
  95. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement