@@ -5,17 +5,16 @@ import path from 'path';
|
||||
import fs from 'fs';
|
||||
import qs from 'querystring';
|
||||
|
||||
const public_html = path.resolve(__dirname, "../../public_html");
|
||||
const public_html = path.resolve(__dirname, '../../public_html');
|
||||
const views = path.resolve(__dirname, '../../views');
|
||||
|
||||
export class PublicAPIRouter extends PromiseRouter {
|
||||
|
||||
verifyEmail(req) {
|
||||
const { token, username } = req.query;
|
||||
const appId = req.params.appId;
|
||||
const config = Config.get(appId);
|
||||
|
||||
if(!config){
|
||||
if (!config) {
|
||||
this.invalidRequest();
|
||||
}
|
||||
|
||||
@@ -28,15 +27,18 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
}
|
||||
|
||||
const userController = config.userController;
|
||||
return userController.verifyEmail(username, token).then(() => {
|
||||
const params = qs.stringify({username});
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.verifyEmailSuccessURL}?${params}`
|
||||
});
|
||||
}, ()=> {
|
||||
return this.invalidVerificationLink(req);
|
||||
})
|
||||
return userController.verifyEmail(username, token).then(
|
||||
() => {
|
||||
const params = qs.stringify({ username });
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.verifyEmailSuccessURL}?${params}`,
|
||||
});
|
||||
},
|
||||
() => {
|
||||
return this.invalidVerificationLink(req);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
resendVerificationEmail(req) {
|
||||
@@ -44,7 +46,7 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
const appId = req.params.appId;
|
||||
const config = Config.get(appId);
|
||||
|
||||
if(!config){
|
||||
if (!config) {
|
||||
this.invalidRequest();
|
||||
}
|
||||
|
||||
@@ -58,51 +60,60 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
|
||||
const userController = config.userController;
|
||||
|
||||
return userController.resendVerificationEmail(username).then(() => {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.linkSendSuccessURL}`
|
||||
});
|
||||
}, ()=> {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.linkSendFailURL}`
|
||||
});
|
||||
})
|
||||
return userController.resendVerificationEmail(username).then(
|
||||
() => {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.linkSendSuccessURL}`,
|
||||
});
|
||||
},
|
||||
() => {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.linkSendFailURL}`,
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
changePassword(req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const config = Config.get(req.query.id);
|
||||
|
||||
if(!config){
|
||||
if (!config) {
|
||||
this.invalidRequest();
|
||||
}
|
||||
|
||||
if (!config.publicServerURL) {
|
||||
return resolve({
|
||||
status: 404,
|
||||
text: 'Not found.'
|
||||
text: 'Not found.',
|
||||
});
|
||||
}
|
||||
// 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);
|
||||
fs.readFile(
|
||||
path.resolve(views, 'choose_password'),
|
||||
'utf-8',
|
||||
(err, data) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
data = data.replace(
|
||||
'PARSE_SERVER_URL',
|
||||
`'${config.publicServerURL}'`
|
||||
);
|
||||
resolve({
|
||||
text: data,
|
||||
});
|
||||
}
|
||||
data = data.replace("PARSE_SERVER_URL", `'${config.publicServerURL}'`);
|
||||
resolve({
|
||||
text: data
|
||||
})
|
||||
});
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
requestResetPassword(req) {
|
||||
|
||||
const config = req.config;
|
||||
|
||||
if(!config){
|
||||
if (!config) {
|
||||
this.invalidRequest();
|
||||
}
|
||||
|
||||
@@ -116,22 +127,29 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
return this.invalidLink(req);
|
||||
}
|
||||
|
||||
return config.userController.checkResetTokenValidity(username, token).then(() => {
|
||||
const params = qs.stringify({token, id: config.applicationId, username, app: config.appName, });
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.choosePasswordURL}?${params}`
|
||||
})
|
||||
}, () => {
|
||||
return this.invalidLink(req);
|
||||
})
|
||||
return config.userController.checkResetTokenValidity(username, token).then(
|
||||
() => {
|
||||
const params = qs.stringify({
|
||||
token,
|
||||
id: config.applicationId,
|
||||
username,
|
||||
app: config.appName,
|
||||
});
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.choosePasswordURL}?${params}`,
|
||||
});
|
||||
},
|
||||
() => {
|
||||
return this.invalidLink(req);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
resetPassword(req) {
|
||||
|
||||
const config = req.config;
|
||||
|
||||
if(!config){
|
||||
if (!config) {
|
||||
this.invalidRequest();
|
||||
}
|
||||
|
||||
@@ -139,46 +157,55 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
return this.missingPublicServerURL();
|
||||
}
|
||||
|
||||
const {
|
||||
username,
|
||||
token,
|
||||
new_password
|
||||
} = req.body;
|
||||
const { username, token, new_password } = req.body;
|
||||
|
||||
if (!username || !token || !new_password) {
|
||||
return this.invalidLink(req);
|
||||
}
|
||||
|
||||
return config.userController.updatePassword(username, token, new_password).then(() => {
|
||||
const params = qs.stringify({username: username});
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.passwordResetSuccessURL}?${params}`
|
||||
});
|
||||
}, (err) => {
|
||||
const params = qs.stringify({username: username, token: token, id: config.applicationId, error:err, app:config.appName});
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.choosePasswordURL}?${params}`
|
||||
});
|
||||
});
|
||||
|
||||
return config.userController
|
||||
.updatePassword(username, token, new_password)
|
||||
.then(
|
||||
() => {
|
||||
const params = qs.stringify({ username: username });
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.passwordResetSuccessURL}?${params}`,
|
||||
});
|
||||
},
|
||||
err => {
|
||||
const params = qs.stringify({
|
||||
username: username,
|
||||
token: token,
|
||||
id: config.applicationId,
|
||||
error: err,
|
||||
app: config.appName,
|
||||
});
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.choosePasswordURL}?${params}`,
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
invalidLink(req) {
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: req.config.invalidLinkURL
|
||||
location: req.config.invalidLinkURL,
|
||||
});
|
||||
}
|
||||
|
||||
invalidVerificationLink(req) {
|
||||
const config = req.config;
|
||||
if (req.query.username && req.params.appId) {
|
||||
const params = qs.stringify({username: req.query.username, appId: req.params.appId});
|
||||
const params = qs.stringify({
|
||||
username: req.query.username,
|
||||
appId: req.params.appId,
|
||||
});
|
||||
return Promise.resolve({
|
||||
status: 302,
|
||||
location: `${config.invalidVerificationLinkURL}?${params}`
|
||||
location: `${config.invalidVerificationLinkURL}?${params}`,
|
||||
});
|
||||
} else {
|
||||
return this.invalidLink(req);
|
||||
@@ -187,15 +214,15 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
|
||||
missingPublicServerURL() {
|
||||
return Promise.resolve({
|
||||
text: 'Not found.',
|
||||
status: 404
|
||||
text: 'Not found.',
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
|
||||
invalidRequest() {
|
||||
const error = new Error();
|
||||
error.status = 403;
|
||||
error.message = "unauthorized";
|
||||
error.message = 'unauthorized';
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -205,30 +232,59 @@ export class PublicAPIRouter extends PromiseRouter {
|
||||
}
|
||||
|
||||
mountRoutes() {
|
||||
this.route('GET','/apps/:appId/verify_email',
|
||||
req => { this.setConfig(req) },
|
||||
req => { return this.verifyEmail(req); });
|
||||
this.route(
|
||||
'GET',
|
||||
'/apps/:appId/verify_email',
|
||||
req => {
|
||||
this.setConfig(req);
|
||||
},
|
||||
req => {
|
||||
return this.verifyEmail(req);
|
||||
}
|
||||
);
|
||||
|
||||
this.route('POST', '/apps/:appId/resend_verification_email',
|
||||
req => { this.setConfig(req); },
|
||||
req => { return this.resendVerificationEmail(req); });
|
||||
this.route(
|
||||
'POST',
|
||||
'/apps/:appId/resend_verification_email',
|
||||
req => {
|
||||
this.setConfig(req);
|
||||
},
|
||||
req => {
|
||||
return this.resendVerificationEmail(req);
|
||||
}
|
||||
);
|
||||
|
||||
this.route('GET','/apps/choose_password',
|
||||
req => { return this.changePassword(req); });
|
||||
this.route('GET', '/apps/choose_password', req => {
|
||||
return this.changePassword(req);
|
||||
});
|
||||
|
||||
this.route('POST','/apps/:appId/request_password_reset',
|
||||
req => { this.setConfig(req) },
|
||||
req => { return this.resetPassword(req); });
|
||||
this.route(
|
||||
'POST',
|
||||
'/apps/:appId/request_password_reset',
|
||||
req => {
|
||||
this.setConfig(req);
|
||||
},
|
||||
req => {
|
||||
return this.resetPassword(req);
|
||||
}
|
||||
);
|
||||
|
||||
this.route('GET','/apps/:appId/request_password_reset',
|
||||
req => { this.setConfig(req) },
|
||||
req => { return this.requestResetPassword(req); });
|
||||
this.route(
|
||||
'GET',
|
||||
'/apps/:appId/request_password_reset',
|
||||
req => {
|
||||
this.setConfig(req);
|
||||
},
|
||||
req => {
|
||||
return this.requestResetPassword(req);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
expressRouter() {
|
||||
const router = express.Router();
|
||||
router.use("/apps", express.static(public_html));
|
||||
router.use("/", super.expressRouter());
|
||||
router.use('/apps', express.static(public_html));
|
||||
router.use('/', super.expressRouter());
|
||||
return router;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user