diff --git a/src/App.vue b/src/App.vue index 87101e17..156e97bc 100644 --- a/src/App.vue +++ b/src/App.vue @@ -56,7 +56,9 @@ export default { }, beforeCreate() { this.$store.dispatch('config/update') - this.$store.dispatch('auth/checkAuth') + .then(() => { + this.$store.dispatch('auth/checkAuth') + }) setLanguage() }, diff --git a/src/helpers/redirectToProvider.ts b/src/helpers/redirectToProvider.ts new file mode 100644 index 00000000..ba2137f4 --- /dev/null +++ b/src/helpers/redirectToProvider.ts @@ -0,0 +1,13 @@ +interface Provider { + name: string + key: string + authUrl: string + clientId: string +} + +export const redirectToProvider = (provider: Provider, redirectUrl: string) => { + const state = Math.random().toString(36).substring(2, 24) + localStorage.setItem('state', state) + + window.location.href = `${provider.authUrl}?client_id=${provider.clientId}&redirect_uri=${redirectUrl}${provider.key}&response_type=code&scope=openid email profile&state=${state}` +} diff --git a/src/store/modules/auth.js b/src/store/modules/auth.js index 2dfbd509..eb2fd6c7 100644 --- a/src/store/modules/auth.js +++ b/src/store/modules/auth.js @@ -189,6 +189,7 @@ export default { ctx.commit('authenticated', authenticated) if (!authenticated) { ctx.commit('info', null) + ctx.dispatch('config/redirectToProviderIfNothingElseIsEnabled', null, {root: true}) } return Promise.resolve() @@ -198,7 +199,7 @@ export default { if (!jwt) { return } - + const HTTP = HTTPFactory() // We're not returning the promise here to prevent blocking the initial ui render if the user is // accessing the site with a token in local storage diff --git a/src/store/modules/config.js b/src/store/modules/config.js index 60017fed..8f22ae74 100644 --- a/src/store/modules/config.js +++ b/src/store/modules/config.js @@ -3,6 +3,7 @@ import Vue from 'vue' import {CONFIG} from '../mutation-types' import {HTTPFactory} from '@/http-common' import {objectToCamelCase} from '@/helpers/case' +import {redirectToProvider} from '../../helpers/redirectToProvider' export default { namespaced: true, @@ -69,5 +70,15 @@ export default { }) .catch(e => Promise.reject(e)) }, + redirectToProviderIfNothingElseIsEnabled(ctx) { + if (ctx.state.auth.local.enabled === false && + ctx.state.auth.openidConnect.enabled && + ctx.state.auth.openidConnect.providers && + ctx.state.auth.openidConnect.providers.length === 1 && + window.location.pathname.startsWith('/login') // Kinda hacky, but prevents an endless loop. + ) { + redirectToProvider(ctx.state.auth.openidConnect.providers[0], ctx.state.auth.openidConnect.redirectUrl) + } + }, }, } \ No newline at end of file diff --git a/src/views/user/Login.vue b/src/views/user/Login.vue index 547041ec..066b7229 100644 --- a/src/views/user/Login.vue +++ b/src/views/user/Login.vue @@ -83,7 +83,7 @@