Directly redirect to the openid auth provider if that's the only auth method
This commit is contained in:
parent
a5687d78f5
commit
3aa316988b
5 changed files with 49 additions and 16 deletions
|
@ -56,7 +56,9 @@ export default {
|
|||
},
|
||||
beforeCreate() {
|
||||
this.$store.dispatch('config/update')
|
||||
.then(() => {
|
||||
this.$store.dispatch('auth/checkAuth')
|
||||
})
|
||||
|
||||
setLanguage()
|
||||
},
|
||||
|
|
13
src/helpers/redirectToProvider.ts
Normal file
13
src/helpers/redirectToProvider.ts
Normal file
|
@ -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}`
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
|
@ -83,7 +83,7 @@
|
|||
</form>
|
||||
|
||||
<div
|
||||
v-if="hasApiUrl && openidConnect.enabled && openidConnect.providers && openidConnect.providers.length > 0"
|
||||
v-if="hasOpenIdProviders"
|
||||
class="mt-4">
|
||||
<x-button
|
||||
@click="redirectToProvider(p)"
|
||||
|
@ -110,6 +110,7 @@ import {ERROR_MESSAGE, LOADING} from '@/store/mutation-types'
|
|||
import legal from '../../components/misc/legal'
|
||||
import ApiConfig from '@/components/misc/api-config.vue'
|
||||
import {getErrorText} from '@/message'
|
||||
import {redirectToProvider} from '../../helpers/redirectToProvider'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
|
@ -150,7 +151,14 @@ export default {
|
|||
this.hasApiUrl = window.API_URL !== ''
|
||||
this.setTitle(this.$t('user.auth.login'))
|
||||
},
|
||||
computed: mapState({
|
||||
computed: {
|
||||
hasOpenIdProviders() {
|
||||
return this.hasApiUrl &&
|
||||
this.openidConnect.enabled &&
|
||||
this.openidConnect.providers &&
|
||||
this.openidConnect.providers.length > 0
|
||||
},
|
||||
...mapState({
|
||||
registrationEnabled: state => state.config.registrationEnabled,
|
||||
loading: LOADING,
|
||||
errorMessage: ERROR_MESSAGE,
|
||||
|
@ -159,6 +167,7 @@ export default {
|
|||
localAuthEnabled: state => state.config.auth.local.enabled,
|
||||
openidConnect: state => state.config.auth.openidConnect,
|
||||
}),
|
||||
},
|
||||
methods: {
|
||||
setLoading() {
|
||||
const timeout = setTimeout(() => {
|
||||
|
@ -202,10 +211,7 @@ export default {
|
|||
})
|
||||
},
|
||||
redirectToProvider(provider) {
|
||||
const state = Math.random().toString(36).substring(2, 24)
|
||||
localStorage.setItem('state', state)
|
||||
|
||||
window.location.href = `${provider.authUrl}?client_id=${provider.clientId}&redirect_uri=${this.openidConnect.redirectUrl}${provider.key}&response_type=code&scope=openid email profile&state=${state}`
|
||||
redirectToProvider(provider, this.openidConnect.redirectUrl)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue