mobile update
This commit is contained in:
@@ -46,10 +46,16 @@ export const authApi = {
|
||||
me: () =>
|
||||
apiClient.get<User>('/me'),
|
||||
|
||||
getGoogleAuthUrl: (redirectUrl?: string) =>
|
||||
getGoogleAuthUrl: (redirectUrl?: string, mobile?: boolean) =>
|
||||
apiClient.get<OAuthUrlResponse>('/auth/google', {
|
||||
params: redirectUrl ? { redirect_url: redirectUrl } : undefined,
|
||||
params: {
|
||||
...(redirectUrl ? { redirect_url: redirectUrl } : {}),
|
||||
...(mobile ? { mobile: true } : {}),
|
||||
},
|
||||
}),
|
||||
|
||||
mobileCallback: (token: string) =>
|
||||
apiClient.get('/auth/mobile-callback', { params: { token } }),
|
||||
};
|
||||
|
||||
export const familyApi = {
|
||||
|
||||
@@ -1,18 +1,62 @@
|
||||
import { useState } from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { authApi } from '../api/client';
|
||||
import { useStore } from '../store/useStore';
|
||||
import { Loader2, Wallet } from 'lucide-react';
|
||||
import { FcGoogle } from 'react-icons/fc';
|
||||
|
||||
const isTauriEnv = () => typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window;
|
||||
|
||||
const DEEP_LINK_SCHEME = 'com.arrelin.family-budget-android://auth';
|
||||
|
||||
export default function Login() {
|
||||
const { t } = useTranslation();
|
||||
const { setUser } = useStore();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
if (!isTauriEnv()) return;
|
||||
|
||||
let unlisten: (() => void) | null = null;
|
||||
|
||||
const setupDeepLink = async () => {
|
||||
const { onOpenUrl } = await import('@tauri-apps/plugin-deep-link');
|
||||
unlisten = await onOpenUrl(async (urls) => {
|
||||
const url = Array.isArray(urls) ? urls[0] : urls;
|
||||
if (!url.startsWith(DEEP_LINK_SCHEME)) return;
|
||||
|
||||
const token = new URL(url).searchParams.get('token');
|
||||
if (!token) return;
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
await authApi.mobileCallback(token);
|
||||
const me = await authApi.me();
|
||||
setUser(me.data);
|
||||
} catch {
|
||||
setError(t('login.authError'));
|
||||
setLoading(false);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
setupDeepLink();
|
||||
return () => { unlisten?.(); };
|
||||
}, []);
|
||||
|
||||
const handleGoogleLogin = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
|
||||
if (isTauriEnv()) {
|
||||
const { open } = await import('@tauri-apps/plugin-shell');
|
||||
const response = await authApi.getGoogleAuthUrl(undefined, true);
|
||||
await open(response.data.url);
|
||||
return;
|
||||
}
|
||||
|
||||
const currentUrl = window.location.origin;
|
||||
const response = await authApi.getGoogleAuthUrl(currentUrl);
|
||||
window.location.href = response.data.url;
|
||||
|
||||
Reference in New Issue
Block a user