移动端sso
This commit is contained in:
1 parent
18151fda81
commit
f08cd94924
10 files changed
+155
-11
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { App } from '@capacitor/app'
|
||||
import { Browser } from '@capacitor/browser'
|
||||
import { Capacitor } from '@capacitor/core'
|
||||
import type { Router } from 'vue-router'
|
||||
|
||||
const ssoPaths = new Set(['/sso/callback', '/sso/bind'])
|
||||
|
||||
function appLinkRoute(url: string) {
|
||||
try {
|
||||
const target = new URL(url)
|
||||
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
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async function handleAppUrl(router: Router, url: string) {
|
||||
const route = appLinkRoute(url)
|
||||
if (!route) return
|
||||
await Browser.close().catch(() => undefined)
|
||||
await router.replace(route)
|
||||
}
|
||||
|
||||
export async function initializeNativeAppLinks(router: Router) {
|
||||
if (!Capacitor.isNativePlatform()) return
|
||||
|
||||
await App.addListener('appUrlOpen', event => {
|
||||
void handleAppUrl(router, event.url)
|
||||
})
|
||||
const launchUrl = await App.getLaunchUrl()
|
||||
if (launchUrl?.url) await handleAppUrl(router, launchUrl.url)
|
||||
}
|
||||
|
||||
export async function openSsoLogin(url: string) {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
window.location.assign(url)
|
||||
return
|
||||
}
|
||||
await Browser.open({ url, toolbarColor: '#173a50' })
|
||||
}
|
||||
Reference in new issue
Block a user