From 7d282bf1d0201f68eb034c3a5e669298dc1902cf Mon Sep 17 00:00:00 2001 From: Cody Jackson Date: Thu, 18 Apr 2024 13:45:09 -0700 Subject: [PATCH] Migrate the usage of asyncData to fetch After the migration we will then remove all of the initialization code. This is motivated by the Vue3 migration. --- shell/pages/auth/login.vue | 170 +++++++++---------- shell/pages/auth/logout.vue | 4 +- shell/pages/auth/setup.vue | 99 ++++++----- shell/pages/c/_cluster/auth/config/index.vue | 18 +- shell/pages/c/_cluster/auth/roles/index.vue | 14 +- 5 files changed, 158 insertions(+), 147 deletions(-) diff --git a/shell/pages/auth/login.vue b/shell/pages/auth/login.vue index 57c9f088a01..a58e2d48374 100644 --- a/shell/pages/auth/login.vue +++ b/shell/pages/auth/login.vue @@ -23,113 +23,50 @@ import { LOGIN_ERRORS } from '@shell/store/auth'; import { getBrand, getVendor, - getProduct, setBrand, setVendor } from '@shell/config/private-label'; import loadPlugins from '@shell/plugins/plugin'; import { fetchInitialSettings } from '@shell/utils/settings'; +import Loading from '@shell/components/Loading'; export default { name: 'Login', components: { - LabeledInput, AsyncButton, Checkbox, BrandImage, Banner, InfoBox, CopyCode, Password, LocaleSelector + LabeledInput, AsyncButton, Checkbox, BrandImage, Banner, InfoBox, CopyCode, Password, LocaleSelector, Loading }, - async asyncData({ route, redirect, store }) { - const drivers = await store.dispatch('auth/getAuthProviders'); - const providers = sortBy(drivers.map((x) => x.id), ['id']); - - const hasLocal = providers.includes('local'); - const hasOthers = hasLocal && !!providers.find((x) => x !== 'local'); - - if ( hasLocal ) { - // Local is special and handled here so that it can be toggled - removeObject(providers, 'local'); - } - - let firstLoginSetting, plSetting, brand; - - // Load settings. - // For newer versions this will return all settings if you are somehow logged in, - // and just the public ones if you aren't. - try { - await fetchInitialSettings(store); - - firstLoginSetting = store.getters['management/byId'](MANAGEMENT.SETTING, SETTING.FIRST_LOGIN); - plSetting = store.getters['management/byId'](MANAGEMENT.SETTING, SETTING.PL); - brand = store.getters['management/byId'](MANAGEMENT.SETTING, SETTING.BRAND); - } catch (e) { - // Older versions used Norman API to get these - firstLoginSetting = await store.dispatch('rancher/find', { - type: NORMAN.SETTING, - id: SETTING.FIRST_LOGIN, - opt: { url: `/v3/settings/${ SETTING.FIRST_LOGIN }` } - }); - - plSetting = await store.dispatch('rancher/find', { - type: NORMAN.SETTING, - id: SETTING.PL, - opt: { url: `/v3/settings/${ SETTING.PL }` } - }); - - brand = await store.dispatch('rancher/find', { - type: NORMAN.SETTING, - id: SETTING.BRAND, - opt: { url: `/v3/settings/${ SETTING.BRAND }` } - }); - } - - if (plSetting.value?.length && plSetting.value !== getVendor()) { - setVendor(plSetting.value); - } - - if (brand?.value?.length && brand.value !== getBrand()) { - setBrand(brand.value); - } - - let singleProvider; - - if (providers.length === 1) { - singleProvider = providers[0]; - } + data() { + const username = this.$cookies.get(USERNAME, { parseJSON: false }) || ''; return { - vendor: getVendor(), - providers, - hasOthers, - hasLocal, - showLocal: !hasOthers || (route.query[LOCAL] === _FLAGGED), - firstLogin: firstLoginSetting?.value === 'true', - singleProvider, - showLocaleSelector: !process.env.loginLocaleSelector || process.env.loginLocaleSelector === 'true' - }; - }, - - data({ $cookies }) { - const username = $cookies.get(USERNAME, { parseJSON: false }) || ''; - - return { - product: getProduct(), - username, remember: !!username, password: '', - timedOut: this.$route.query[TIMED_OUT] === _FLAGGED, - loggedOut: this.$route.query[LOGGED_OUT] === _FLAGGED, - isSsoLogout: this.$route.query[IS_SSO] === _FLAGGED, - err: this.$route.query.err, + timedOut: this.$route.query[TIMED_OUT] === _FLAGGED, + loggedOut: this.$route.query[LOGGED_OUT] === _FLAGGED, + isSsoLogout: this.$route.query[IS_SSO] === _FLAGGED, + err: this.$route.query.err, + showLocaleSelector: !process.env.loginLocaleSelector || process.env.loginLocaleSelector === 'true', + hasLocal: false, + showLocal: false, providers: [], providerComponents: [], - customLoginError: {} + customLoginError: {}, + firstLogin: false, + vendor: getVendor() }; }, computed: { ...mapGetters({ t: 'i18n/t' }), + singleProvider() { + return this.providers.length === 1 ? this.providers[0] : undefined; + }, + nonLocalPrompt() { if (this.singleProvider) { const provider = this.displayName(this.singleProvider); @@ -179,12 +116,24 @@ export default { }, async fetch() { + const { firstLoginSetting } = await this.loadInitialSettings(); const { value } = await this.$store.dispatch('management/find', { type: MANAGEMENT.SETTING, id: SETTING.BANNERS }); + const drivers = await this.$store.dispatch('auth/getAuthProviders'); + const providers = sortBy(drivers.map((x) => x.id), ['id']); + const hasLocal = providers.includes('local'); + const hasOthers = hasLocal && !!providers.find((x) => x !== 'local'); - this.customLoginError = JSON.parse(value).loginError; - }, + if ( hasLocal ) { + // Local is special and handled here so that it can be toggled + removeObject(providers, 'local'); + } - mounted() { + this.vendor = getVendor(); + this.providers = providers; + this.hasLocal = hasLocal; + this.showLocal = !hasOthers || (this.$route.query[LOCAL] === _FLAGGED); + this.customLoginError = JSON.parse(value).loginError; + this.firstLogin = firstLoginSetting?.value === 'true'; this.username = this.firstLogin ? 'admin' : this.username; this.$nextTick(() => { this.focusSomething(); @@ -192,6 +141,52 @@ export default { }, methods: { + async loadInitialSettings() { + let firstLoginSetting, plSetting, brand; + + // Load settings. + // For newer versions this will return all settings if you are somehow logged in, + // and just the public ones if you aren't. + try { + await fetchInitialSettings(this.$store); + + firstLoginSetting = this.$store.getters['management/byId'](MANAGEMENT.SETTING, SETTING.FIRST_LOGIN); + plSetting = this.$store.getters['management/byId'](MANAGEMENT.SETTING, SETTING.PL); + brand = this.$store.getters['management/byId'](MANAGEMENT.SETTING, SETTING.BRAND); + } catch (e) { + // Older versions used Norman API to get these + firstLoginSetting = await this.$store.dispatch('rancher/find', { + type: NORMAN.SETTING, + id: SETTING.FIRST_LOGIN, + opt: { url: `/v3/settings/${ SETTING.FIRST_LOGIN }` } + }); + + plSetting = await this.$store.dispatch('rancher/find', { + type: NORMAN.SETTING, + id: SETTING.PL, + opt: { url: `/v3/settings/${ SETTING.PL }` } + }); + + brand = await this.$store.dispatch('rancher/find', { + type: NORMAN.SETTING, + id: SETTING.BRAND, + opt: { url: `/v3/settings/${ SETTING.BRAND }` } + }); + } + + if (plSetting.value?.length && plSetting.value !== getVendor()) { + setVendor(plSetting.value); + } + + if (brand?.value?.length && brand.value !== getBrand()) { + setBrand(brand.value); + } + + return { + firstLoginSetting, plSetting, brand + }; + }, + displayName(provider) { return this.t(`model.authConfig.provider.${ provider }`); }, @@ -290,7 +285,11 @@ export default {