Skip to content

Commit e1bbbed

Browse files
committed
Simplify icpStorage.
1 parent 268b3e0 commit e1bbbed

4 files changed

Lines changed: 17 additions & 28 deletions

File tree

popup-app/components/IdpCallback.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,17 @@ import React, { Component } from 'react'
22

33
import auth from '../../src'
44
import { Client } from '../../src/ipc'
5-
import { postMessageStorage } from '../../src/storage'
5+
import { ipcStorage } from '../../src/storage'
66

77
export default class IdpCallback extends Component {
88
state = { loggedIn: false }
99
client = new Client(window.opener, this.props.appOrigin)
1010

11-
postSession = async () => {
12-
const storage = postMessageStorage(window.opener, this.props.appOrigin)
11+
async componentDidMount() {
12+
const storage = ipcStorage(this.client)
1313
const session = await auth.currentSession(storage)
1414
await this.client.request('foundSession', session)
15-
}
16-
17-
constructor(props) {
18-
super(props)
19-
this.postSession().then(() => {
20-
this.setState({ loggedIn: true })
21-
})
22-
}
23-
24-
render() {
25-
return this.state.loggedIn ? <LoggedIn /> : <Loading />
15+
this.setState({ loggedIn: true })
2616
}
2717

2818
componentDidUpdate() {
@@ -31,8 +21,9 @@ export default class IdpCallback extends Component {
3121
afterLoggedIn()
3222
}
3323
}
34-
}
35-
36-
const Loading = () => <h1 class="center">Logging in...</h1>
3724

38-
const LoggedIn = () => <h1 className="center">Logged in!</h1>
25+
render() {
26+
const message = this.state.loggedIn ? 'Logged in!' : 'Logging in…'
27+
return <h1 class="center">{message}</h1>
28+
}
29+
}

popup-app/components/IdpSelect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22

33
import auth from '../../src'
44
import { Client } from '../../src/ipc'
5-
import { postMessageStorage } from '../../src/storage'
5+
import { ipcStorage } from '../../src/storage'
66

77
import './IdpSelect.css'
88

@@ -56,7 +56,7 @@ class IdpSelect extends React.Component {
5656
}
5757
loginOptions = {
5858
...loginOptions,
59-
storage: postMessageStorage(window.opener, appOrigin)
59+
storage: ipcStorage(client)
6060
}
6161
await auth.login(idp.url, loginOptions)
6262
}

src/__test__/storage.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import { polyfillWindow, polyunfillWindow } from './spec-helpers'
44
import type { AsyncStorage } from '../storage'
5-
import { defaultStorage, postMessageStorage } from '../storage'
5+
import { defaultStorage, ipcStorage } from '../storage'
6+
import { Client } from '../ipc'
67

78
beforeEach(polyfillWindow)
89

@@ -21,7 +22,7 @@ describe('defaultStorage', () => {
2122
})
2223
})
2324

24-
describe('postMessage storage', () => {
25+
describe('ipcStorage', () => {
2526
;[
2627
{
2728
expectedMethod: 'getItem',
@@ -64,7 +65,8 @@ describe('postMessage storage', () => {
6465
done.fail(e)
6566
}
6667
})
67-
const store = postMessageStorage(window, window.location.origin)
68+
const client = new Client(window, window.location.origin)
69+
const store = ipcStorage(client)
6870
const item = await store[expectedMethod](...expectedArgs)
6971
expect(item).toBe(expectedRet)
7072
done()

src/storage.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ export const memStorage = (): Storage => {
9393
}
9494
}
9595

96-
export const postMessageStorage = (
97-
storageWindow: window,
98-
storageOrigin: string
99-
): AsyncStorage => {
100-
const client = new Client(storageWindow, storageOrigin)
96+
export function ipcStorage(client: Client): AsyncStorage {
10197
return {
10298
getItem: (key: string): Promise<?string> =>
10399
client.request('storage/getItem', key),

0 commit comments

Comments
 (0)