Skip to content

Commit

Permalink
fix: After log back in, register AND login
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Apr 29, 2019
1 parent d3e8baf commit beceea5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
17 changes: 3 additions & 14 deletions packages/cozy-authentication/src/Authentication.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
import Welcome from './steps/Welcome'
import SelectServer from './steps/SelectServer'
import { withClient } from 'cozy-client'
import { registerAndLogin } from './utils/onboarding'

const STEP_WELCOME = 'STEP_WELCOME'
const STEP_EXISTING_SERVER = 'STEP_EXISTING_SERVER'
Expand Down Expand Up @@ -45,20 +46,8 @@ class Authentication extends Component {
try {
this.setState({ generalError: null, fetching: true })
const { client } = this.props
const { client: clientInfo, token } = await cozyClient.stackClient.register(url)

const destructuredToken = {
accessToken: token.accessToken,
refreshToken: token.refreshToken,
scope: token.scope,
tokenType: token.tokenType
}
await client.login({ url, token })
await onComplete({
url,
token: destructuredToken,
clientInfo
})
await registerAndLogin(client, url)
await onComplete()
} catch (err) {
this.setState({ generalError: err })
onException(err, {
Expand Down
4 changes: 2 additions & 2 deletions packages/cozy-authentication/src/MobileRouter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { withClient } from 'cozy-client'
import Authentication from './Authentication'
import Revoked from './Revoked'
import deeplink from './utils/deeplink'
import { doOnboardingLogin } from './utils/onboarding'
import { doOnboardingLogin, registerAndLogin } from './utils/onboarding'

// Even if the component is not yet mounted, we save
// the deeplink
Expand Down Expand Up @@ -137,7 +137,7 @@ export class MobileRouter extends Component {
async handleLogBackIn() {
const { client } = this.props
await client.stackClient.unregister().catch(() => {})
await client.stackClient.register(client.uri)
await registerAndLogin(client, client.stackClient.uri)
}

async afterAuthentication() {
Expand Down
7 changes: 7 additions & 0 deletions packages/cozy-authentication/src/utils/onboarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const addProtocolToDomain = domain => {
return `${protocol}://${domain}`
}

// Should be in cozy-client
export const doOnboardingLogin = async (
client,
domain,
Expand Down Expand Up @@ -138,3 +139,9 @@ export const doOnboardingLogin = async (
throw e
}
}

// Should be in cozy-client
export const registerAndLogin = async (client, url) => {
const { token } = await client.register(url)
await client.login({ url, token })
}

0 comments on commit beceea5

Please sign in to comment.