Skip to content

Commit

Permalink
fix: Adjust the third party login policy
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Oct 19, 2023
1 parent f9d1184 commit 234d7f6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
46 changes: 46 additions & 0 deletions xmcl-keystone-ui/src/composables/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { useLocalStorageCache, useLocalStorageCacheStringValue } from '@/composa
import { AUTHORITY_DEV, AUTHORITY_MICROSOFT, AUTHORITY_MOJANG, YggdrasilApi } from '@xmcl/runtime-api'
import { Ref } from 'vue'
import { DialogKey } from './dialog'
import { injection } from '@/util/inject'
import { kUserContext } from './user'
import { kSettingsState } from './setting'

export function useAccountSystemHistory() {
const authority = useLocalStorageCacheStringValue('loginLastAuthAuthority', AUTHORITY_MICROSOFT as string, 'last-auth-service')
Expand All @@ -18,6 +21,49 @@ export interface AuthorityItem {
value: string
}

const strictLocales = [
// 欧洲 (Europe)
'de-DE', // 德国 (Germany)
'en-GB', // 英国 (United Kingdom)
'fr-FR', // 法国 (France)
'es-ES', // 西班牙 (Spain)
'it-IT', // 意大利 (Italy)
'pt-PT', // 葡萄牙 (Portugal)
'nl-NL', // 荷兰 (Netherlands)
'sv-SE', // 瑞典 (Sweden)
'da-DK', // 丹麦 (Denmark)
'fi-FI', // 芬兰 (Finland)
'no-NO', // 挪威 (Norway)
'el-GR', // 希腊 (Greece)
'tr-TR', // 土耳其 (Turkey)
'is-IS', // 冰岛 (Iceland)
'en-IE', // 爱尔兰 (Ireland)
'el-CY', // 塞浦路斯 (Cyprus)

// 澳洲 (Australia)
'en-AU', // 澳大利亚 (Australia)

// 北美 (North America)
'en-US', // 美国 (United States)
'en-CA', // 加拿大 (Canada)

'ja-JP', // 日本 (Japan)
'ko-KR', // 韩国 (South Korea)
]

export function useAllowThirdparty() {
const { state: setting } = injection(kSettingsState)
const { users } = injection(kUserContext)
const allowThirdParty = computed(() => {
if (users.value.some(u => u.authority === AUTHORITY_MICROSOFT)) return true
if (setting.value?.developerMode) return true
const locale = (new Intl.NumberFormat()).resolvedOptions().locale
if (strictLocales.includes(locale)) return false
return true
})
return allowThirdParty
}

export function useAuthorityItems(allowThirdparty: Ref<boolean>, services: Ref<YggdrasilApi[]>) {
const { t } = useI18n()
const items: Ref<AuthorityItem[]> = computed(() => {
Expand Down
6 changes: 2 additions & 4 deletions xmcl-keystone-ui/src/views/AppLoginForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ import { useBusy, useRefreshable, useService } from '@/composables'
import { injection } from '@/util/inject'
import { AUTHORITY_DEV, AUTHORITY_MICROSOFT, AUTHORITY_MOJANG, OfficialUserServiceKey, UserException, UserServiceKey, isException } from '@xmcl/runtime-api'
import { Ref } from 'vue'
import { useAccountSystemHistory, useAuthorityItems } from '../composables/login'
import { useAccountSystemHistory, useAllowThirdparty, useAuthorityItems } from '../composables/login'
import { kUserContext, useLoginValidation } from '../composables/user'
import AppLoginAuthoritySelect from './AppLoginAuthoritySelect.vue'
import { kSettingsState } from '@/composables/setting'
Expand Down Expand Up @@ -174,9 +174,7 @@ const getUserServiceAccount = (serv: string) => {
// Authority items
const { data: services } = injection(kYggdrasilServices)
const { state: setting } = injection(kSettingsState)
const { users } = injection(kUserContext)
const items = useAuthorityItems(computed(() => !!setting.value?.developerMode || users.value.some(u => u.authority === AUTHORITY_MICROSOFT)), computed(() => services.value || []))
const items = useAuthorityItems(useAllowThirdparty(), computed(() => services.value || []))
// Account history
const { authority, history } = useAccountSystemHistory()
Expand Down
21 changes: 13 additions & 8 deletions xmcl-keystone-ui/src/views/UserMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,19 @@ const login = ref(users.value.length === 0)
const { refresh: onRefresh, refreshing, error } = useRefreshable(async () => {
if (users.value.length === 0) {
login.value = true
} else if (selected.value?.id || selected.value.invalidated || expired.value) {
// Try to refresh
const authority = selected.value?.authority
await refreshUser(selected.value.id).catch((e) => {
console.error(e)
reset({ username: selected.value?.username, authority, error: t('login.userRelogin') })
login.value = true
})
} else {
if (!selected.value.id) {
select(users.value[0].id)
}
if (selected.value?.id || selected.value.invalidated || expired.value) {
// Try to refresh
const authority = selected.value?.authority
await refreshUser(selected.value.id).catch((e) => {
console.error(e)
reset({ username: selected.value?.username, authority, error: t('login.userRelogin') })
login.value = true
})
}
}
})
Expand Down

0 comments on commit 234d7f6

Please sign in to comment.