Skip to content

Commit

Permalink
Removes oktaAuth.stop() from OktaVue module
Browse files Browse the repository at this point in the history
OKTA-492711
<<<Jenkins Check-In of Tested SHA: d6d0aeb for [email protected]>>>
Artifact: okta-vue
Files changed count: 10
PR Link: #96
  • Loading branch information
denysoblohin-okta authored and eng-prod-CI-bot-okta committed Jun 2, 2022
1 parent 36d12c1 commit 12f779a
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 47 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[authState]: https://github.com/okta/okta-auth-js#authstatemanager

# 5.3.0

### Others

- [#96](https://github.com/okta/okta-vue/pull/96)
- Removes `oktaAuth.stop()` from `OktaVue` module
- Removes `oktaAuth.start()` from `LoginCallback` component

# 5.2.1

### Others
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[![npm version](https://img.shields.io/npm/v/@okta/okta-vue.svg?style=flat-square)](https://www.npmjs.com/package/@okta/okta-vue)
[![build status](https://img.shields.io/travis/okta/okta-oidc-js/master.svg?style=flat-square)](https://travis-ci.org/okta/okta-vue)

> Okta Vue version 4 is for Vue 3 and Vue Router 4. If you are looking for @okta/okta-vue@3.x which supports Vue 2, please checkout the [master branch](https://github.com/okta/okta-vue).
> Okta Vue version 4+ is for Vue 3 and Vue Router 4. If you are looking for @okta/okta-vue@3.x which supports Vue 2, please checkout the [master branch](https://github.com/okta/okta-vue).
Okta Vue SDK builds on top of the [Okta Auth SDK][]. This SDK integrates with the [vue-router][] and extends the [Vue prototype][] with an [Okta Auth SDK][] instance to help you quickly add authentication and authorization to your Vue single-page web application.

Expand Down Expand Up @@ -58,7 +58,7 @@ This library currently supports:
This library is available through [npm](https://www.npmjs.com/package/@okta/okta-vue). To install it, simply add it to your project:

```bash
npm install --save @okta/okta-vue@^4.0.0
npm install --save @okta/okta-vue
```

### Configuration
Expand Down
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ module.exports = {
'^.+\\.jsx?$': 'babel-jest',
'.*\\.(vue)$': 'vue3-jest'
},
testEnvironment: 'jsdom'
testEnvironment: 'jsdom',
setupFilesAfterEnv: [
'./test/jest.setup.js'
],
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@babel/core": "^7.12.3",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@okta/okta-auth-js": "^6.0.0",
"@okta/okta-auth-js": "^6.5.1",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-replace": "^2.3.4",
Expand Down
3 changes: 1 addition & 2 deletions src/components/LoginCallback.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export default defineComponent({
},
async beforeMount () {
try {
await this.$auth.handleLoginRedirect()
this.$auth.start();
await this.$auth.handleLoginRedirect();
} catch (e) {
const isInteractionRequiredError = this.$auth.isInteractionRequiredError || this.$auth.idx.isInteractionRequiredError;
if (isInteractionRequiredError(e)) {
Expand Down
15 changes: 7 additions & 8 deletions src/okta-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,17 @@ function install (app: App, {
if (!oktaAuth.options.restoreOriginalUri) {
oktaAuth.options.restoreOriginalUri = async (oktaAuth: OktaAuth, originalUri: string) => {
// If a router is available, provide a default implementation
if (_router && originalUri) {
const path = toRelativeUrl(originalUri, window.location.origin)
if (_router) {
const path = toRelativeUrl(originalUri || '/', window.location.origin);
_router.replace({ path })
}
}
}

// Calculates initial auth state and fires change event for listeners
// Also starts services
oktaAuth.start();

app.mixin({
data () {
return {
Expand All @@ -120,16 +124,11 @@ function install (app: App, {
},
created () {
// subscribe to the latest authState
this.authState = oktaAuth.authStateManager.getAuthState()
oktaAuth.authStateManager.subscribe(this.$_oktaVue_handleAuthStateUpdate)
if (!oktaAuth.token.isLoginRedirect()) {
// Calculates initial auth state and fires change event for listeners
// Also starts the token auto-renew service
oktaAuth.start();
}
},
beforeUnmount () {
oktaAuth.authStateManager.unsubscribe(this.$_oktaVue_handleAuthStateUpdate)
oktaAuth.stop()
},
// private property naming convention follows
// https://vuejs.org/v2/style-guide/#Private-property-names-essential
Expand Down
3 changes: 3 additions & 0 deletions test/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// broadcast-channel should not detect node environment
// https://github.com/pubkey/broadcast-channel/blob/master/src/util.js#L61
process[Symbol.toStringTag] = 'Process';
18 changes: 11 additions & 7 deletions test/specs/LoginCallback.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ describe('LoginCallback', () => {
let oktaAuth
let wrapper

beforeEach(() => {
oktaAuth = null
wrapper = null
})

function createOktaAuth(options = {}) {
oktaAuth = new OktaAuth(Object.assign({
issuer: 'https://foo',
Expand All @@ -34,6 +29,15 @@ describe('LoginCallback', () => {
}, options));
}

beforeEach(() => {
oktaAuth = null
wrapper = null
})

afterEach(() => {
oktaAuth?.stop()
})

async function navigateToCallback (options = {}) {
jest.spyOn(oktaAuth, 'isLoginRedirect').mockReturnValue(options.isLoginRedirect)
jest.spyOn(oktaAuth, 'storeTokensFromRedirect').mockResolvedValue(undefined)
Expand Down Expand Up @@ -73,13 +77,13 @@ describe('LoginCallback', () => {
expect(oktaAuth.handleLoginRedirect).toHaveBeenCalled()
})

it('starts oktaAuth service on login redirect', async () => {
it('does not start oktaAuth service after login redirect', async () => {
createOktaAuth()
jest.spyOn(oktaAuth, 'handleLoginRedirect');
jest.spyOn(oktaAuth, 'start');
await navigateToCallback()
expect(oktaAuth.handleLoginRedirect).toHaveBeenCalled()
expect(oktaAuth.start).toHaveBeenCalled()
expect(oktaAuth.start).toHaveBeenCalledTimes(1)
})

it('calls the default "restoreOriginalUri" options when in login redirect uri', async () => {
Expand Down
21 changes: 13 additions & 8 deletions test/specs/OktaVue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,16 @@ describe('OktaVue', () => {
})

it('should render "authenticated" when authState.isAuthenticated is true', () => {
oktaAuth.authStateManager.updateAuthState = jest.fn().mockImplementation(() => {
oktaAuth.emitter.emit('authStateChange', {
isAuthenticated: true
})
oktaAuth.authStateManager.getAuthState = jest.fn().mockReturnValue({
isAuthenticated: true
})
bootstrap()
expect(wrapper.find('#state').text()).toBe('authenticated')
})

it('should render "not authenticated" when authState.isAuthenticated is false', () => {
oktaAuth.authStateManager.updateAuthState = jest.fn().mockImplementation(() => {
oktaAuth.emitter.emit('authStateChange', {
isAuthenticated: false
})
oktaAuth.authStateManager.getAuthState = jest.fn().mockReturnValue({
isAuthenticated: false
})
bootstrap()
expect(wrapper.find('#state').text()).toBe('not authenticated')
Expand Down Expand Up @@ -164,4 +160,13 @@ describe('OktaVue', () => {
expect(oktaAuth.authStateManager.unsubscribe).toHaveBeenCalledWith(wrapper.vm.$_oktaVue_handleAuthStateUpdate)
})

it('should call service start, but not stop', () => {
oktaAuth.start = jest.fn()
oktaAuth.stop = jest.fn()
bootstrap()
wrapper.unmount()
expect(oktaAuth.start).toHaveBeenCalled()
expect(oktaAuth.stop).not.toHaveBeenCalled()
})

})
Loading

0 comments on commit 12f779a

Please sign in to comment.