@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
111 lines (109 loc) • 3.97 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { get, getGlobalHeaders, postJSON } from '../utils/ajax';
import { catchError, map, pluck } from 'rxjs/operators';
import { from, of } from 'rxjs';
/**
* @deprecated Please note API deprecation for Crafter v4.0.0+
**/
export function fetchSSOLogoutURL() {
return get('/studio/api/2/users/me/logout/sso/url').pipe(pluck('response'));
}
export function login(credentials) {
// Regular post works fine, but fetch provides the redirect: 'manual' option which cancels the 302
// that's useless for when doing the async style login.
return from(
fetch('/studio/login', {
method: 'POST',
cache: 'no-cache',
credentials: 'include',
headers: Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded' }, getGlobalHeaders()),
redirect: 'manual',
body: `username=${credentials.username}&password=${credentials.password}`
})
).pipe(map(() => true));
}
export function sendPasswordRecovery(username) {
return get(`/studio/api/2/users/forgot_password?username=${username}`).pipe(
pluck('response', 'response'),
catchError((error) => {
var _a, _b;
throw (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.response) !== null && _b !== void 0
? _b
: error;
})
);
}
export function setPassword(token, password, confirmation = password) {
return password !== confirmation
? of('Password and confirmation mismatch').pipe(
map((msg) => {
throw new Error(msg);
})
)
: postJSON(`/studio/api/2/users/set_password`, {
token,
new: password
}).pipe(
map(({ response }) => {
if (response.user == null) {
throw new Error('Expired or incorrect token');
}
return response.user;
})
);
}
export function validatePasswordResetToken(token) {
return get(`/studio/api/2/users/validate_token?token=${token}`).pipe(
map(() => true),
catchError((error) => {
if (error.status === 401) return of(false);
else throw new Error(error.response);
})
);
}
export function obtainAuthToken() {
return get('/studio/refresh.json').pipe(
pluck('response'),
map((auth) => ({ token: auth.token, expiresAt: new Date(auth.expiresAt).getTime() }))
);
}
export function fetchAuthenticationType() {
return get('/studio/authType.json').pipe(
pluck('response', 'authType'),
map((value) => {
var _a;
return (_a = value === null || value === void 0 ? void 0 : value.toLowerCase()) !== null && _a !== void 0
? _a
: 'db';
})
);
}