Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0.0 #38

Merged
merged 9 commits into from
Feb 2, 2017
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Modern JavaScript applications take a lot of bootstrapping. This library helps w
* [fetch](#fetch)
* [fetchMultiple](#fetchmultiple)
* [html](#html)
* [mount](#mount)
* [Install](#install)
* [See Also](#see-also)
* [License](#license)
Expand All @@ -42,7 +41,7 @@ mount({
})
```

This will create a redux store with the `fetch`, `history`, `logger`, `multi`, and `promise` middleware applied, wrap your application with a redux provider, initialize the browser history, and mount your component to `#id`.
This will create a redux store with the `fetch`, `history`, `logger`, `multi`, and `promise` middleware applied, wrap your application with a redux provider, initialize the browser history, and mount your component to `#id`. The component passed as `app` will be passed `history` from [react-router-redux](https://github.com/reactjs/react-router-redux) and the initialized redux `store` as props.

## API

Expand Down Expand Up @@ -144,9 +143,7 @@ store.dispatch(fetchMultiple({

`html({title})`

### mount

`mount({app, id, reducers})`
Used for creating the default HTML needed to use a woonerf application.

## Install

Expand Down
12 changes: 0 additions & 12 deletions __mocks__/auth0-js.js

This file was deleted.

71 changes: 0 additions & 71 deletions __tests__/src/__snapshots__/auth0.js.snap
Original file line number Diff line number Diff line change
@@ -1,74 +1,3 @@
exports[`auth0 refreshUser should throw error when no credentials supplied 1`] = `"Auth0 credentials not found!"`;

exports[`auth0 refreshUser should work when no user is present in localStorage 1`] = `
Array [
Array [
Object {
"payload": Object {
"args": Array [
"/login",
],
"method": "push",
},
"type": "@@router/CALL_HISTORY_METHOD",
},
],
]
`;

exports[`auth0 refreshUser when a user is present in localStorage should handle ok token 1`] = `
Array [
Array [
Object {
"payload": Object {
"idToken": "fake-token",
"refreshToken": "ok-token",
},
"type": "set auth0 user",
},
],
Array [
Object {
"payload": Object {
"idToken": "fake-token",
"refreshToken": "ok-token",
},
"type": "set auth0 user",
},
],
]
`;

exports[`auth0 refreshUser when a user is present in localStorage should handle token that produces Auth0 error 1`] = `
Array [
Array [
Object {
"payload": Object {
"refreshToken": "token-that-produces-error",
},
"type": "set auth0 user",
},
],
Array [
Object {
"payload": null,
"type": "set auth0 user",
},
],
Array [
Object {
"payload": Object {
"args": Array [
"/login",
],
"method": "push",
},
"type": "@@router/CALL_HISTORY_METHOD",
},
],
]
`;

exports[`auth0 setAuth0User should work 1`] = `
Object {
"payload": Object {
Expand Down
9 changes: 9 additions & 0 deletions __tests__/src/__snapshots__/mount.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ exports[`mount should mount the react application to root 1`] = `
"transitionTo": [Function],
"unsubscribe": [Function],
}
}
store={
Object {
"dispatch": [Function],
"getState": [Function],
"replaceReducer": [Function],
"subscribe": [Function],
Symbol(observable): [Function],
}
}>
<div />
</App>
Expand Down
46 changes: 2 additions & 44 deletions __tests__/src/auth0.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,12 @@
/* globals describe, expect, it, jest */
/* globals describe, expect, it */

import {getLock, refreshUser, setAuth0User} from '../../src/auth0'
import {getLock, setAuth0User} from '../../src/auth0'

describe('auth0', () => {
it('getLock should work', () => {
getLock()
})

describe('refreshUser', () => {
it('should work when no user is present in localStorage', () => {
window.localStorage = {
getItem: () => null
}
const dispatch = jest.fn()
refreshUser(dispatch, true)
expect(dispatch.mock.calls).toMatchSnapshot()
})

it('should throw error when no credentials supplied', () => {
const dispatch = jest.fn()
expect(() => refreshUser(dispatch)).toThrowErrorMatchingSnapshot()
})

describe('when a user is present in localStorage', () => {
it('should handle token that produces Auth0 error', () => {
window.localStorage = {
getItem: () => JSON.stringify({
refreshToken: 'token-that-produces-error'
}),
setItem: () => null
}
const dispatch = jest.fn()
refreshUser(dispatch, true)
expect(dispatch.mock.calls).toMatchSnapshot()
})

it('should handle ok token', () => {
window.localStorage = {
getItem: () => JSON.stringify({
refreshToken: 'ok-token'
}),
setItem: () => null
}
const dispatch = jest.fn()
refreshUser(dispatch, true)
expect(dispatch.mock.calls).toMatchSnapshot()
})
})
})

it('setAuth0User should work', () => {
expect(setAuth0User({ user: 'a user' })).toMatchSnapshot()
})
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"author": "Trevor Gerhardt <trevorgerhardt@gmail.com>",
"license": "MIT",
"dependencies": {
"auth0-js": "^8.2.0",
"auth0-lock": "^10.10.2",
"isomorphic-fetch": "^2.2.1",
"lodash.isequal": "^4.5.0",
Expand Down
38 changes: 0 additions & 38 deletions src/auth0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Auth0Lock from 'auth0-lock'
import Auth0Client from 'auth0-js'
import {push} from 'react-router-redux'
import {createAction} from 'redux-actions'

const AUTH0_CLIENT_ID = process.env.AUTH0_CLIENT_ID
Expand All @@ -19,39 +17,3 @@ export const defaultLockOptions = {
autoclose: true
}
export const getLock = (lockOptions) => new Auth0Lock(AUTH0_CLIENT_ID, AUTH0_DOMAIN, lockOptions)
export const getClient = () => new Auth0Client({
clientID: AUTH0_CLIENT_ID,
domain: AUTH0_DOMAIN
})

/**
* Use on application mount when authentication is required
*/
export function refreshUser (dispatch, overrideAuthIsRequired) {
const localStorage = window.localStorage
if (authIsRequired || overrideAuthIsRequired) {
const client = getClient()
const userString = localStorage.getItem('user')
const user = userString && JSON.parse(userString)
if (user && user.refreshToken) {
dispatch(setAuth0User(user))
if (process.env.NODE_ENV !== 'development') {
client.refreshToken(user.refreshToken, function (err, delegationResult) {
if (err) {
dispatch(setAuth0User(null))
localStorage.setItem('user', null)
dispatch(push('/login'))
} else {
user.idToken = delegationResult.id_token
dispatch(setAuth0User(user))
localStorage.setItem('user', JSON.stringify(user))
}
})
}
} else {
dispatch(push('/login'))
}
} else {
throw new Error('Auth0 credentials not found!')
}
}
2 changes: 1 addition & 1 deletion src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function create ({
const store = createStore(reducers)
const history = syncHistoryWithStore(browserHistory, store)
return React.createElement(Provider, {store},
React.createElement(app, {history}))
React.createElement(app, {history, store}))
}

export default function mount ({
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,6 @@ auth0-js@8.1.3:
url-join "^1.1.0"
winchan "^0.1.4"

auth0-js@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/auth0-js/-/auth0-js-8.2.0.tgz#0fc7e916a3785da84090d5eed4ae08db75535fbe"
dependencies:
base64-js "^1.2.0"
idtoken-verifier "^1.0.1"
superagent "^3.3.1"
url-join "^1.1.0"
winchan "^0.1.4"

auth0-lock@^10.10.2:
version "10.10.2"
resolved "https://registry.yarnpkg.com/auth0-lock/-/auth0-lock-10.10.2.tgz#b4792ab3cfa5bd789c08835a0e63dea1abc8dbdb"
Expand Down