|
| 1 | +// These methods handle the User-related routes. |
| 2 | + |
| 3 | +import hat from 'hat'; |
| 4 | +import deepcopy from 'deepcopy'; |
| 5 | + |
| 6 | +import ClassesRouter from './ClassesRouter' |
| 7 | +import PromiseRouter from '../PromiseRouter' |
| 8 | +import rest from '../rest'; |
| 9 | +import Auth from '../Auth'; |
| 10 | +import passwordCrypto from '../password'; |
| 11 | +import RestWrite from '../RestWrite'; |
| 12 | + |
| 13 | +const rack = hat.rack(); |
| 14 | + |
| 15 | +export class UsersRouter extends ClassesRouter { |
| 16 | + handleFind(req) { |
| 17 | + req.params.className = '_User'; |
| 18 | + return super.handleFind(req); |
| 19 | + } |
| 20 | + |
| 21 | + handleGet(req) { |
| 22 | + req.params.className = '_User'; |
| 23 | + return super.handleGet(req); |
| 24 | + } |
| 25 | + |
| 26 | + handleCreate(req) { |
| 27 | + let data = deepcopy(req.body); |
| 28 | + data.installationId = req.info.installationId; |
| 29 | + req.body = data; |
| 30 | + req.params.className = '_User'; |
| 31 | + return super.handleCreate(req); |
| 32 | + } |
| 33 | + |
| 34 | + handleUpdate(req) { |
| 35 | + req.params.className = '_User'; |
| 36 | + return super.handleUpdate(req); |
| 37 | + } |
| 38 | + |
| 39 | + handleDelete(req) { |
| 40 | + req.params.className = '_User'; |
| 41 | + return super.handleDelete(req); |
| 42 | + } |
| 43 | + |
| 44 | + handleMe(req) { |
| 45 | + if (!req.info || !req.info.sessionToken) { |
| 46 | + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, |
| 47 | + 'Object not found.'); |
| 48 | + } |
| 49 | + return rest.find(req.config, Auth.master(req.config), '_Session', |
| 50 | + { _session_token: req.info.sessionToken }, |
| 51 | + { include: 'user' }) |
| 52 | + .then((response) => { |
| 53 | + if (!response.results || |
| 54 | + response.results.length == 0 || |
| 55 | + !response.results[0].user) { |
| 56 | + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, |
| 57 | + 'Object not found.'); |
| 58 | + } else { |
| 59 | + let user = response.results[0].user; |
| 60 | + return { response: user }; |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + handleLogIn(req) { |
| 66 | + // Use query parameters instead if provided in url |
| 67 | + if (!req.body.username && req.query.username) { |
| 68 | + req.body = req.query; |
| 69 | + } |
| 70 | + |
| 71 | + // TODO: use the right error codes / descriptions. |
| 72 | + if (!req.body.username) { |
| 73 | + throw new Parse.Error(Parse.Error.USERNAME_MISSING, 'username is required.'); |
| 74 | + } |
| 75 | + if (!req.body.password) { |
| 76 | + throw new Parse.Error(Parse.Error.PASSWORD_MISSING, 'password is required.'); |
| 77 | + } |
| 78 | + |
| 79 | + let user; |
| 80 | + return req.database.find('_User', { username: req.body.username }) |
| 81 | + .then((results) => { |
| 82 | + if (!results.length) { |
| 83 | + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Invalid username/password.'); |
| 84 | + } |
| 85 | + user = results[0]; |
| 86 | + return passwordCrypto.compare(req.body.password, user.password); |
| 87 | + }).then((correct) => { |
| 88 | + if (!correct) { |
| 89 | + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Invalid username/password.'); |
| 90 | + } |
| 91 | + |
| 92 | + let token = 'r:' + rack(); |
| 93 | + user.sessionToken = token; |
| 94 | + delete user.password; |
| 95 | + |
| 96 | + req.config.filesController.expandFilesInObject(req.config, user); |
| 97 | + |
| 98 | + let expiresAt = new Date(); |
| 99 | + expiresAt.setFullYear(expiresAt.getFullYear() + 1); |
| 100 | + |
| 101 | + let sessionData = { |
| 102 | + sessionToken: token, |
| 103 | + user: { |
| 104 | + __type: 'Pointer', |
| 105 | + className: '_User', |
| 106 | + objectId: user.objectId |
| 107 | + }, |
| 108 | + createdWith: { |
| 109 | + 'action': 'login', |
| 110 | + 'authProvider': 'password' |
| 111 | + }, |
| 112 | + restricted: false, |
| 113 | + expiresAt: Parse._encode(expiresAt) |
| 114 | + }; |
| 115 | + |
| 116 | + if (req.info.installationId) { |
| 117 | + sessionData.installationId = req.info.installationId |
| 118 | + } |
| 119 | + |
| 120 | + let create = new RestWrite(req.config, Auth.master(req.config), '_Session', null, sessionData); |
| 121 | + return create.execute(); |
| 122 | + }).then(() => { |
| 123 | + return { response: user }; |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + handleLogOut(req) { |
| 128 | + let success = {response: {}}; |
| 129 | + if (req.info && req.info.sessionToken) { |
| 130 | + return rest.find(req.config, Auth.master(req.config), '_Session', |
| 131 | + { _session_token: req.info.sessionToken } |
| 132 | + ).then((records) => { |
| 133 | + if (records.results && records.results.length) { |
| 134 | + return rest.del(req.config, Auth.master(req.config), '_Session', |
| 135 | + records.results[0].objectId |
| 136 | + ).then(() => { |
| 137 | + return Promise.resolve(success); |
| 138 | + }); |
| 139 | + } |
| 140 | + return Promise.resolve(success); |
| 141 | + }); |
| 142 | + } |
| 143 | + return Promise.resolve(success); |
| 144 | + } |
| 145 | + |
| 146 | + getExpressRouter() { |
| 147 | + let router = new PromiseRouter(); |
| 148 | + |
| 149 | + router.route('GET', '/users', (req) => { return this.handleFind(req); }); |
| 150 | + router.route('POST', '/users', (req) => { return this.handleCreate(req); }); |
| 151 | + router.route('GET', '/users/:objectId', (req) => { return this.handleGet(req); }); |
| 152 | + router.route('PUT', '/users/:objectId', (req) => { return this.handleUpdate(req); }); |
| 153 | + router.route('DELETE', '/users/:objectId', (req) => { return this.handleDelete(req); }); |
| 154 | + |
| 155 | + router.route('GET', '/users/me', (req) => { return this.handleMe(req); }); |
| 156 | + router.route('GET', '/login', (req) => { return this.handleLogIn(req); }); |
| 157 | + router.route('POST', '/logout', (req) => { return this.handleLogOut(req); }); |
| 158 | + router.route('POST', '/requestPasswordReset', () => { |
| 159 | + throw new Parse.Error(Parse.Error.COMMAND_UNAVAILABLE, 'This path is not implemented yet.'); |
| 160 | + }); |
| 161 | + return router; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +export default UsersRouter; |
0 commit comments