Skip to content

Commit

Permalink
removing cache of provider per domain
Browse files Browse the repository at this point in the history
  • Loading branch information
caiokf committed Nov 18, 2019
1 parent 861b216 commit 9af79d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
36 changes: 11 additions & 25 deletions spec/auth0.spec.js → spec/provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,17 @@ import React from 'react'
import { mount, shallow } from 'enzyme'

import AuthProvider from '../src/provider'
import Auth from '../src/auth'
jest.mock('../src/auth')

describe('Auth0 Provider', () => {
let auth0MockFn
let auth0MockResult
beforeEach(() => {
Auth.mockClear()
})

describe('Auth0 Provider', () => {
beforeEach(() => {
auth0MockResult = {
computeAuthed: jest.fn(),
login: jest.fn(),
logout: jest.fn(),
isLoggedIn: jest.fn(),
getTokenExpirationDate: jest.fn(),
isTokenExpired: jest.fn(),
getIdToken: jest.fn(),
setIdToken: jest.fn(),
getAccessToken: jest.fn(),
setAccessToken: jest.fn(),
getNextPath: jest.fn(),
setNextPath: jest.fn(),
getProfile: jest.fn(),
setProfile: jest.fn(),
}

auth0MockFn = jest.fn().mockReturnValue(auth0MockResult)
window.ReactRouterAuth0Provider = auth0MockFn
window.ReactRouterAuth0Provider = null
Auth.mockClear()
})

it('requires domain prop', () => {
Expand Down Expand Up @@ -96,7 +82,7 @@ describe('Auth0 Provider', () => {
</AuthProvider>
)

expect(auth0MockFn).toHaveBeenCalledWith('any_domain', 'any_clientId', {})
expect(Auth).toHaveBeenCalledWith('any_domain', 'any_clientId', {})
})

it('initializes Auth0 with domain, clientId and props as options', () => {
Expand All @@ -106,6 +92,6 @@ describe('Auth0 Provider', () => {
</AuthProvider>
)

expect(auth0MockFn).toHaveBeenCalledWith('any_domain', 'any_clientId', { scope: 'email profile openid' })
expect(Auth).toHaveBeenCalledWith('any_domain', 'any_clientId', { scope: 'email profile openid' })
})
})
})
10 changes: 1 addition & 9 deletions src/provider.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import React from 'react'
import Auth from './Auth'
import Auth from './auth'

const getOrCreateReactRouterAuth0Provider = (domain, clientId, options) => {
window.ReactRouterAuth0Provider = window.ReactRouterAuth0Provider || new Auth(domain, clientId, options)

window.ReactRouterAuth0Provider.__cachedInstances = window.ReactRouterAuth0Provider.__cachedInstances || {}
const cacheKey = `key=${clientId} options=${JSON.stringify(options)}`

const provider = window.ReactRouterAuth0Provider.__cachedInstances[cacheKey] || window.ReactRouterAuth0Provider(domain, clientId, options)
window.ReactRouterAuth0Provider.__cachedInstances[cacheKey] = provider

return provider
}

export default class AuthProvider extends React.Component {
Expand Down

0 comments on commit 9af79d1

Please sign in to comment.