Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Kinvey = require('kinvey-node-sdk');
- var CacheMiddleware = require('kinvey-node-sdk/dist/export').CacheMiddleware;
- var Storage = require('kinvey-node-sdk/dist/export').Storage;
- var express = require('express');
- var bodyParser = require('body-parser');
- var service = express();
- // Create a dumb storage adapter that does not actually store any data.
- class DumbStorageAdapter {
- find(collection) {
- return Promise.resolve([]);
- }
- findById(collection, id) {
- return Promise.reject(new NotFoundError(`An entity with _id = ${id} was not found in the ${collection}`
- + ` collection on the ${this.name} memory database.`));
- }
- save(collection, entities) {
- return Promise.resolve(entities);
- }
- removeById(collection, id) {
- return Promise.reject(new NotFoundError(`An entity with _id = ${id} was not found in the ${collection}`
- + ` collection on the ${this.name} memory database.`));
- }
- clear() {
- return Promise.resolve(null);
- }
- static load(name) {
- return new DumbStorageAdapter(name);
- }
- }
- // Create a custom storage class that uses the DumbStorageAdapter
- class CustomStorage extends Storage {
- loadAdapter() {
- return Promise.resolve()
- .then(() => DumbStorageAdapter.load(this.name))
- .then((adapter) => {
- if (isDefined(adapter) === false) {
- throw new Error('Unable to load a storage adapter.');
- }
- return adapter;
- });
- }
- }
- // Create a custom cache middleware that uses the CustomStorage
- class CustomCacheMiddleware extends CacheMiddleware {
- loadStorage(name) {
- return new CustomStorage(name);
- }
- }
- // Update the Kinvey CacheRack with our CustomCacheMiddleware
- Kinvey.CacheRack.reset();
- Kinvey.CacheRack.use(new CustomCacheMiddleware());
- // Setup the service
- service.use(bodyParser.json()); // for parsing application/json
- // Service endpoints
- service.post('/login', function (req, res) {
- Kinvey.User.loginWithMIC(req.body.redirectUri, Kinvey.AuthorizationGrant.AuthorizationCodeAPI, {
- username: req.body.username,
- password: req.body.password
- })
- .then(function(user) {
- res.send(user);
- })
- .catch(function(error) {
- res.status(error.code || 500).send({
- message: error.message,
- debug: error.debug
- });
- })
- });
- Kinvey.initialize({
- appKey: '<appKey>',
- appSecret: '<appSecret>'
- })
- .then(function() {
- // Start the service
- service.listen(3000, function () {
- console.log('Kinvey MIC proxy service is listening on port 3000...')
- });
- })
- .catch(function(error) {
- console.log(error);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement