From a0dddae28935c7f8f405e5672356bf4f639869bb Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 30 Sep 2024 21:48:52 +0530 Subject: [PATCH] feat: let users re auth legacy google inboxes (#10179) This PR allows migration of legacy GMail inbox users to new OAuth based inbox ## How to test? 1. Create an inbox from the seed data and set it's IMAP address to `imap.gmail.com` from the UI 2. Open `rails console` and run the following ``` inbox = Inbox.find(100) # use your inbox id here channel = inbox.channel channel.update(email: 'hello@chatwoot.com') channel.prompt_reauthorization! ``` 3. This will show the prompt on the UI. Once you click on Reauthorize, it should open Google Auth. Reauthroize with the same email address as used in the inbox and it should start working as usual ### Setting up ENV ```sh GOOGLE_OAUTH_CLIENT_ID=.apps.googleusercontent.com GOOGLE_OAUTH_CLIENT_SECRET= GOOGLE_OAUTH_CALLBACK_URL="http://localhost:3000/omniauth/google_oauth2/callback" ``` --- .../dashboard/settings/inbox/Settings.vue | 16 ++++++- .../inbox/channels/google/Reauthorize.vue | 44 +++++++++++++++++++ app/views/api/v1/models/_inbox.json.jbuilder | 6 +-- 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 app/javascript/dashboard/routes/dashboard/settings/inbox/channels/google/Reauthorize.vue diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue index 09ae9add9..0fa76069f 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue @@ -7,12 +7,13 @@ import SettingIntroBanner from 'dashboard/components/widgets/SettingIntroBanner. import SettingsSection from '../../../../components/SettingsSection.vue'; import inboxMixin from 'shared/mixins/inboxMixin'; import FacebookReauthorize from './facebook/Reauthorize.vue'; +import MicrosoftReauthorize from './channels/microsoft/Reauthorize.vue'; +import GoogleReauthorize from './channels/google/Reauthorize.vue'; import PreChatFormSettings from './PreChatForm/Settings.vue'; import WeeklyAvailability from './components/WeeklyAvailability.vue'; import GreetingsEditor from 'shared/components/GreetingsEditor.vue'; import ConfigurationPage from './settingsPage/ConfigurationPage.vue'; import CollaboratorsPage from './settingsPage/CollaboratorsPage.vue'; -import MicrosoftReauthorize from './channels/microsoft/Reauthorize.vue'; import WidgetBuilder from './WidgetBuilder.vue'; import BotConfiguration from './components/BotConfiguration.vue'; import { FEATURE_FLAGS } from '../../../../featureFlags'; @@ -32,6 +33,7 @@ export default { WidgetBuilder, SenderNameExamplePreview, MicrosoftReauthorize, + GoogleReauthorize, }, mixins: [inboxMixin], setup() { @@ -203,6 +205,16 @@ export default { facebookUnauthorized() { return this.isAFacebookInbox && this.inbox.reauthorization_required; }, + googleUnauthorized() { + const isLegacyInbox = ['imap.gmail.com', 'imap.google.com'].includes( + this.inbox.imap_address + ); + + return ( + (this.isAGoogleInbox || isLegacyInbox) && + this.inbox.reauthorization_required + ); + }, }, watch: { $route(to) { @@ -367,9 +379,9 @@ export default { /> - +
+import { ref } from 'vue'; +import InboxReconnectionRequired from '../../components/InboxReconnectionRequired'; +import googleClient from 'dashboard/api/channel/googleClient'; + +import { useI18n } from 'dashboard/composables/useI18n'; +import { useAlert } from 'dashboard/composables'; + +const props = defineProps({ + inbox: { + type: Object, + default: () => ({}), + }, +}); + +const { t } = useI18n(); + +const isRequestingAuthorization = ref(false); + +async function requestAuthorization() { + try { + isRequestingAuthorization.value = true; + const response = await googleClient.generateAuthorization({ + email: props.inbox.email, + }); + + const { + data: { url }, + } = response; + window.location.href = url; + } catch (error) { + useAlert(t('INBOX_MGMT.ADD.GOOGLE.ERROR_MESSAGE')); + } finally { + isRequestingAuthorization.value = false; + } +} + + + diff --git a/app/views/api/v1/models/_inbox.json.jbuilder b/app/views/api/v1/models/_inbox.json.jbuilder index 88a029680..c49564e07 100644 --- a/app/views/api/v1/models/_inbox.json.jbuilder +++ b/app/views/api/v1/models/_inbox.json.jbuilder @@ -72,10 +72,8 @@ if resource.email? json.imap_port resource.channel.try(:imap_port) json.imap_enabled resource.channel.try(:imap_enabled) json.imap_enable_ssl resource.channel.try(:imap_enable_ssl) - - if resource.channel.try(:microsoft?) || resource.channel.try(:google?) - json.reauthorization_required resource.channel.try(:provider_config).empty? || resource.channel.try(:reauthorization_required?) - end + # show this even for regular imap channels, to allow transitioning to OAuth + json.reauthorization_required resource.channel.try(:provider_config).empty? || resource.channel.try(:reauthorization_required?) end ## SMTP