Skip to content

Commit

Permalink
Change to clear user when token expires.
Browse files Browse the repository at this point in the history
  • Loading branch information
soukoku committed Mar 4, 2019
1 parent 20310bd commit f5da3eb
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 82 deletions.
45 changes: 30 additions & 15 deletions dist/VueOidcAuth.common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/VueOidcAuth.common.js.map

Large diffs are not rendered by default.

45 changes: 30 additions & 15 deletions dist/VueOidcAuth.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/VueOidcAuth.umd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/VueOidcAuth.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/VueOidcAuth.umd.min.js.map

Large diffs are not rendered by default.

41 changes: 26 additions & 15 deletions sample/src/auth.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import Vue from 'vue';
import { createOidcAuth, SignInType } from 'vue-oidc-client';
import Vue from 'vue'
import {
createOidcAuth,
SignInType,
LogLevel
} from 'vue-oidc-client/src/VueOidcAuth'

const loco = window.location;
const appRootUrl = `${loco.protocol}//${loco.host}${process.env.BASE_URL}`;
const loco = window.location
const appRootUrl = `${loco.protocol}//${loco.host}${process.env.BASE_URL}`

var mainOidc = createOidcAuth('main', SignInType.Window, appRootUrl, {
authority: 'https://demo.identityserver.io/',
client_id: 'implicit.shortlived', // 'implicit.shortlived',
response_type: 'id_token token',
scope: 'openid profile email api',
// test use
prompt: 'login',
login_hint: 'bob'
});
Vue.prototype.$oidc = mainOidc;
export default mainOidc;
var mainOidc = createOidcAuth(
'main',
SignInType.Window,
appRootUrl,
{
authority: 'https://demo.identityserver.io/',
client_id: 'implicit.shortlived', // 'implicit.shortlived',
response_type: 'id_token token',
scope: 'openid profile email api',
// test use
prompt: 'login',
login_hint: 'bob'
},
console,
LogLevel.DEBUG
)
Vue.prototype.$oidc = mainOidc
export default mainOidc
16 changes: 8 additions & 8 deletions sample/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5501,10 +5501,10 @@ obuf@^1.0.0, obuf@^1.1.2:
resolved "https://registry.yarnpkg.com/obuf/-/obuf-1.1.2.tgz#09bea3343d41859ebd446292d11c9d4db619084e"
integrity sha512-PX1wu0AmAdPqOL1mWhqmlOd8kOIZQwGZw6rh7uby9fTc5lhaOWFLX3I6R1hrF9k3zUY40e6igsLGkDXK92LJNg==

oidc-client@^1.5.4:
version "1.5.4"
resolved "https://registry.yarnpkg.com/oidc-client/-/oidc-client-1.5.4.tgz#5efb7464848e39926778ad96b155935282b9fe0b"
integrity sha512-ddom2rLs7xc6kAOOD7LMXQJTcoJxGLfIM/Up19/LiDYQQrSERGCtyuqjyDoR8T2XQtFhU5Y2HWs1O3WdvyYckA==
oidc-client@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/oidc-client/-/oidc-client-1.6.1.tgz#76a7f24ad7fb5342269b6cae895bb32e1e27e785"
integrity sha512-buA9G0hlFjUwxoL/xuHunBtTgoICtJSojnZtATqMliUTKxPYAzHprOQ85Lj0hgF+Zv8lI/ViqaNFDG0Z5KCUKA==
dependencies:
jsrsasign "^8.0.12"
optionalDependencies:
Expand Down Expand Up @@ -8063,11 +8063,11 @@ vue-loader@^15.6.2:
vue-style-loader "^4.1.0"

