Skip to content

Commit

Permalink
fix: Skip client creation for PWA supported browsers & core ClientType (
Browse files Browse the repository at this point in the history
  • Loading branch information
Yserz committed Jun 5, 2018
1 parent f53faf2 commit 287e9ff
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 65 deletions.
44 changes: 24 additions & 20 deletions app/script/auth/module/action/AuthAction.js
Expand Up @@ -52,8 +52,8 @@ function doLoginPlain(loginData, onBeforeLogin, onAfterLogin) {

return Promise.resolve()
.then(() => onBeforeLogin(dispatch, getState, global))
.then(() => core.login(loginData, false, ClientAction.generateClientPayload(loginData.persist)))
.then(() => persistAuthData(loginData.persist, core, dispatch))
.then(() => core.login(loginData, false, ClientAction.generateClientPayload(loginData.clientType)))
.then(() => persistAuthData(loginData.clientType, core, dispatch))
.then(() => dispatch(CookieAction.setCookie(COOKIE_NAME_APP_OPENED, {appInstanceId: APP_INSTANCE_ID})))
.then(() => {
const authenticationContext = loginData.email
Expand All @@ -68,7 +68,7 @@ function doLoginPlain(loginData, onBeforeLogin, onAfterLogin) {
})
.then(() => dispatch(SelfAction.fetchSelf()))
.then(() => onAfterLogin(dispatch, getState, global))
.then(() => dispatch(ClientAction.doInitializeClient(loginData.persist, loginData.password)))
.then(() => dispatch(ClientAction.doInitializeClient(loginData.clientType, loginData.password)))
.then(() => dispatch(AuthActionCreator.successfulLogin()))
.catch(error => {
if (error.label === BackendError.LABEL.NEW_CLIENT || error.label === BackendError.LABEL.TOO_MANY_CLIENTS) {
Expand All @@ -81,17 +81,21 @@ function doLoginPlain(loginData, onBeforeLogin, onAfterLogin) {
};
}

function persistAuthData(persist, core, dispatch) {
function persistAuthData(clientType, core, dispatch) {
const persist = clientType === ClientType.PERMANENT;
const accessToken = core.apiClient.accessTokenStore.accessToken;
const expiresMillis = accessToken.expires_in * 1000;
const expireTimestamp = Date.now() + expiresMillis;
return Promise.all([
dispatch(setLocalStorage(LocalStorageKey.AUTH.PERSIST, persist)),
const saveTasks = [
dispatch(setLocalStorage(LocalStorageKey.AUTH.ACCESS_TOKEN.EXPIRATION, expireTimestamp)),
dispatch(setLocalStorage(LocalStorageKey.AUTH.ACCESS_TOKEN.TTL, expiresMillis)),
dispatch(setLocalStorage(LocalStorageKey.AUTH.ACCESS_TOKEN.TYPE, accessToken.token_type)),
dispatch(setLocalStorage(LocalStorageKey.AUTH.ACCESS_TOKEN.VALUE, accessToken.access_token)),
]);
];
if (clientType !== ClientType.NONE) {
saveTasks.push(dispatch(setLocalStorage(LocalStorageKey.AUTH.PERSIST, persist)));
}
return Promise.all(saveTasks);
}

export function pushAccountRegistrationData(registration) {
Expand All @@ -102,7 +106,7 @@ export function pushAccountRegistrationData(registration) {

export function doRegisterTeam(registration) {
return function(dispatch, getState, {apiClient, core}) {
const isPermanentClient = true;
const clientType = ClientType.PERMANENT;
registration.locale = currentLanguage();
registration.name = registration.name.trim();
registration.email = registration.email.trim();
Expand All @@ -115,13 +119,13 @@ export function doRegisterTeam(registration) {
dispatch(AuthActionCreator.startRegisterTeam({...registration, password: '******'}));
return Promise.resolve()
.then(() => dispatch(doSilentLogout()))
.then(() => apiClient.register(registration, isPermanentClient))
.then(() => apiClient.register(registration, clientType))
.then(newAccount => (createdAccount = newAccount))
.then(() => core.init())
.then(() => persistAuthData(isPermanentClient, core, dispatch))
.then(() => persistAuthData(clientType, core, dispatch))
.then(() => dispatch(CookieAction.setCookie(COOKIE_NAME_APP_OPENED, {appInstanceId: APP_INSTANCE_ID})))
.then(() => dispatch(SelfAction.fetchSelf()))
.then(() => dispatch(ClientAction.doInitializeClient(isPermanentClient)))
.then(() => dispatch(ClientAction.doInitializeClient(clientType)))
.then(() => dispatch(AuthActionCreator.successfulRegisterTeam(createdAccount)))
.catch(error => {
if (error.label === BackendError.LABEL.NEW_CLIENT) {
Expand All @@ -136,7 +140,7 @@ export function doRegisterTeam(registration) {

export function doRegisterPersonal(registration) {
return function(dispatch, getState, {apiClient, core}) {
const isPermanentClient = true;
const clientType = ClientType.PERMANENT;
registration.locale = currentLanguage();
registration.name = registration.name.trim();
registration.email = registration.email.trim();
Expand All @@ -153,13 +157,13 @@ export function doRegisterPersonal(registration) {
);
return Promise.resolve()
.then(() => dispatch(doSilentLogout()))
.then(() => apiClient.register(registration, isPermanentClient))
.then(() => apiClient.register(registration, clientType))
.then(newAccount => (createdAccount = newAccount))
.then(() => core.init())
.then(() => persistAuthData(isPermanentClient, core, dispatch))
.then(() => persistAuthData(clientType, core, dispatch))
.then(() => dispatch(CookieAction.setCookie(COOKIE_NAME_APP_OPENED, {appInstanceId: APP_INSTANCE_ID})))
.then(() => dispatch(SelfAction.fetchSelf()))
.then(() => dispatch(ClientAction.doInitializeClient(isPermanentClient)))
.then(() => dispatch(ClientAction.doInitializeClient(clientType)))
.then(() => dispatch(AuthActionCreator.successfulRegisterPersonal(createdAccount)))
.catch(error => {
if (error.label === BackendError.LABEL.NEW_CLIENT) {
Expand All @@ -172,9 +176,9 @@ export function doRegisterPersonal(registration) {
};
}

export function doRegisterWireless(registrationData) {
export function doRegisterWireless(registrationData, options = {shouldInitializeClient: true}) {
return function(dispatch, getState, {apiClient, core}) {
const isPermanentClient = false;
const clientType = options.shouldInitializeClient ? ClientType.TEMPORARY : ClientType.NONE;
registrationData.locale = currentLanguage();
registrationData.name = registrationData.name.trim();

Expand All @@ -188,13 +192,13 @@ export function doRegisterWireless(registrationData) {
dispatch(AuthActionCreator.startRegisterWireless(obfuscatedRegistrationData));

return Promise.resolve()
.then(() => apiClient.register(registrationData, isPermanentClient))
.then(() => apiClient.register(registrationData, clientType))
.then(newAccount => (createdAccount = newAccount))
.then(() => core.init())
.then(() => persistAuthData(isPermanentClient, core, dispatch))
.then(() => persistAuthData(clientType, core, dispatch))
.then(() => dispatch(CookieAction.setCookie(COOKIE_NAME_APP_OPENED, {appInstanceId: APP_INSTANCE_ID})))
.then(() => dispatch(SelfAction.fetchSelf()))
.then(() => dispatch(ClientAction.doInitializeClient(isPermanentClient)))
.then(() => clientType !== ClientType.NONE && dispatch(ClientAction.doInitializeClient(clientType)))
.then(() => dispatch(AuthActionCreator.successfulRegisterWireless(createdAccount)))
.catch(error => {
if (error.label === BackendError.LABEL.NEW_CLIENT) {
Expand Down
12 changes: 8 additions & 4 deletions app/script/auth/module/action/ClientAction.js
Expand Up @@ -23,6 +23,7 @@ import * as Runtime from '../../Runtime';
import * as Environment from '../../Environment';
import * as StringUtil from '../../util/stringUtil';
import * as NotificationAction from './NotificationAction';
import {ClientType} from '@wireapp/api-client/dist/commonjs/client/index';

export function doGetAllClients() {
return function(dispatch, getState, {apiClient}) {
Expand Down Expand Up @@ -53,11 +54,11 @@ export function doRemoveClient(clientId, password) {
};
}

export function doInitializeClient(persist, password) {
export function doInitializeClient(clientType, password) {
return function(dispatch, getState, {core}) {
dispatch(ClientActionCreator.startInitializeClient());
return Promise.resolve()
.then(() => core.initClient({password, persist}, generateClientPayload(persist)))
.then(() => core.initClient({clientType, password}, generateClientPayload(clientType)))
.then(creationStatus =>
Promise.resolve()
.then(() => dispatch(ClientActionCreator.successfulInitializeClient(creationStatus)))
Expand All @@ -77,7 +78,10 @@ export function doInitializeClient(persist, password) {
};
}

export function generateClientPayload(persist) {
export function generateClientPayload(clientType: ClientType) {
if (clientType === ClientType.NONE) {
return undefined;
}
const deviceLabel = `${Runtime.getOsFamily()}${Runtime.getOs().version ? ` ${Runtime.getOs().version}` : ''}`;
let deviceModel = StringUtil.capitalize(Runtime.getBrowserName());

Expand All @@ -92,7 +96,7 @@ export function generateClientPayload(persist) {
if (!Environment.isEnvironment(Environment.PRODUCTION)) {
deviceModel = `${deviceModel} (Internal)`;
}
} else if (!persist) {
} else if (clientType === ClientType.TEMPORARY) {
deviceModel = `${deviceModel} (Temporary)`;
}

Expand Down
17 changes: 12 additions & 5 deletions app/script/auth/page/ConversationJoin.jsx
Expand Up @@ -50,7 +50,7 @@ import {ROUTE, QUERY_KEY} from '../route';
import {injectIntl, FormattedHTMLMessage} from 'react-intl';
import {withRouter} from 'react-router';
import React, {Component} from 'react';
import {getURLParameter, getAppPath, hasURLParameter} from '../util/urlUtil';
import {getURLParameter, getAppPath, hasURLParameter, pathWithParams} from '../util/urlUtil';
import BackendError from '../module/action/BackendError';
import AppAlreadyOpen from '../component/AppAlreadyOpen';
import WirelessUnsupportedBrowser from '../component/WirelessUnsupportedBrowser';
Expand Down Expand Up @@ -121,13 +121,18 @@ class ConversationJoin extends Component {
.then(() => this.routeToApp());
};

routeToApp = () => {
isPwaSupportedBrowser = () => {
const pwaAware = hasURLParameter(QUERY_KEY.PWA_AWARE);
const isPwaSupportedBrowser = Environment.onEnvironment({
return Environment.onEnvironment({
onProduction: false,
onStaging: pwaAware && (isMobileOs() || isSafari()),
});
const redirectLocation = isPwaSupportedBrowser ? EXTERNAL_ROUTE.PWA : getAppPath();
};

routeToApp = () => {
const redirectLocation = this.isPwaSupportedBrowser()
? pathWithParams(EXTERNAL_ROUTE.PWA, QUERY_KEY.IMMEDIATE_LOGIN)
: getAppPath();
window.location.replace(redirectLocation);
};

Expand All @@ -148,7 +153,9 @@ class ConversationJoin extends Component {
expires_in: this.state.expiresIn,
name,
};
return this.props.doRegisterWireless(registrationData);
return this.props.doRegisterWireless(registrationData, {
shouldInitializeClient: !this.isPwaSupportedBrowser(),
});
})
.then(() => this.props.doJoinConversationByCode(this.state.conversationKey, this.state.conversationCode))
.then(conversationEvent => this.props.setLastEventDate(new Date(conversationEvent.time)))
Expand Down
3 changes: 2 additions & 1 deletion app/script/auth/page/Login.js
Expand Up @@ -60,6 +60,7 @@ import * as URLUtil from '../util/urlUtil';
import * as ClientSelector from '../module/selector/ClientSelector';
import {resetError} from '../module/action/creator/AuthActionCreator';
import Page from './Page';
import {ClientType} from '@wireapp/api-client/dist/commonjs/client/index';

class Login extends React.PureComponent {
inputs = {};
Expand Down Expand Up @@ -170,7 +171,7 @@ class Login extends React.PureComponent {
})
.then(() => {
const {email, password, persist} = this.state;
const login = {password, persist};
const login = {clientType: persist ? ClientType.PERMANENT : ClientType.TEMPORARY, password};

if (this.isValidEmail(email)) {
login.email = email;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"@bower_components/webrtc-adapter": "webrtc/adapter#6.2.1",
"@bower_components/wire-audio-files": "wireapp/wire-audio-files#1.1.1",
"@bower_components/wire-theme": "wireapp/wire-theme#1.0.6",
"@wireapp/core": "2.9.6",
"@wireapp/core": "3.0.2",
"@wireapp/react-ui-kit": "1.1.33",
"babel-plugin-react-intl": "2.4.0",
"babel-polyfill": "6.26.0",
Expand Down
68 changes: 34 additions & 34 deletions yarn.lock
Expand Up @@ -495,71 +495,71 @@
"@webassemblyjs/wast-parser" "1.5.9"
long "^3.2.0"

"@wireapp/api-client@0.10.11":
version "0.10.11"
resolved "https://registry.yarnpkg.com/@wireapp/api-client/-/api-client-0.10.11.tgz#4351192691a0e3b84be85af4e07bfb9053568b1c"
"@wireapp/api-client@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@wireapp/api-client/-/api-client-1.1.0.tgz#f4543566b625bc59f46be30d1e17226163910a66"
dependencies:
"@types/node" "10.3.0"
"@types/spark-md5" "3.0.0"
"@types/tough-cookie" "2.3.3"
"@wireapp/priority-queue" "0.1.36"
"@wireapp/store-engine" "0.11.21"
"@wireapp/priority-queue" "0.1.37"
"@wireapp/store-engine" "0.11.22"
axios "0.18.0"
html5-websocket "2.0.3"
logdown "3.2.3"
reconnecting-websocket "3.2.2"
spark-md5 "3.0.0"
tough-cookie "2.3.4"

"@wireapp/cbor@3.0.38":
version "3.0.38"
resolved "https://registry.yarnpkg.com/@wireapp/cbor/-/cbor-3.0.38.tgz#4b94578a49eeac2555b42d25ab02d220da68a35a"
"@wireapp/cbor@3.0.39":
version "3.0.39"
resolved "https://registry.yarnpkg.com/@wireapp/cbor/-/cbor-3.0.39.tgz#9431292deb6f5c4a703d927e0fe0f42bc9199449"

"@wireapp/core@2.9.6":
version "2.9.6"
resolved "https://registry.yarnpkg.com/@wireapp/core/-/core-2.9.6.tgz#02154f2eeeca0d432d177b06c04dc3c8c174f7ae"
"@wireapp/core@3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@wireapp/core/-/core-3.0.2.tgz#7dcea280dfc31c47f576ce50df17c6b9c694f1a4"
dependencies:
"@types/node" "10.3.0"
"@wireapp/api-client" "0.10.11"
"@wireapp/cryptobox" "8.3.65"
"@wireapp/store-engine" "0.11.21"
"@wireapp/api-client" "1.1.0"
"@wireapp/cryptobox" "8.3.66"
"@wireapp/store-engine" "0.11.22"
bazinga64 "5.1.16"
logdown "3.2.3"
protobufjs "6.8.6"
pure-uuid "1.5.2"

"@wireapp/cryptobox@8.3.65":
version "8.3.65"
resolved "https://registry.yarnpkg.com/@wireapp/cryptobox/-/cryptobox-8.3.65.tgz#ebff47ff68e189844309ae41f3fb20c7cd9b6bc3"
"@wireapp/cryptobox@8.3.66":
version "8.3.66"
resolved "https://registry.yarnpkg.com/@wireapp/cryptobox/-/cryptobox-8.3.66.tgz#c4dcc41d4b528c4143fcff83a68f044a2d33fb05"
dependencies:
"@types/fs-extra" "5.0.2"
"@types/node" "10.3.0"
"@wireapp/lru-cache" "2.1.18"
"@wireapp/priority-queue" "0.1.36"
"@wireapp/proteus" "7.1.50"
"@wireapp/store-engine" "0.11.21"
"@wireapp/lru-cache" "2.1.19"
"@wireapp/priority-queue" "0.1.37"
"@wireapp/proteus" "7.1.51"
"@wireapp/store-engine" "0.11.22"
dexie "2.0.4"
fs-extra "6.0.1"

"@wireapp/lru-cache@2.1.18":
version "2.1.18"
resolved "https://registry.yarnpkg.com/@wireapp/lru-cache/-/lru-cache-2.1.18.tgz#256d42e78c950173794f2313284beaadae583653"
"@wireapp/lru-cache@2.1.19":
version "2.1.19"
resolved "https://registry.yarnpkg.com/@wireapp/lru-cache/-/lru-cache-2.1.19.tgz#b110bf95c403bd48115301710a44a4bf013d1109"

"@wireapp/priority-queue@0.1.36":
version "0.1.36"
resolved "https://registry.yarnpkg.com/@wireapp/priority-queue/-/priority-queue-0.1.36.tgz#c1cd7fb428d1622b4725576955d1dfd57dc9a573"
"@wireapp/priority-queue@0.1.37":
version "0.1.37"
resolved "https://registry.yarnpkg.com/@wireapp/priority-queue/-/priority-queue-0.1.37.tgz#d663d26875535c984e938c4a439346ab744ec5e0"
dependencies:
"@types/node" "10.3.0"
pure-uuid "1.5.2"

"@wireapp/proteus@7.1.50":
version "7.1.50"
resolved "https://registry.yarnpkg.com/@wireapp/proteus/-/proteus-7.1.50.tgz#fd3627028b53cfefe4931e187b586ca313ad51a9"
"@wireapp/proteus@7.1.51":
version "7.1.51"
resolved "https://registry.yarnpkg.com/@wireapp/proteus/-/proteus-7.1.51.tgz#b86308989f5135fb9d6f96fd0b5c1cd8e97fc076"
dependencies:
"@types/chai" "4.1.3"
"@types/ed2curve" "0.2.2"
"@types/node" "10.3.0"
"@wireapp/cbor" "3.0.38"
"@wireapp/cbor" "3.0.39"
ed2curve "0.2.1"
libsodium-wrappers-sumo "0.7.3"

Expand All @@ -575,9 +575,9 @@
styled-components "3.3.2"
styled-normalize "4.0.0"

"@wireapp/store-engine@0.11.21":
version "0.11.21"
resolved "https://registry.yarnpkg.com/@wireapp/store-engine/-/store-engine-0.11.21.tgz#5321034c66a47751af16c6d131e00eb11fcbdcb3"
"@wireapp/store-engine@0.11.22":
version "0.11.22"
resolved "https://registry.yarnpkg.com/@wireapp/store-engine/-/store-engine-0.11.22.tgz#5a0026dca94827914e7a686bb1447226c3cc8a11"
dependencies:
"@types/filesystem" "0.0.28"
"@types/node" "10.3.0"
Expand Down

0 comments on commit 287e9ff

Please sign in to comment.