From f42c9149fba22444386b12ece25cca5bf8f4510a Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Mon, 5 Jul 2021 14:21:15 -0400 Subject: [PATCH] [FIX] Google OAuth triggering cookies logic (#3244) * Remove checkCookiesAndLogout * Add loginEmailPassword to loginOAuthOrSso * Add isFromWebView field * Fix migrations * Minor tweak * Fix OAuth for other services * Fix migrations * Stop persisting loginEmailPassword Co-authored-by: Diego Mello --- app/actions/login.js | 5 +++-- app/lib/database/model/servers/User.js | 4 ++-- app/lib/database/model/servers/migrations.js | 10 ++++++++++ app/lib/database/schema/servers.js | 5 +++-- app/lib/rocketchat.js | 16 ++++++++-------- app/sagas/deepLinking.js | 2 +- app/sagas/login.js | 10 ++++------ app/views/SettingsView/index.js | 2 +- 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/app/actions/login.js b/app/actions/login.js index daf13d3b4..e0f4c1e28 100644 --- a/app/actions/login.js +++ b/app/actions/login.js @@ -1,10 +1,11 @@ import * as types from './actionsTypes'; -export function loginRequest(credentials, logoutOnError) { +export function loginRequest(credentials, logoutOnError, isFromWebView) { return { type: types.LOGIN.REQUEST, credentials, - logoutOnError + logoutOnError, + isFromWebView }; } diff --git a/app/lib/database/model/servers/User.js b/app/lib/database/model/servers/User.js index 5b4d3d65a..6d78c27fa 100644 --- a/app/lib/database/model/servers/User.js +++ b/app/lib/database/model/servers/User.js @@ -22,7 +22,7 @@ export default class User extends Model { @field('avatar_etag') avatarETag; - @field('login_email_password') loginEmailPassword; - @field('show_message_in_main_thread') showMessageInMainThread; + + @field('is_from_webview') isFromWebView; } diff --git a/app/lib/database/model/servers/migrations.js b/app/lib/database/model/servers/migrations.js index 782700a37..80954d475 100644 --- a/app/lib/database/model/servers/migrations.js +++ b/app/lib/database/model/servers/migrations.js @@ -95,6 +95,16 @@ export default schemaMigrations({ ] }) ] + }, { + toVersion: 11, + steps: [ + addColumns({ + table: 'users', + columns: [ + { name: 'is_from_webview', type: 'boolean', isOptional: true } + ] + }) + ] } ] }); diff --git a/app/lib/database/schema/servers.js b/app/lib/database/schema/servers.js index 0c81a72fc..1105cf165 100644 --- a/app/lib/database/schema/servers.js +++ b/app/lib/database/schema/servers.js @@ -1,7 +1,7 @@ import { appSchema, tableSchema } from '@nozbe/watermelondb'; export default appSchema({ - version: 10, + version: 11, tables: [ tableSchema({ name: 'users', @@ -15,7 +15,8 @@ export default appSchema({ { name: 'roles', type: 'string', isOptional: true }, { name: 'login_email_password', type: 'boolean', isOptional: true }, { name: 'show_message_in_main_thread', type: 'boolean', isOptional: true }, - { name: 'avatar_etag', type: 'string', isOptional: true } + { name: 'avatar_etag', type: 'string', isOptional: true }, + { name: 'is_from_webview', type: 'boolean', isOptional: true } ] }), tableSchema({ diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index dcb3358cb..1cc0d4e0f 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -526,10 +526,10 @@ const RocketChat = { return this.post('users.forgotPassword', { email }, false); }, - loginTOTP(params, loginEmailPassword) { + loginTOTP(params, loginEmailPassword, isFromWebView = false) { return new Promise(async(resolve, reject) => { try { - const result = await this.login(params, loginEmailPassword); + const result = await this.login(params, isFromWebView); return resolve(result); } catch (e) { if (e.data?.error && (e.data.error === 'totp-required' || e.data.error === 'totp-invalid')) { @@ -592,15 +592,15 @@ const RocketChat = { return this.loginTOTP(params, true); }, - async loginOAuthOrSso(params) { - const result = await this.loginTOTP(params); - reduxStore.dispatch(loginRequest({ resume: result.token })); + async loginOAuthOrSso(params, isFromWebView = true) { + const result = await this.loginTOTP(params, false, isFromWebView); + reduxStore.dispatch(loginRequest({ resume: result.token }, false, isFromWebView)); }, - async login(params, loginEmailPassword) { + async login(credentials, isFromWebView = false) { const sdk = this.shareSDK || this.sdk; // RC 0.64.0 - await sdk.login(params); + await sdk.login(credentials); const { result } = sdk.currentLogin; const user = { id: result.userId, @@ -615,7 +615,7 @@ const RocketChat = { emails: result.me.emails, roles: result.me.roles, avatarETag: result.me.avatarETag, - loginEmailPassword, + isFromWebView, showMessageInMainThread: result.me.settings?.preferences?.showMessageInMainThread ?? true }; return user; diff --git a/app/sagas/deepLinking.js b/app/sagas/deepLinking.js index 556573a54..a406f56a9 100644 --- a/app/sagas/deepLinking.js +++ b/app/sagas/deepLinking.js @@ -97,7 +97,7 @@ const fallbackNavigation = function* fallbackNavigation() { const handleOAuth = function* handleOAuth({ params }) { const { credentialToken, credentialSecret } = params; try { - yield RocketChat.loginOAuthOrSso({ oauth: { credentialToken, credentialSecret } }); + yield RocketChat.loginOAuthOrSso({ oauth: { credentialToken, credentialSecret } }, false); } catch (e) { log(e); } diff --git a/app/sagas/login.js b/app/sagas/login.js index de82cfb7e..83eb04dd6 100644 --- a/app/sagas/login.js +++ b/app/sagas/login.js @@ -30,15 +30,15 @@ import Navigation from '../lib/Navigation'; const getServer = state => state.server.server; const loginWithPasswordCall = args => RocketChat.loginWithPassword(args); -const loginCall = args => RocketChat.login(args); +const loginCall = (credentials, isFromWebView) => RocketChat.login(credentials, isFromWebView); const logoutCall = args => RocketChat.logout(args); -const handleLoginRequest = function* handleLoginRequest({ credentials, logoutOnError = false }) { +const handleLoginRequest = function* handleLoginRequest({ credentials, logoutOnError = false, isFromWebView = false }) { logEvent(events.LOGIN_DEFAULT_LOGIN); try { let result; if (credentials.resume) { - result = yield call(loginCall, credentials); + result = yield loginCall(credentials, isFromWebView); } else { result = yield call(loginWithPasswordCall, credentials); } @@ -68,7 +68,6 @@ const handleLoginRequest = function* handleLoginRequest({ credentials, logoutOnE log(e); } }); - yield put(loginSuccess(result)); } } catch (e) { @@ -148,14 +147,13 @@ const handleLoginSuccess = function* handleLoginSuccess({ user }) { status: user.status, statusText: user.statusText, roles: user.roles, - loginEmailPassword: user.loginEmailPassword, + isFromWebView: user.isFromWebView, showMessageInMainThread: user.showMessageInMainThread, avatarETag: user.avatarETag }; yield serversDB.action(async() => { try { const userRecord = await usersCollection.find(user.id); - u.loginEmailPassword = userRecord?.loginEmailPassword; await userRecord.update((record) => { record._raw = sanitizedRaw({ id: user.id, ...record._raw }, usersCollection.schema); Object.assign(record, u); diff --git a/app/views/SettingsView/index.js b/app/views/SettingsView/index.js index 55687cf0f..5e696144b 100644 --- a/app/views/SettingsView/index.js +++ b/app/views/SettingsView/index.js @@ -65,7 +65,7 @@ class SettingsView extends React.Component { const usersCollection = db.get('users'); try { const userRecord = await usersCollection.find(user.id); - if (!userRecord.loginEmailPassword) { + if (userRecord.isFromWebView) { showConfirmationAlert({ title: I18n.t('Clear_cookies_alert'), message: I18n.t('Clear_cookies_desc'),