Refactor and advancements
- Drops mailController, centralized in UserController - Adds views folder for change_password - Improves PromiseRouter to support text results - Improves PromiseRouter to support empty responses for redirects - Adds options to AdaptableController - UsersController gracefully fails when no adapter is set - Refactors GlobalConfig into same style for Routers
This commit is contained in:
48
src/Routers/GlobalConfigRouter.js
Normal file
48
src/Routers/GlobalConfigRouter.js
Normal file
@@ -0,0 +1,48 @@
|
||||
// global_config.js
|
||||
|
||||
var Parse = require('parse/node').Parse;
|
||||
|
||||
import PromiseRouter from '../PromiseRouter';
|
||||
|
||||
export class GlobalConfigRouter extends PromiseRouter {
|
||||
getGlobalConfig(req) {
|
||||
return req.config.database.rawCollection('_GlobalConfig')
|
||||
.then(coll => coll.findOne({'_id': 1}))
|
||||
.then(globalConfig => ({response: { params: globalConfig.params }}))
|
||||
.catch(() => ({
|
||||
status: 404,
|
||||
response: {
|
||||
code: Parse.Error.INVALID_KEY_NAME,
|
||||
error: 'config does not exist',
|
||||
}
|
||||
}));
|
||||
}
|
||||
updateGlobalConfig(req) {
|
||||
if (!req.auth.isMaster) {
|
||||
return Promise.resolve({
|
||||
status: 401,
|
||||
response: {error: 'unauthorized'},
|
||||
});
|
||||
}
|
||||
|
||||
return req.config.database.rawCollection('_GlobalConfig')
|
||||
.then(coll => coll.findOneAndUpdate({ _id: 1 }, { $set: req.body }))
|
||||
.then(response => {
|
||||
return { response: { result: true } }
|
||||
})
|
||||
.catch(() => ({
|
||||
status: 404,
|
||||
response: {
|
||||
code: Parse.Error.INVALID_KEY_NAME,
|
||||
error: 'config cannot be updated',
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
mountRoutes() {
|
||||
this.route('GET', '/config', req => { return this.getGlobalConfig(req) });
|
||||
this.route('PUT', '/config', req => { return this.updateGlobalConfig(req) });
|
||||
}
|
||||
}
|
||||
|
||||
export default GlobalConfigRouter;
|
||||
@@ -3,6 +3,10 @@ import UserController from '../Controllers/UserController';
|
||||
import Config from '../Config';
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
let public_html = path.resolve(__dirname, "../../public_html");
|
||||
let views = path.resolve(__dirname, '../../views');
|
||||
|
||||
export class PublicAPIRouter extends PromiseRouter {
|
||||
|
||||
@@ -13,33 +17,75 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
var config = new Config(appId);
|
||||
|
||||
if (!token || !username) {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: config.invalidLinkURL
|
||||
});
|
||||
return this.invalidLink(req);
|
||||
}
|
||||
|
||||
let userController = new UserController(appId);
|
||||
let userController = config.userController;
|
||||
return userController.verifyEmail(username, token, appId).then( () => {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.verifyEmailSuccessURL}?username=${username}`
|
||||
});
|
||||
}, ()=> {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: config.invalidLinkURL
|
||||
});
|
||||
return this.invalidLink(req);
|
||||
})
|
||||
}
|
||||
|
||||
changePassword(req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var config = new Config(req.params.appId);
|
||||
// Should we keep the file in memory or leave like that?
|
||||
fs.readFile(path.resolve(views, "choose_password"), 'utf-8', (err, data) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
data = data.replace("PARSE_SERVER_URL", `'${config.serverURL}'`);
|
||||
resolve({
|
||||
text: data
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
resetPassword(req) {
|
||||
var { username, token } = req.params;
|
||||
|
||||
if (!username || !token) {
|
||||
return this.invalidLink(req);
|
||||
}
|
||||
|
||||
let config = req.config;
|
||||
return config.userController.checkResetTokenValidity(username, token).then( () => {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.choosePasswordURL}?token=${token}&id=${config.applicationId}&username=${username}`
|
||||
})
|
||||
}, () => {
|
||||
return this.invalidLink(req);
|
||||
})
|
||||
}
|
||||
|
||||
invalidLink(req) {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: req.config.invalidLinkURL
|
||||
});
|
||||
}
|
||||
|
||||
setConfig(req) {
|
||||
req.config = new Config(req.params.appId);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
mountRoutes() {
|
||||
this.route('GET','/apps/:appId/verify_email', req => { return this.verifyEmail(req); });
|
||||
this.route('GET','/apps/:appId/verify_email', this.setConfig, req => { return this.verifyEmail(req); });
|
||||
this.route('GET','/apps/choose_password', req => { return this.changePassword(req); });
|
||||
this.route('GET','/apps/:appId/request_password_reset', this.setConfig, req => { return this.resetPassword(req); });
|
||||
}
|
||||
|
||||
expressApp() {
|
||||
var router = express();
|
||||
router.use("/apps", express.static(path.resolve(__dirname, "../../public")));
|
||||
router.use("/apps", express.static(public_html));
|
||||
router.use(super.expressApp());
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -27,16 +27,14 @@ export class UsersRouter extends ClassesRouter {
|
||||
req.body = data;
|
||||
req.params.className = '_User';
|
||||
|
||||
if (req.config.verifyUserEmails) {
|
||||
req.config.mailController.setEmailVerificationStatus(req.body, false);
|
||||
}
|
||||
req.config.userController.setEmailVerifyToken(req.body);
|
||||
|
||||
let p = super.handleCreate(req);
|
||||
|
||||
if (req.config.verifyUserEmails) {
|
||||
|
||||
if (req.config.verifyUserEmails) {
|
||||
// Send email as fire-and-forget once the user makes it into the DB.
|
||||
p.then(() => {
|
||||
req.config.mailController.sendVerificationEmail(req.body, req.config);
|
||||
req.config.userController.sendVerificationEmail(req.body, req.config);
|
||||
});
|
||||
}
|
||||
return p;
|
||||
@@ -155,10 +153,23 @@ export class UsersRouter extends ClassesRouter {
|
||||
return Promise.resolve(success);
|
||||
}
|
||||
|
||||
handleReset(req) {
|
||||
handleResetRequest(req) {
|
||||
|
||||
let { email } = req.body.email;
|
||||
if (!email) {
|
||||
throw "Missing email";
|
||||
}
|
||||
let userController = req.config.userController;
|
||||
return userController.requestPasswordReset();
|
||||
|
||||
return userController.sendPasswordResetEmail(email).then((token) => {
|
||||
return Promise.resolve({
|
||||
response: {}
|
||||
})
|
||||
}, (err) => {
|
||||
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `no user found with email ${email}`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
mountRoutes() {
|
||||
this.route('GET', '/users', req => { return this.handleFind(req); });
|
||||
@@ -169,7 +180,7 @@ export class UsersRouter extends ClassesRouter {
|
||||
this.route('DELETE', '/users/:objectId', req => { return this.handleDelete(req); });
|
||||
this.route('GET', '/login', req => { return this.handleLogIn(req); });
|
||||
this.route('POST', '/logout', req => { return this.handleLogOut(req); });
|
||||
this.route('POST', '/requestPasswordReset', req => this.handleReset(req));
|
||||
this.route('POST', '/requestPasswordReset', req => { return this.handleResetRequest(req); })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user