BVB Source Codes

parse-server Show SessionsRouter.js Source code

Return Download parse-server: download SessionsRouter.js Source code - Download parse-server Source code - Type:.js
  1.  
  2. import ClassesRouter from './ClassesRouter';
  3. import Parse         from 'parse/node';
  4. import rest          from '../rest';
  5. import Auth          from '../Auth';
  6. import RestWrite     from '../RestWrite';
  7. import { newToken }  from '../cryptoUtils';
  8.  
  9. export class SessionsRouter extends ClassesRouter {
  10.   handleFind(req) {
  11.     req.params.className = '_Session';
  12.     return super.handleFind(req);
  13.   }
  14.  
  15.   handleGet(req) {
  16.     req.params.className = '_Session';
  17.     return super.handleGet(req);
  18.   }
  19.  
  20.   handleCreate(req) {
  21.     req.params.className = '_Session';
  22.     return super.handleCreate(req);
  23.   }
  24.  
  25.   handleUpdate(req) {
  26.     req.params.className = '_Session';
  27.     return super.handleUpdate(req);
  28.   }
  29.  
  30.   handleDelete(req) {
  31.     req.params.className = '_Session';
  32.     return super.handleDelete(req);
  33.   }
  34.  
  35.   handleMe(req) {
  36.     // TODO: Verify correct behavior
  37.     if (!req.info || !req.info.sessionToken) {
  38.       throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN,
  39.         'Session token required.');
  40.     }
  41.     return rest.find(req.config, Auth.master(req.config), '_Session', { sessionToken: req.info.sessionToken }, undefined, req.info.clientSDK)
  42.       .then((response) => {
  43.         if (!response.results || response.results.length == 0) {
  44.           throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN,
  45.             'Session token not found.');
  46.         }
  47.         return {
  48.           response: response.results[0]
  49.         };
  50.       });
  51.   }
  52.  
  53.   handleUpdateToRevocableSession(req) {
  54.     const config = req.config;
  55.     const masterAuth = Auth.master(config)
  56.     const user = req.auth.user;
  57.     // Issue #2720
  58.     // Calling without a session token would result in a not found user
  59.     if (!user) {
  60.       throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'invalid session');
  61.     }
  62.     const expiresAt = config.generateSessionExpiresAt();
  63.     const sessionData = {
  64.       sessionToken: 'r:' + newToken(),
  65.       user: {
  66.         __type: 'Pointer',
  67.         className: '_User',
  68.         objectId: user.id
  69.       },
  70.       createdWith: {
  71.         'action': 'upgrade',
  72.       },
  73.       restricted: false,
  74.       installationId: req.auth.installationId,
  75.       expiresAt: Parse._encode(expiresAt)
  76.     };
  77.     const create = new RestWrite(config, masterAuth, '_Session', null, sessionData);
  78.     return create.execute().then(() => {
  79.       // delete the session token, use the db to skip beforeSave
  80.       return config.database.update('_User', {
  81.         objectId: user.id
  82.       }, {
  83.         sessionToken: {__op: 'Delete'}
  84.       });
  85.     }).then(() => {
  86.       return Promise.resolve({ response: sessionData });
  87.     });
  88.   }
  89.  
  90.   mountRoutes() {
  91.     this.route('GET','/sessions/me', req => { return this.handleMe(req); });
  92.     this.route('GET', '/sessions', req => { return this.handleFind(req); });
  93.     this.route('GET', '/sessions/:objectId', req => { return this.handleGet(req); });
  94.     this.route('POST', '/sessions', req => { return this.handleCreate(req); });
  95.     this.route('PUT', '/sessions/:objectId', req => { return this.handleUpdate(req); });
  96.     this.route('DELETE', '/sessions/:objectId', req => { return this.handleDelete(req); });
  97.     this.route('POST', '/upgradeToRevocableSession', req => { return this.handleUpdateToRevocableSession(req); })
  98.   }
  99. }
  100.  
  101. export default SessionsRouter;
  102.  
downloadSessionsRouter.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top