sso修复
This commit is contained in:
1 parent
76b26a49a8
commit
d95ff27ee4
7 files changed
+104
-37
No files matched your search
@@ -4,21 +4,38 @@ import { Capacitor } from '@capacitor/core'
|
||||
import type { Router } from 'vue-router'
|
||||
|
||||
const ssoPaths = new Set(['/sso/callback', '/sso/bind'])
|
||||
const nativeSsoStateKey = 'jiaowu_native_sso_state'
|
||||
|
||||
export function beginNativeSsoLogin() {
|
||||
if (!Capacitor.isNativePlatform()) return null
|
||||
const bytes = crypto.getRandomValues(new Uint8Array(32))
|
||||
const state = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('')
|
||||
localStorage.setItem(nativeSsoStateKey, state)
|
||||
return state
|
||||
}
|
||||
|
||||
export function nativeSsoLoginState() {
|
||||
return Capacitor.isNativePlatform() ? localStorage.getItem(nativeSsoStateKey) : null
|
||||
}
|
||||
|
||||
export function clearNativeSsoLoginState() {
|
||||
localStorage.removeItem(nativeSsoStateKey)
|
||||
}
|
||||
|
||||
function appLinkRoute(url: string) {
|
||||
try {
|
||||
const target = new URL(url)
|
||||
if (target.protocol === 'mingxu:' && target.host === 'open' && ssoPaths.has(target.pathname)) {
|
||||
return target.pathname + target.search + target.hash
|
||||
}
|
||||
const publicBase = new URL(
|
||||
String(import.meta.env.VITE_PUBLIC_BASE_URL ?? location.origin),
|
||||
)
|
||||
if (
|
||||
target.protocol !== 'https:' ||
|
||||
target.origin !== publicBase.origin ||
|
||||
!ssoPaths.has(target.pathname)
|
||||
) {
|
||||
return null
|
||||
}
|
||||
return target.pathname + target.search + target.hash
|
||||
return target.protocol === 'https:' &&
|
||||
target.origin === publicBase.origin &&
|
||||
ssoPaths.has(target.pathname)
|
||||
? target.pathname + target.search + target.hash
|
||||
: null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@ import {
|
||||
isNativeApp,
|
||||
saveAuthSession,
|
||||
} from '../auth/session'
|
||||
import {
|
||||
clearNativeSsoLoginState,
|
||||
nativeSsoLoginState,
|
||||
} from '../services/nativeAppLinks'
|
||||
|
||||
export interface CurrentUser {
|
||||
id: string
|
||||
@@ -39,10 +43,12 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const { data } = await http.post('/auth/sso/exchange', {
|
||||
code,
|
||||
isNativeApp: isNativeApp(),
|
||||
nativeState: nativeSsoLoginState(),
|
||||
})
|
||||
token.value = data.token
|
||||
user.value = data.user
|
||||
saveAuthSession(data)
|
||||
clearNativeSsoLoginState()
|
||||
}
|
||||
|
||||
async function bindSso(code: string, userName: string, password: string) {
|
||||
@@ -51,10 +57,12 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
userName,
|
||||
password,
|
||||
isNativeApp: isNativeApp(),
|
||||
nativeState: nativeSsoLoginState(),
|
||||
})
|
||||
token.value = data.token
|
||||
user.value = data.user
|
||||
saveAuthSession(data)
|
||||
clearNativeSsoLoginState()
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useRoute, useRouter } from 'vue-router'
|
||||
import http, { apiErrorMessage } from '../api/http'
|
||||
import { useAuthStore } from '../stores/auth'
|
||||
import { isNativeApp } from '../auth/session'
|
||||
import { openSsoLogin } from '../services/nativeAppLinks'
|
||||
import { beginNativeSsoLogin, openSsoLogin } from '../services/nativeAppLinks'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
@@ -45,8 +45,9 @@ async function loadAccount() {
|
||||
async function startBinding() {
|
||||
actionLoading.value = true
|
||||
try {
|
||||
const nativeState = beginNativeSsoLogin()
|
||||
const { data } = await http.post('/auth/sso/prepare-binding', null, {
|
||||
params: { nativeApp: isNativeApp() },
|
||||
params: { nativeApp: isNativeApp(), nativeState },
|
||||
})
|
||||
await openSsoLogin(String(data.loginUrl))
|
||||
} catch (error) {
|
||||
|
||||
Loaded 3 of 7 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user