Add Google login to mobile admin PWA
This commit is contained in:
@@ -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>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user