@stackend/api
Version:
JS bindings to api.stackend.com
308 lines • 11.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.changePassword = exports.sendPasswordChangeToken = exports.performLoginRedirect = exports.login = exports.logout = exports._getLogoutUrl = exports._getLoginUrl = exports.getAuthenticationTypeByUserPermalink = exports.getAuthenticationTypeName = exports.AuthenticationType = void 0;
var get_1 = __importDefault(require("lodash/get"));
var api_1 = require("../api");
var AccessToken_1 = require("../api/AccessToken");
/**
* Ways to log in.
*/
var AuthenticationType;
(function (AuthenticationType) {
/**
* Login using facebook
*/
AuthenticationType["FACEBOOK"] = "FACEBOOK";
/**
* Login using email+password
*/
AuthenticationType["XCAP"] = "XCAP";
/**
* Login using google token
*/
AuthenticationType["GOOGLE"] = "GOOGLE";
/**
* Custom oauth solution
*/
AuthenticationType["OAUTH2"] = "OAUTH2";
})(AuthenticationType = exports.AuthenticationType || (exports.AuthenticationType = {}));
var AUTH_NAMES = (_a = {},
_a[AuthenticationType.FACEBOOK] = 'Facebook',
_a[AuthenticationType.GOOGLE] = 'Google',
_a[AuthenticationType.XCAP] = 'E-mail & password',
_a[AuthenticationType.OAUTH2] = 'OAuth2',
_a);
function getAuthenticationTypeName(type) {
if (AUTH_NAMES[type]) {
return AUTH_NAMES[type];
}
return AUTH_NAMES[AuthenticationType.XCAP];
}
exports.getAuthenticationTypeName = getAuthenticationTypeName;
/**
* Given a user permalink, get the authentication type
* @param permalink
* @returns {string}
*/
function getAuthenticationTypeByUserPermalink(permalink) {
var provider = AuthenticationType.XCAP;
if (permalink.startsWith('g_')) {
provider = AuthenticationType.GOOGLE;
}
else if (permalink.startsWith('fb_')) {
provider = AuthenticationType.FACEBOOK;
}
else if (permalink.startsWith('o_')) {
provider = AuthenticationType.OAUTH2;
}
return provider;
}
exports.getAuthenticationTypeByUserPermalink = getAuthenticationTypeByUserPermalink;
/**
* Get the login url.
*
* @param config
* @param request
* @param returnUrl
* @param communityPermalink
* @param provider
* @returns string
*/
function _getLoginUrl(_a) {
var config = _a.config, request = _a.request, returnUrl = _a.returnUrl, communityPermalink = _a.communityPermalink, provider = _a.provider;
var pfx = (0, api_1._getServerWithContextPath)(config);
switch (provider) {
case AuthenticationType.FACEBOOK:
return (pfx +
'/facebook/?login=true' +
(returnUrl ? '&returnurl=' + encodeURIComponent(returnUrl) : '') +
(communityPermalink ? '&c=' + encodeURIComponent(communityPermalink) : ''));
case AuthenticationType.XCAP: {
var parameters = {};
if (returnUrl) {
parameters.returnUrl = returnUrl;
}
if (communityPermalink) {
parameters.c = communityPermalink;
}
parameters.email = (0, get_1.default)(arguments[0], 'email');
parameters.password = (0, get_1.default)(arguments[0], 'password');
return (0, api_1._getApiUrl)({
state: { request: request, config: config, communities: {} },
url: '/user/login',
parameters: parameters
});
}
case AuthenticationType.GOOGLE:
return (pfx +
'/google?login=true' +
(returnUrl ? '&r=' + encodeURIComponent(returnUrl) : '') +
(communityPermalink ? '&c=' + encodeURIComponent(communityPermalink) : ''));
case AuthenticationType.OAUTH2:
return (pfx +
'/oauth2?login=true' +
(returnUrl ? '&r=' + encodeURIComponent(returnUrl) : '') +
(communityPermalink ? '&c=' + encodeURIComponent(communityPermalink) : ''));
default:
throw Error('_getLoginUrl() provider "' + provider + '" not supported');
}
}
exports._getLoginUrl = _getLoginUrl;
function _getReturnUrl(_a) {
var request = _a.request, returnUrl = _a.returnUrl;
var pfx = '';
if (!returnUrl) {
returnUrl = '';
returnUrl += (0, get_1.default)(request, 'location.href', '');
returnUrl += (0, get_1.default)(request, 'location.search', '');
return encodeURIComponent(returnUrl);
}
else {
// No proto
if (returnUrl.indexOf('//') === -1) {
// Special case for /stacks
if (request.location.pathname.indexOf('/stacks/') !== -1) {
pfx = request.absoluteUrl + request.contextPath;
}
else {
pfx = request.absoluteCommunityUrl;
}
}
else {
pfx = '';
}
}
return encodeURIComponent(pfx + returnUrl);
}
/**
* Get the logout url.
* @param config
* @param request
* @param returnUrl
* @returns {string}
* @deprecated Should not link directly to logout. It requires a post with a token.
*/
function _getLogoutUrl(_a) {
var config = _a.config, request = _a.request, returnUrl = _a.returnUrl;
var ru = _getReturnUrl({ request: request, returnUrl: returnUrl });
return (0, api_1._getApiUrl)({
state: { request: request, config: config, communities: {} },
url: '/user/logout',
parameters: { redirectUrl: ru }
});
//const pfx = _getServerWithContextPath(config);
//return pfx + '/logout?redirectUrl=' + ru;
}
exports._getLogoutUrl = _getLogoutUrl;
/**
* Logout
* @param redirectUrl
* @returns {Thunk<XcapJsonResult>}
*/
function logout(_a) {
var redirectUrl = _a.redirectUrl;
var r = (0, api_1.post)({
url: '/user/logout',
parameters: arguments
});
(0, AccessToken_1.clearPersistentData)();
(0, AccessToken_1.clearAccessToken)();
return r;
}
exports.logout = logout;
/**
* Login.
* This will redirect the user to the provider specific login url.
* Client side only!
* Some login providers may have extra parameters.
* The email provider requires email and password to be supplied.
*/
function login(props) {
var _a;
// FIXME: return type
var provider = props.provider, email = props.email, password = props.password, returnUrl = props.returnUrl, config = props.config, request = props.request;
var communityPermalink = props[api_1.COMMUNITY_PARAMETER];
switch (provider) {
case AuthenticationType.XCAP: {
return (0, api_1.post)({
url: '/user/login',
parameters: (_a = {
returnUrl: returnUrl,
c: communityPermalink,
xcap_email: email,
xcap_password: password
},
_a[api_1.COMMUNITY_PARAMETER] = communityPermalink,
_a)
});
}
case AuthenticationType.FACEBOOK:
case AuthenticationType.GOOGLE:
case AuthenticationType.OAUTH2: {
var url_1 = _getLoginUrl({ provider: provider, returnUrl: returnUrl, config: config, request: request, communityPermalink: communityPermalink });
if ((0, api_1.isRunningInBrowser)()) {
window.location = url_1;
}
return function () { return url_1; };
}
default:
throw Error('login(): provider "' + provider + '" not supported');
}
}
exports.login = login;
/**
* Handle the redirects when logging in.
* @param loginResult Response from the login or verifyEmail method.
* @param request
* @param email
* @param returnUrl
* @returns {boolean}
*/
function performLoginRedirect(_a) {
// FIXME: Move this browser functionality to frontend project
var loginResult = _a.loginResult, request = _a.request, email = _a.email, returnUrl = _a.returnUrl;
if (loginResult.error || loginResult.loginFailed) {
return false;
}
switch (loginResult.__resultCode) {
case 'verify':
browserHistory.push(request.contextPath +
'/register/verify?email=' +
(email ? encodeURIComponent(email) : '') +
'&provider=' +
encodeURIComponent(loginResult.provider ? loginResult.provider : AuthenticationType.XCAP));
break;
case 'register':
browserHistory.push(request.contextPath +
'/register/details?email=' +
(loginResult.email ? encodeURIComponent(loginResult.email) : '') +
'&provider=' +
(loginResult.provider ? encodeURIComponent(loginResult.provider) : '') +
(loginResult.firstName ? '&firstName=' + encodeURIComponent(loginResult.firstName) : '') +
(loginResult.lastName ? '&lastName=' + encodeURIComponent(loginResult.lastName) : '') +
(returnUrl ? '&returnUrl=' + encodeURIComponent(returnUrl) : '') +
(loginResult.userReferenceId ? '&userReferenceId=' + encodeURIComponent(loginResult.userReferenceId) : ''));
break;
case 'blocked':
browserHistory.push(request.contextPath + '/user/blocked');
break;
case 'inactive':
//browserHistory.push(request.contextPath + '/user/removed');
return false;
case 'none': // Already logged in
default: {
// Reload desired
var r = typeof returnUrl === 'string' ? returnUrl : request.absoluteCommunityUrl;
if ((0, api_1.isRunningInBrowser)()) {
if (window.location.href === r) {
window.location.reload();
}
else {
window.location = r;
}
}
break;
}
}
return true;
}
exports.performLoginRedirect = performLoginRedirect;
/**
* Send a token to the email address that will allow the user to change password.
* @param email
*/
function sendPasswordChangeToken(_a) {
var email = _a.email;
return (0, api_1.post)({
url: '/user/send-password-change-token',
parameters: arguments
});
}
exports.sendPasswordChangeToken = sendPasswordChangeToken;
/**
* Change password.
*
* This method has two ways of operation:
*
* - When not logged in, supply a code to change the password
* - When logged in, supply the old password
*
* @param email User email adress
* @param checkCode Verification code (optional)
* @param oldPassword Old password as verification (optional)
* @param password The new password
* @param returnUrl The url to return to when the password is changed
*/
function changePassword(_a) {
var email = _a.email, checkCode = _a.checkCode, oldPassword = _a.oldPassword, password = _a.password, returnUrl = _a.returnUrl;
return (0, api_1.post)({
url: '/user/change-password',
parameters: arguments
});
}
exports.changePassword = changePassword;
//# sourceMappingURL=index.js.map