Add Google login to mobile admin PWA
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit is contained in:
Codex Agent
2026-01-23 13:59:14 +01:00
parent 2729c3c713
commit 72dd1409e8
2 changed files with 206 additions and 1 deletions

View File

@@ -68,6 +68,8 @@ export default function MobileLoginPage() {
const searchParams = React.useMemo(() => new URLSearchParams(location.search), [location.search]);
const rawReturnTo = searchParams.get('return_to');
const oauthError = searchParams.get('error');
const oauthDescription = searchParams.get('error_description');
const computeDefaultAfterLogin = React.useCallback(
(abilityList?: string[]) => {
@@ -78,11 +80,31 @@ export default function MobileLoginPage() {
);
const fallbackTarget = computeDefaultAfterLogin();
const { finalTarget } = React.useMemo(
const { finalTarget, encodedFinal } = React.useMemo(
() => resolveReturnTarget(rawReturnTo, fallbackTarget),
[rawReturnTo, fallbackTarget],
);
const oauthMessage = React.useMemo(() => {
if (!oauthError) {
return null;
}
const fallback = oauthDescription ?? oauthError;
return t(`login.oauth_errors.${oauthError}`, { defaultValue: fallback });
}, [oauthDescription, oauthError, t]);
const googleHref = React.useMemo(() => {
if (!encodedFinal) {
return '/event-admin/auth/google';
}
const params = new URLSearchParams({ return_to: encodedFinal });
return `/event-admin/auth/google?${params.toString()}`;
}, [encodedFinal]);
React.useEffect(() => {
if (status === 'authenticated') {
navigate(finalTarget, { replace: true });
@@ -92,6 +114,7 @@ export default function MobileLoginPage() {
const [login, setLogin] = React.useState('');
const [password, setPassword] = React.useState('');
const [error, setError] = React.useState<string | null>(null);
const [isRedirectingToGoogle, setIsRedirectingToGoogle] = React.useState(false);
const [installBannerDismissed, setInstallBannerDismissedState] = React.useState(() => getInstallBannerDismissed());
const installBanner = shouldShowInstallBanner(
{
@@ -138,6 +161,15 @@ export default function MobileLoginPage() {
});
};
const handleGoogleLogin = React.useCallback(() => {
if (typeof window === 'undefined') {
return;
}
setIsRedirectingToGoogle(true);
window.location.assign(googleHref);
}, [googleHref]);
return (
<YStack
minHeight="100vh"
@@ -176,6 +208,22 @@ export default function MobileLoginPage() {
<MobileCard backgroundColor={surface} borderColor={border}>
<form onSubmit={handleSubmit}>
<YStack space="$3">
{oauthMessage ? (
<YStack
borderRadius={12}
padding="$2.5"
borderWidth={1}
borderColor={dangerText}
backgroundColor={dangerBg}
>
<Text fontSize="$sm" fontWeight="800" color={dangerText}>
{t('login.oauth_error_title', 'Login aktuell nicht moeglich')}
</Text>
<Text fontSize="$xs" color={dangerText}>
{oauthMessage}
</Text>
</YStack>
) : null}
<MobileField
label={t('login.email', 'E-Mail-Adresse')}
hint={t('login.email_hint', 'Dein Benutzername ist deine E-Mail-Adresse.')}
@@ -249,6 +297,29 @@ export default function MobileLoginPage() {
</Text>
</XStack>
</Button>
<Button
type="button"
onPress={handleGoogleLogin}
disabled={isRedirectingToGoogle || isSubmitting}
height={52}
borderRadius={16}
backgroundColor={surface}
borderColor={border}
borderWidth={1}
color={text}
>
<XStack alignItems="center" space="$2">
{isRedirectingToGoogle ? (
<Loader2 size={16} className="animate-spin" />
) : (
<GoogleIcon size={18} />
)}
<Text fontSize="$sm" color={text} fontWeight="700">
{t('login.google_cta', 'Mit Google anmelden')}
</Text>
</XStack>
</Button>
</YStack>
</form>
</MobileCard>
@@ -283,3 +354,14 @@ export default function MobileLoginPage() {
</YStack>
);
}
function GoogleIcon({ size = 18 }: { size?: number }) {
return (
<svg width={size} height={size} viewBox="0 0 24 24" aria-hidden>
<path
fill="#EA4335"
d="M12 11.999v4.8h6.7c-.3 1.7-2 4.9-6.7 4.9-4 0-7.4-3.3-7.4-7.4S8 6 12 6c2.3 0 3.9 1 4.8 1.9l3.3-3.2C18.1 2.6 15.3 1 12 1 5.9 1 1 5.9 1 12s4.9 11 11 11c6.3 0 10.5-4.4 10.5-10.6 0-.7-.1-1.2-.2-1.8H12z"
/>
</svg>
);
}

View File

@@ -0,0 +1,123 @@
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
let locationSearch = '?return_to=encoded-value';
const navigateMock = vi.fn();
vi.mock('react-router-dom', () => ({
useNavigate: () => navigateMock,
useLocation: () => ({ search: locationSearch }),
}));
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string, fallback?: string | Record<string, unknown>) => {
if (typeof fallback === 'string') {
return fallback;
}
if (fallback && typeof fallback === 'object' && typeof fallback.defaultValue === 'string') {
return fallback.defaultValue;
}
return key;
},
}),
}));
vi.mock('@tanstack/react-query', () => ({
useMutation: () => ({ mutate: vi.fn(), isPending: false }),
}));
vi.mock('../../auth/context', () => ({
useAuth: () => ({ status: 'unauthenticated', applyToken: vi.fn(), abilities: [] }),
}));
vi.mock('../../lib/returnTo', () => ({
resolveReturnTarget: () => ({ finalTarget: '/event-admin/mobile/dashboard', encodedFinal: 'encoded-value' }),
}));
vi.mock('../hooks/useInstallPrompt', () => ({
useInstallPrompt: () => ({
isInstalled: false,
isStandalone: false,
canInstall: false,
isIos: false,
promptInstall: vi.fn(),
}),
}));
vi.mock('../lib/installBanner', () => ({
getInstallBannerDismissed: () => false,
setInstallBannerDismissed: vi.fn(),
shouldShowInstallBanner: () => ({ shouldShow: false }),
}));
vi.mock('../components/MobileInstallBanner', () => ({
MobileInstallBanner: () => null,
}));
vi.mock('@tamagui/stacks', () => ({
YStack: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
XStack: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));
vi.mock('@tamagui/text', () => ({
SizableText: ({ children }: { children: React.ReactNode }) => <span>{children}</span>,
}));
vi.mock('@tamagui/button', () => ({
Button: ({ children, onPress, onClick, ...rest }: { children: React.ReactNode; onPress?: () => void; onClick?: () => void }) => (
<button type="button" onClick={onPress ?? onClick} {...rest}>
{children}
</button>
),
}));
vi.mock('../components/Primitives', () => ({
MobileCard: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));
vi.mock('../components/FormControls', () => ({
MobileField: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
MobileInput: (props: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />,
}));
vi.mock('../theme', () => ({
ADMIN_GRADIENTS: { loginBackground: 'linear-gradient(0deg, #000, #111)' },
useAdminTheme: () => ({
text: '#111827',
muted: '#6b7280',
primary: '#ff5a5f',
dangerBg: '#fee2e2',
dangerText: '#991b1b',
border: '#e5e7eb',
surface: '#ffffff',
}),
}));
vi.mock('lucide-react', () => ({
Loader2: () => <span>loader</span>,
}));
import MobileLoginPage from '../LoginPage';
describe('MobileLoginPage', () => {
it('renders Google login button', () => {
locationSearch = '?return_to=encoded-value';
render(<MobileLoginPage />);
const googleButton = screen.getByText('Mit Google anmelden');
expect(googleButton).toBeInTheDocument();
});
it('shows oauth error details when present', () => {
locationSearch = '?error=google_failed&error_description=Login%20failed';
render(<MobileLoginPage />);
expect(screen.getByText('Login aktuell nicht moeglich')).toBeInTheDocument();
expect(screen.getByText('Login failed')).toBeInTheDocument();
});
});