2022-06-01 12:40:52 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2021-12-02 17:46:58 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-01-18 16:55:40 +03:00
|
|
|
const _ = require('lodash');
|
2022-06-01 12:40:52 +03:00
|
|
|
const {BadRequestError, NoPermissionError, NotFoundError, UnauthorizedError} = require('@tryghost/errors');
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
badRequest: 'Bad Request.',
|
|
|
|
notFound: 'Not Found.',
|
|
|
|
offerArchived: 'This offer is archived.',
|
|
|
|
tierArchived: 'This tier is archived.',
|
|
|
|
existingSubscription: 'A subscription exists for this Member.',
|
|
|
|
unableToCheckout: 'Unable to initiate checkout session'
|
|
|
|
};
|
2021-01-18 16:55:40 +03:00
|
|
|
|
|
|
|
module.exports = class RouterController {
|
2021-10-21 14:35:29 +03:00
|
|
|
/**
|
|
|
|
* RouterController
|
|
|
|
*
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {any} deps.offersAPI
|
|
|
|
* @param {any} deps.paymentsService
|
|
|
|
* @param {any} deps.productRepository
|
|
|
|
* @param {any} deps.memberRepository
|
|
|
|
* @param {any} deps.StripePrice
|
2022-01-10 12:42:05 +03:00
|
|
|
* @param {() => boolean} deps.allowSelfSignup
|
2021-10-21 14:35:29 +03:00
|
|
|
* @param {any} deps.magicLinkService
|
|
|
|
* @param {import('@tryghost/members-stripe-service')} deps.stripeAPIService
|
2022-08-18 18:38:42 +03:00
|
|
|
* @param {import('@tryghost/member-attribution')} deps.memberAttributionService
|
2021-10-21 14:35:29 +03:00
|
|
|
* @param {any} deps.tokenService
|
2022-08-25 22:15:34 +03:00
|
|
|
* @param {any} deps.sendEmailWithMagicLink
|
2021-10-21 14:35:29 +03:00
|
|
|
* @param {{isSet(name: string): boolean}} deps.labsService
|
|
|
|
*/
|
2021-01-18 16:55:40 +03:00
|
|
|
constructor({
|
2021-10-13 12:11:12 +03:00
|
|
|
offersAPI,
|
2021-10-21 14:35:29 +03:00
|
|
|
paymentsService,
|
2021-10-06 16:01:04 +03:00
|
|
|
productRepository,
|
2021-01-18 16:55:40 +03:00
|
|
|
memberRepository,
|
2021-05-04 18:57:58 +03:00
|
|
|
StripePrice,
|
2021-01-18 16:55:40 +03:00
|
|
|
allowSelfSignup,
|
|
|
|
magicLinkService,
|
|
|
|
stripeAPIService,
|
|
|
|
tokenService,
|
2022-08-18 18:38:42 +03:00
|
|
|
memberAttributionService,
|
2021-01-27 18:19:09 +03:00
|
|
|
sendEmailWithMagicLink,
|
2022-02-09 16:00:39 +03:00
|
|
|
labsService
|
2021-01-18 16:55:40 +03:00
|
|
|
}) {
|
2021-10-13 12:11:12 +03:00
|
|
|
this._offersAPI = offersAPI;
|
2021-10-21 14:35:29 +03:00
|
|
|
this._paymentsService = paymentsService;
|
2021-10-06 16:01:04 +03:00
|
|
|
this._productRepository = productRepository;
|
2021-01-18 16:55:40 +03:00
|
|
|
this._memberRepository = memberRepository;
|
2021-05-04 18:57:58 +03:00
|
|
|
this._StripePrice = StripePrice;
|
2021-01-18 16:55:40 +03:00
|
|
|
this._allowSelfSignup = allowSelfSignup;
|
|
|
|
this._magicLinkService = magicLinkService;
|
|
|
|
this._stripeAPIService = stripeAPIService;
|
|
|
|
this._tokenService = tokenService;
|
|
|
|
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
|
2022-08-18 18:38:42 +03:00
|
|
|
this._memberAttributionService = memberAttributionService;
|
2021-09-28 14:36:30 +03:00
|
|
|
this.labsService = labsService;
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async ensureStripe(_req, res, next) {
|
2021-01-26 14:26:28 +03:00
|
|
|
if (!this._stripeAPIService.configured) {
|
2021-01-18 16:55:40 +03:00
|
|
|
res.writeHead(400);
|
|
|
|
return res.end('Stripe not configured');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this._stripeAPIService.ready();
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(500);
|
|
|
|
return res.end('There was an error configuring stripe');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async createCheckoutSetupSession(req, res) {
|
|
|
|
const identity = req.body.identity;
|
|
|
|
|
2021-01-19 13:42:39 +03:00
|
|
|
if (!identity) {
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end();
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:55:40 +03:00
|
|
|
let email;
|
|
|
|
try {
|
|
|
|
if (!identity) {
|
|
|
|
email = null;
|
|
|
|
} else {
|
|
|
|
const claims = await this._tokenService.decodeToken(identity);
|
|
|
|
email = claims && claims.sub;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(401);
|
|
|
|
return res.end('Unauthorized');
|
|
|
|
}
|
|
|
|
|
|
|
|
const member = email ? await this._memberRepository.get({email}) : null;
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
res.writeHead(403);
|
|
|
|
return res.end('Bad Request.');
|
|
|
|
}
|
2021-02-23 14:19:21 +03:00
|
|
|
|
|
|
|
let customer;
|
|
|
|
if (!req.body.subscription_id) {
|
|
|
|
customer = await this._stripeAPIService.getCustomerForMemberCheckoutSession(member);
|
|
|
|
} else {
|
|
|
|
const subscriptions = await member.related('stripeSubscriptions').fetch();
|
|
|
|
const subscription = subscriptions.models.find((sub) => {
|
|
|
|
return sub.get('subscription_id') === req.body.subscription_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!subscription) {
|
2022-08-29 15:02:58 +03:00
|
|
|
res.writeHead(404, {
|
|
|
|
'Content-Type': 'text/plain;charset=UTF-8'
|
|
|
|
});
|
|
|
|
return res.end(`Could not find subscription ${req.body.subscription_id}`);
|
2021-02-23 14:19:21 +03:00
|
|
|
}
|
|
|
|
customer = await this._stripeAPIService.getCustomer(subscription.get('customer_id'));
|
|
|
|
}
|
2021-01-18 16:55:40 +03:00
|
|
|
|
|
|
|
const session = await this._stripeAPIService.createCheckoutSetupSession(customer, {
|
2022-02-09 16:00:39 +03:00
|
|
|
successUrl: req.body.successUrl,
|
|
|
|
cancelUrl: req.body.cancelUrl,
|
2021-02-23 14:19:21 +03:00
|
|
|
subscription_id: req.body.subscription_id
|
2021-01-18 16:55:40 +03:00
|
|
|
});
|
|
|
|
const publicKey = this._stripeAPIService.getPublicKey();
|
|
|
|
const sessionInfo = {
|
|
|
|
sessionId: session.id,
|
|
|
|
publicKey
|
|
|
|
};
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
});
|
|
|
|
|
|
|
|
res.end(JSON.stringify(sessionInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
async createCheckoutSession(req, res) {
|
2021-10-06 16:01:04 +03:00
|
|
|
let ghostPriceId = req.body.priceId;
|
2022-05-16 21:27:23 +03:00
|
|
|
const tierId = req.body.tierId;
|
|
|
|
const cadence = req.body.cadence;
|
2021-01-18 16:55:40 +03:00
|
|
|
const identity = req.body.identity;
|
2021-09-28 14:36:30 +03:00
|
|
|
const offerId = req.body.offerId;
|
2022-08-18 18:38:42 +03:00
|
|
|
const metadata = req.body.metadata ?? {};
|
2021-01-18 16:55:40 +03:00
|
|
|
|
2022-05-16 21:27:23 +03:00
|
|
|
if (!ghostPriceId && !offerId && !tierId && !cadence) {
|
2022-06-01 12:40:52 +03:00
|
|
|
throw new BadRequestError({
|
|
|
|
message: tpl(messages.badRequest)
|
|
|
|
});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
2022-05-16 21:27:23 +03:00
|
|
|
if (offerId && (ghostPriceId || (tierId && cadence))) {
|
2022-06-01 12:40:52 +03:00
|
|
|
throw new BadRequestError({
|
|
|
|
message: tpl(messages.badRequest)
|
|
|
|
});
|
2022-05-16 21:27:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ghostPriceId && tierId && cadence) {
|
2022-06-01 12:40:52 +03:00
|
|
|
throw new BadRequestError({
|
|
|
|
message: tpl(messages.badRequest)
|
|
|
|
});
|
2022-05-16 21:27:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tierId && !cadence) {
|
2022-06-01 12:40:52 +03:00
|
|
|
throw new BadRequestError({
|
|
|
|
message: tpl(messages.badRequest)
|
|
|
|
});
|
2022-05-16 21:27:23 +03:00
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:40 +03:00
|
|
|
if (cadence && cadence !== 'month' && cadence !== 'year') {
|
2022-06-01 12:40:52 +03:00
|
|
|
throw new BadRequestError({
|
|
|
|
message: tpl(messages.badRequest)
|
|
|
|
});
|
2021-10-06 16:01:04 +03:00
|
|
|
}
|
|
|
|
|
2021-10-21 14:35:29 +03:00
|
|
|
let couponId = null;
|
2022-08-09 12:13:31 +03:00
|
|
|
let trialDays;
|
2021-11-03 17:13:11 +03:00
|
|
|
if (offerId) {
|
2022-06-01 12:40:52 +03:00
|
|
|
const offer = await this._offersAPI.getOffer({id: offerId});
|
|
|
|
const tier = (await this._productRepository.get(offer.tier)).toJSON();
|
2021-10-06 16:01:04 +03:00
|
|
|
|
2022-06-01 12:40:52 +03:00
|
|
|
if (offer.status === 'archived') {
|
|
|
|
throw new NoPermissionError({
|
|
|
|
message: tpl(messages.offerArchived)
|
|
|
|
});
|
|
|
|
}
|
2021-10-13 12:19:35 +03:00
|
|
|
|
2022-06-01 12:40:52 +03:00
|
|
|
if (offer.cadence === 'month') {
|
|
|
|
ghostPriceId = tier.monthly_price_id;
|
|
|
|
} else {
|
|
|
|
ghostPriceId = tier.yearly_price_id;
|
|
|
|
}
|
2022-08-09 12:13:31 +03:00
|
|
|
// Free trial offers don't have a stripe coupon
|
|
|
|
if (offer.type === 'trial') {
|
|
|
|
trialDays = offer.amount;
|
|
|
|
} else {
|
|
|
|
const coupon = await this._paymentsService.getCouponForOffer(offerId);
|
|
|
|
couponId = coupon.id;
|
|
|
|
}
|
2021-10-18 16:27:17 +03:00
|
|
|
|
2022-06-01 12:40:52 +03:00
|
|
|
metadata.offer = offer.id;
|
2021-10-06 16:01:04 +03:00
|
|
|
}
|
2022-05-16 21:27:23 +03:00
|
|
|
|
2022-08-18 18:38:42 +03:00
|
|
|
// Don't allow to set the source manually
|
|
|
|
delete metadata.attribution_id;
|
|
|
|
delete metadata.attribution_url;
|
|
|
|
delete metadata.attribution_type;
|
2022-08-17 12:07:41 +03:00
|
|
|
|
2022-08-18 18:38:42 +03:00
|
|
|
if (metadata.urlHistory) {
|
|
|
|
// The full attribution history doesn't fit in the Stripe metadata (can't store objects + limited to 50 keys and 500 chars values)
|
|
|
|
// So we need to add top-level attributes with string values
|
|
|
|
const urlHistory = metadata.urlHistory;
|
|
|
|
delete metadata.urlHistory;
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
const attribution = await this._memberAttributionService.getAttribution(urlHistory);
|
2022-08-18 18:38:42 +03:00
|
|
|
|
|
|
|
// Don't set null properties
|
|
|
|
if (attribution.id) {
|
|
|
|
metadata.attribution_id = attribution.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribution.url) {
|
|
|
|
metadata.attribution_url = attribution.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribution.type) {
|
|
|
|
metadata.attribution_type = attribution.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 21:27:23 +03:00
|
|
|
if (!ghostPriceId) {
|
|
|
|
const tier = await this._productRepository.get({id: tierId});
|
|
|
|
if (tier) {
|
|
|
|
if (cadence === 'month') {
|
|
|
|
ghostPriceId = tier.get('monthly_price_id');
|
|
|
|
} else {
|
|
|
|
ghostPriceId = tier.get('yearly_price_id');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 16:01:04 +03:00
|
|
|
|
2021-05-04 18:57:58 +03:00
|
|
|
const price = await this._StripePrice.findOne({
|
|
|
|
id: ghostPriceId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!price) {
|
2022-06-01 12:40:52 +03:00
|
|
|
throw new NotFoundError({
|
|
|
|
message: tpl(messages.notFound)
|
|
|
|
});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
2021-05-04 18:57:58 +03:00
|
|
|
const priceId = price.get('stripe_price_id');
|
2021-01-18 16:55:40 +03:00
|
|
|
|
2022-01-21 11:31:12 +03:00
|
|
|
const product = await this._productRepository.get({stripe_price_id: priceId});
|
2022-08-05 12:58:01 +03:00
|
|
|
|
2022-08-09 12:13:31 +03:00
|
|
|
if (this.labsService.isSet('freeTrial') && !trialDays) {
|
2022-08-05 12:58:01 +03:00
|
|
|
trialDays = product.get('trial_days');
|
|
|
|
}
|
2022-01-21 11:31:12 +03:00
|
|
|
|
|
|
|
if (product.get('active') !== true) {
|
2022-06-01 12:40:52 +03:00
|
|
|
throw new NoPermissionError({
|
|
|
|
message: tpl(messages.tierArchived)
|
|
|
|
});
|
2022-01-21 11:31:12 +03:00
|
|
|
}
|
|
|
|
|
2022-06-01 12:40:52 +03:00
|
|
|
let member = null;
|
|
|
|
if (identity) {
|
|
|
|
try {
|
2021-01-18 16:55:40 +03:00
|
|
|
const claims = await this._tokenService.decodeToken(identity);
|
2022-06-01 12:40:52 +03:00
|
|
|
const email = claims && claims.sub;
|
|
|
|
if (email) {
|
|
|
|
member = await this._memberRepository.get({email}, {withRelated: ['stripeCustomers', 'products']});
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
throw new UnauthorizedError({err});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
2022-06-01 12:40:52 +03:00
|
|
|
} else if (req.body.customerEmail) {
|
|
|
|
member = await this._memberRepository.get({email: req.body.customerEmail}, {withRelated: ['stripeCustomers', 'products']});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
2022-02-09 16:00:39 +03:00
|
|
|
let successUrl = req.body.successUrl;
|
|
|
|
let cancelUrl = req.body.cancelUrl;
|
2021-11-03 11:57:28 +03:00
|
|
|
|
2022-01-04 17:50:24 +03:00
|
|
|
if (!member && req.body.customerEmail && !req.body.successUrl) {
|
|
|
|
const memberExistsForCustomer = await this._memberRepository.get({email: req.body.customerEmail});
|
|
|
|
if (!memberExistsForCustomer) {
|
|
|
|
successUrl = await this._magicLinkService.getMagicLink({
|
|
|
|
tokenData: {
|
2022-08-18 18:38:42 +03:00
|
|
|
email: req.body.customerEmail,
|
|
|
|
attribution: {
|
|
|
|
id: metadata.attribution_id ?? null,
|
|
|
|
type: metadata.attribution_type ?? null,
|
|
|
|
url: metadata.attribution_url ?? null
|
|
|
|
}
|
2022-01-04 17:50:24 +03:00
|
|
|
},
|
|
|
|
type: 'signup'
|
|
|
|
});
|
2021-11-03 11:57:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:55:40 +03:00
|
|
|
if (!member) {
|
|
|
|
const customer = null;
|
2021-05-04 18:57:58 +03:00
|
|
|
const session = await this._stripeAPIService.createCheckoutSession(priceId, customer, {
|
2021-10-21 19:06:36 +03:00
|
|
|
coupon: couponId,
|
2021-11-03 11:57:28 +03:00
|
|
|
successUrl,
|
|
|
|
cancelUrl,
|
2022-08-05 12:58:01 +03:00
|
|
|
trialDays,
|
2021-01-18 16:55:40 +03:00
|
|
|
customerEmail: req.body.customerEmail,
|
2021-10-18 16:27:17 +03:00
|
|
|
metadata: metadata
|
2021-01-18 16:55:40 +03:00
|
|
|
});
|
|
|
|
const publicKey = this._stripeAPIService.getPublicKey();
|
|
|
|
|
|
|
|
const sessionInfo = {
|
|
|
|
publicKey,
|
|
|
|
sessionId: session.id
|
|
|
|
};
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.end(JSON.stringify(sessionInfo));
|
|
|
|
}
|
|
|
|
|
2022-08-17 12:07:41 +03:00
|
|
|
let restrictCheckout = false;
|
|
|
|
if (!this.labsService.isSet('compExpiring')) {
|
|
|
|
restrictCheckout = member.related('products').length !== 0;
|
|
|
|
} else {
|
|
|
|
restrictCheckout = member.get('status') === 'paid';
|
|
|
|
}
|
|
|
|
if (restrictCheckout) {
|
2022-06-01 12:40:52 +03:00
|
|
|
if (!identity && req.body.customerEmail) {
|
|
|
|
try {
|
|
|
|
await this._sendEmailWithMagicLink({email: req.body.customerEmail, requestedType: 'signin'});
|
|
|
|
} catch (err) {
|
|
|
|
logging.warn(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NoPermissionError({
|
|
|
|
message: messages.existingSubscription,
|
|
|
|
code: 'CANNOT_CHECKOUT_WITH_EXISTING_SUBSCRIPTION'
|
|
|
|
});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let stripeCustomer;
|
|
|
|
|
|
|
|
for (const customer of member.related('stripeCustomers').models) {
|
|
|
|
try {
|
|
|
|
const fetchedCustomer = await this._stripeAPIService.getCustomer(customer.get('customer_id'));
|
|
|
|
if (!fetchedCustomer.deleted) {
|
|
|
|
stripeCustomer = fetchedCustomer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Ignoring error for fetching customer for checkout');
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stripeCustomer) {
|
2021-01-26 14:26:57 +03:00
|
|
|
stripeCustomer = await this._stripeAPIService.createCustomer({email: member.get('email')});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-05-04 18:57:58 +03:00
|
|
|
const session = await this._stripeAPIService.createCheckoutSession(priceId, stripeCustomer, {
|
2021-10-21 19:06:36 +03:00
|
|
|
coupon: couponId,
|
2021-11-03 11:57:28 +03:00
|
|
|
successUrl,
|
|
|
|
cancelUrl,
|
2022-08-05 12:58:01 +03:00
|
|
|
trialDays,
|
2021-10-18 16:27:17 +03:00
|
|
|
metadata: metadata
|
2021-01-18 16:55:40 +03:00
|
|
|
});
|
|
|
|
const publicKey = this._stripeAPIService.getPublicKey();
|
|
|
|
|
|
|
|
const sessionInfo = {
|
|
|
|
publicKey,
|
|
|
|
sessionId: session.id
|
|
|
|
};
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.end(JSON.stringify(sessionInfo));
|
2022-06-01 12:40:52 +03:00
|
|
|
} catch (err) {
|
|
|
|
throw new BadRequestError({
|
|
|
|
err,
|
|
|
|
message: tpl(messages.unableToCheckout)
|
|
|
|
});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendMagicLink(req, res) {
|
2022-09-06 15:36:06 +03:00
|
|
|
const {email, emailType, autoRedirect} = req.body;
|
|
|
|
let referer = req.get('referer');
|
|
|
|
if (autoRedirect === false){
|
|
|
|
referer = null;
|
|
|
|
}
|
2021-01-18 16:55:40 +03:00
|
|
|
if (!email) {
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end('Bad Request.');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-01-10 12:42:05 +03:00
|
|
|
if (!this._allowSelfSignup()) {
|
2021-09-22 14:32:02 +03:00
|
|
|
const member = await this._memberRepository.get({email});
|
2021-01-18 16:55:40 +03:00
|
|
|
if (member) {
|
2021-09-22 14:32:02 +03:00
|
|
|
const tokenData = {};
|
2022-09-06 15:36:06 +03:00
|
|
|
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, referrer: referer});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-13 12:25:56 +03:00
|
|
|
const tokenData = _.pick(req.body, ['labels', 'name', 'newsletters']);
|
2022-08-25 22:15:34 +03:00
|
|
|
if (req.ip) {
|
|
|
|
tokenData.reqIp = req.ip;
|
|
|
|
}
|
2022-08-18 18:38:42 +03:00
|
|
|
// Save attribution data in the tokenData
|
2022-09-14 22:50:54 +03:00
|
|
|
tokenData.attribution = await this._memberAttributionService.getAttribution(req.body.urlHistory);
|
2022-08-18 18:38:42 +03:00
|
|
|
|
2022-09-06 15:36:06 +03:00
|
|
|
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, referrer: referer});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
res.writeHead(201);
|
|
|
|
return res.end('Created.');
|
|
|
|
} catch (err) {
|
2021-12-16 10:25:32 +03:00
|
|
|
if (err.code === 'EENVELOPE') {
|
|
|
|
logging.error(err);
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end('Bad Request.');
|
|
|
|
}
|
2021-01-18 16:55:40 +03:00
|
|
|
const statusCode = (err && err.statusCode) || 500;
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.error(err);
|
2021-01-18 16:55:40 +03:00
|
|
|
res.writeHead(statusCode);
|
|
|
|
return res.end('Internal Server Error.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|