account.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  2. import {Client} from 'sentry/api';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import type {UserIdentityConfig} from 'sentry/types/auth';
  5. import type {User} from 'sentry/types/user';
  6. import type {ChangeAvatarUser} from 'sentry/views/settings/account/accountDetails';
  7. export async function disconnectIdentity(
  8. identity: UserIdentityConfig,
  9. onSuccess: () => void
  10. ) {
  11. const api = new Client();
  12. try {
  13. await api.requestPromise(
  14. `/users/me/user-identities/${identity.category}/${identity.id}/`,
  15. {
  16. method: 'DELETE',
  17. }
  18. );
  19. } catch {
  20. addErrorMessage('Error disconnecting identity');
  21. return;
  22. }
  23. addSuccessMessage(`Disconnected ${identity.provider.name}`);
  24. onSuccess();
  25. }
  26. export function updateUser(user: User | ChangeAvatarUser) {
  27. const previousUser = ConfigStore.get('user');
  28. // If the user changed their theme preferences, we should also update
  29. // the config store
  30. if (
  31. user.options &&
  32. previousUser.options.theme !== user.options.theme &&
  33. user.options.theme !== 'system'
  34. ) {
  35. ConfigStore.set('theme', user.options.theme);
  36. }
  37. const options = {...previousUser.options, ...user.options};
  38. // We are merging the types because the avatar endpoint ("/users/me/avatar/") doesn't return a full User
  39. ConfigStore.set('user', {...previousUser, ...user, options});
  40. }
  41. export function logout(api: Client) {
  42. return api.requestPromise('/auth/', {method: 'DELETE'});
  43. }
  44. export function removeAuthenticator(api: Client, userId: string, authId: string) {
  45. return api.requestPromise(`/users/${userId}/authenticators/${authId}/`, {
  46. method: 'DELETE',
  47. });
  48. }