GoToSocial: Support account security settings

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-07-26 15:56:13 +02:00
parent ebb5f30eac
commit 57efcaf35c
3 changed files with 51 additions and 21 deletions

View file

@ -4,12 +4,12 @@
* @see module:soapbox/actions/auth
*/
import api from 'soapbox/api';
import toast from 'soapbox/toast';
import { getLoggedInAccount } from 'soapbox/utils/auth';
import { GOTOSOCIAL, parseVersion } from 'soapbox/utils/features';
import { normalizeUsername } from 'soapbox/utils/input';
import api from '../api';
import { AUTH_LOGGED_OUT, messages } from './auth';
import type { AppDispatch, RootState } from 'soapbox/store';
@ -69,20 +69,39 @@ const revokeOAuthTokenById = (id: number) =>
const changePassword = (oldPassword: string, newPassword: string, confirmation: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: CHANGE_PASSWORD_REQUEST });
return api(getState)('/api/pleroma/change_password', {
method: 'POST',
body: JSON.stringify({
password: oldPassword,
new_password: newPassword,
new_password_confirmation: confirmation,
}),
}).then(response => {
if (response.json.error) throw response.json.error; // This endpoint returns HTTP 200 even on failure
dispatch({ type: CHANGE_PASSWORD_SUCCESS, response });
}).catch(error => {
dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true });
throw error;
});
const state = getState();
const instance = state.instance;
const v = parseVersion(instance.version);
if (v.software === GOTOSOCIAL) {
return api(getState)('/api/v1/user/password_change', {
method: 'POST',
body: JSON.stringify({
old_password: oldPassword,
new_password: newPassword,
}),
}).then(response => {
dispatch({ type: CHANGE_PASSWORD_SUCCESS, response });
}).catch(error => {
dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true });
throw error;
});
} else {
return api(getState)('/api/pleroma/change_password', {
method: 'POST',
body: JSON.stringify({
password: oldPassword,
new_password: newPassword,
new_password_confirmation: confirmation,
}),
}).then(response => {
if (response.json.error) throw response.json.error; // This endpoint returns HTTP 200 even on failure
dispatch({ type: CHANGE_PASSWORD_SUCCESS, response });
}).catch(error => {
dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true });
throw error;
});
}
};
const resetPassword = (usernameOrEmail: string) =>
@ -110,10 +129,14 @@ const resetPassword = (usernameOrEmail: string) =>
const changeEmail = (email: string, password: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: CHANGE_EMAIL_REQUEST, email });
return api(getState)('/api/pleroma/change_email', {
const state = getState();
const instance = state.instance;
const v = parseVersion(instance.version);
return api(getState)(v.software === GOTOSOCIAL ? '/api/v1/user/email_change' : '/api/pleroma/change_email', {
method: 'POST',
body: JSON.stringify({
email,
[v.software === GOTOSOCIAL ? 'new_email' : 'email']: email,
password,
}),
}).then(response => {
@ -127,10 +150,14 @@ const changeEmail = (email: string, password: string) =>
const deleteAccount = (password: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: CHANGE_PASSWORD_REQUEST });
const account = getLoggedInAccount(getState());
const state = getState();
const instance = state.instance;
const v = parseVersion(instance.version);
dispatch({ type: DELETE_ACCOUNT_REQUEST });
return api(getState)('/api/pleroma/delete_account', {
return api(getState)(v.software === GOTOSOCIAL ? '/api/v1/accounts/delete' : '/api/pleroma/delete_account', {
method: 'POST',
body: JSON.stringify({ password }),
}).then(response => {

View file

@ -1301,7 +1301,7 @@
"security.fields.new_password.label": "Nowe hasło",
"security.fields.old_password.label": "Obecne hasło",
"security.fields.password.label": "Hasło",
"security.fields.password_confirmation.label": "Obecne hasło (ponownie)",
"security.fields.password_confirmation.label": "Nowe hasło (ponownie)",
"security.headers.delete": "Usuń konto",
"security.headers.tokens": "Sesje",
"security.qr.fail": "Nie udało się uzyskać klucza konfiguracyjnego",

View file

@ -877,8 +877,11 @@ const getInstanceFeatures = (instance: Instance) => {
* @see POST /api/pleroma/change_password
* @see POST /api/pleroma/change_email
* @see POST /api/pleroma/delete_account
* @see POST /api/v1/user/email_change
* @see POST /api/v1/user/password_change
* @see POST /api/v1/accounts/delete_account
*/
security: v.software === PLEROMA,
security: any([v.software === PLEROMA, v.software === GOTOSOCIAL]),
/**
* Ability to manage account sessions.