Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
🌱 Adds ability to override redirect path on login (#159)
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
jmelberg-okta committed Mar 27, 2018
1 parent 0377aa4 commit 89fa782
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
8 changes: 5 additions & 3 deletions packages/okta-vue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,14 @@ router.beforeEach((from, to, next) {
- `scope` *(optional)*: Reserved or custom claims to be returned in the tokens
- `response_type` *(optional)*: Desired token grant types

#### `$auth.loginRedirect`
#### `$auth.loginRedirect(fromUri, additionalParams)`

Performs a full page redirect to Okta based on the initial configuration. If you have an Okta `sessionToken`, you can bypass the full-page redirect by passing in this token. This is recommended when using the [Okta Sign-In Widget](https://github.com/okta/okta-signin-widget). Simply pass in a `sessionToken` into the `loginRedirect` method follows:
Performs a full page redirect to Okta based on the initial configuration. This method accepts a `fromUri` parameter to push the user to after successful authentication.

The parameter `additionalParams` is mapped to the [AuthJS OpenID Connect Options](https://github.com/okta/okta-auth-js#openid-connect-options). This will override any existing [configuration](#configuration). As an example, if you have an Okta `sessionToken`, you can bypass the full-page redirect by passing in this token. This is recommended when using the [Okta Sign-In Widget](https://github.com/okta/okta-signin-widget). Simply pass in a `sessionToken` into the `loginRedirect` method follows:

```typescript
this.$auth.loginRedirect({
this.$auth.loginRedirect('/profile', {
sessionToken: /* sessionToken */
})
```
Expand Down
2 changes: 1 addition & 1 deletion packages/okta-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"scripts": {
"prebuild": "npm run build:package-info",
"prestart": "npm run build",
"prestart": "npm run build && npm run build:harness",
"pretest": "npm run build && npm run build:harness",
"prepublish": "npm run build",
"jest": "jest src/",
Expand Down
8 changes: 5 additions & 3 deletions packages/okta-vue/src/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ function install (Vue, options) {
oktaAuth.userAgent = `${packageInfo.name}/${packageInfo.version} ${oktaAuth.userAgent}`

Vue.prototype.$auth = {
loginRedirect (additionalParams) {
loginRedirect (fromUri, additionalParams) {
if (fromUri) {
localStorage.setItem('referrerPath', fromUri)
}
return oktaAuth.token.getWithRedirect({
responseType: authConfig.response_type,
scopes: authConfig.scope.split(' '),
Expand Down Expand Up @@ -63,8 +66,7 @@ function install (Vue, options) {
authRedirectGuard () {
return async (to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth) && !(await this.isAuthenticated())) {
localStorage.setItem('referrerPath', to.path || '/')
this.loginRedirect()
this.loginRedirect(to.path)
} else {
next()
}
Expand Down
5 changes: 4 additions & 1 deletion packages/okta-vue/test/e2e/harness/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div id="app">
<router-link to="/" tag="button" id='home-button'> Home </router-link>
<button v-if='authenticated' v-on:click='logout' id='logout-button'> Logout </button>
<button v-else v-on:click='$auth.loginRedirect' id='login-button'> Login </button>
<button v-else v-on:click='login' id='login-button'> Login </button>
<router-link to="/protected" tag="button"> Protected </router-link>
<router-view/>
</div>
Expand All @@ -24,6 +24,9 @@ export default {
async isAuthenticated () {
this.authenticated = await this.$auth.isAuthenticated()
},
login () {
this.$auth.loginRedirect('/')
},
async logout () {
await this.$auth.logout()
await this.isAuthenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
password: this.password
})
.then(res =>
this.$auth.loginRedirect({
this.$auth.loginRedirect('/protected', {
sessionToken: res.sessionToken
})
)
Expand Down
2 changes: 2 additions & 0 deletions packages/okta-vue/test/e2e/harness/test/e2e/specs/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
.setValue('#okta-signin-password', process.env.PASSWORD)
.click('#okta-signin-submit')
.waitForElementVisible('#app', 5000)
.assert.urlContains('/protected')
.assert.elementPresent('#logout-button')
.assert.containsText('.protected', 'Protected!')
.pause(2000) // Wait for async function to finish
Expand Down Expand Up @@ -52,6 +53,7 @@ module.exports = {
.setValue('#password', process.env.PASSWORD)
.click('#submit')
.waitForElementVisible('#logout-button', 5000)
.assert.urlContains('/protected')
.assert.elementPresent('#logout-button')
.click('#logout-button')
.end()
Expand Down

0 comments on commit 89fa782

Please sign in to comment.