vue-oidc-client@^0.2.9:
version "0.2.7"
resolved "https://registry.yarnpkg.com/vue-oidc-client/-/vue-oidc-client-0.2.7.tgz#c9980721efdccc8509b3a35e38782fa2514caa83"
integrity sha512-Mr1HaC1D0FeQ33NwRRKy027+b7R40Uc36t+FjZ9yQF+QSe6NzgWim3eJpkBFfVYuMgtkP2rcqGz+F92LQD2/WQ==
version "0.2.9"
resolved "https://registry.yarnpkg.com/vue-oidc-client/-/vue-oidc-client-0.2.9.tgz#04221dd434ef6a20adf5eddeee4dfe13ea2541b9"
integrity sha512-xWalzfClqC6tdNOqe6v2TF9hKCbpo3x4HfpoWpR402kQwuaz/n8jUM82uE4bWE1JdMH57L5x5OiIPn/wzqXIpw==
dependencies:
oidc-client "^1.5.4"
oidc-client "^1.6.1"

vue-router@^3.0.2:
version "3.0.2"
Expand Down
65 changes: 45 additions & 20 deletions src/VueOidcAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,36 @@ export function createOidcAuth(
})

mgr.events.addAccessTokenExpired(() => {
Log.debug(`${authName} auth token expired`)
if (auth.isAuthenticated) {
mgr
.signinSilent()
.then(() => {
Log.debug(`${authName} auth silent signin after token expiration`)
})
.catch(() => {
Log.debug(
`${authName} auth silent signin error after token expiration`
)
signInIfNecessary()
})
}
Log.debug(
`${authName} auth token expired, user is authenticated=${
auth.isAuthenticated
}`
)
auth.user = null
signInIfNecessary()
// if (auth.isAuthenticated) {
// mgr
// .signinSilent()
// .then(() => {
// Log.debug(`${authName} auth silent signin after token expiration`)
// })
// .catch(() => {
// Log.debug(
// `${authName} auth silent signin error after token expiration`
// )
// signInIfNecessary()
// })
// }
})

mgr.events.addSilentRenewError(e => {
Log.debug(`${authName} auth silent renew error ${e}`)
// TODO: need to restart renew manually?
if (auth.isAuthenticated) {
// setTimeout(() => {
// mgr.signinSilent();
// }, 5000);
setTimeout(() => {
Log.debug(`${authName} auth silent renew retry`)
mgr.signinSilent()
}, 5000)
} else {
signInIfNecessary()
}
Expand All @@ -128,15 +135,23 @@ export function createOidcAuth(
signInIfNecessary()
})

mgr.events.addUserSessionChanged(user => {
Log.debug(`${authName} auth user session changed:`, user)
})

function signInIfNecessary() {
if (auth.myRouter) {
const current = auth.myRouter.currentRoute
if (current && current.meta.authName === authName) {
Log.debug(`${authName} auth page re-signin`)
Log.debug(`${authName} auth page re-signin with ${defaultSignInType}`)

signInReal(defaultSignInType, { state: { current } })
.then(() => {})
.catch(() => {})
.then(() => {
// auth.myRouter()
})
.catch(() => {
setTimeout(signInIfNecessary, 5000)
})
// window.location.reload();
// auth.myRouter.go(); //replace('/');
}
Expand Down Expand Up @@ -229,8 +244,18 @@ export function createOidcAuth(
to.matched.some(record => record.meta.authName === this.authName)
) {
if (this.isAuthenticated) {
Log.debug(
`${authName} auth authenticated user entering protected route ${
to.fullPath
}`
)
next()
} else {
Log.debug(
`${authName} auth anon user entering protected route ${
to.fullPath
}`
)
signInReal(defaultSignInType, { state: { to } })
.then(() => {
if (defaultSignInType === SignInType.Window) {
Expand Down
11 changes: 6 additions & 5 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module.exports = {
// productionSourceMap: false,
css: { extract: false }
// chainWebpack: config => {
// config.externals({
// ...config.get('externals'),
// 'oidc-client': 'oidc'
// });
};
// config.externals({
// ...config.get('externals'),
// 'oidc-client': 'oidc'
// })
// }
}

0 comments on commit f5da3eb

Please sign in to comment.