/** * MagicLogin — consumes `?token=` on ANY route. * * Bridges (AYON addon, Nextcloud app, ...) send users to: * https:///?token= * * This component validates the token against /api/auth/me, stores it, * and redirects to the destination path (also given via `redirect` query * param, default `/`). */ import React, { useEffect, useState } from 'react'; import { useNavigate, useSearchParams, useLocation } from 'react-router-dom'; import { useAuth } from '../auth'; import { getMe } from '../api'; export default function MagicLogin() { const [params] = useSearchParams(); const location = useLocation(); const navigate = useNavigate(); const { login } = useAuth(); const [error, setError] = useState(null); useEffect(() => { const token = params.get('token'); if (!token) { setError('No token provided'); return; } // Temporarily store so getMe() picks it up (it reads localStorage) localStorage.setItem('refboard_token', token); getMe() .then((res) => { const user = res.data.user || res.data; login(token, user); const redirect = params.get('redirect') || '/'; navigate(redirect, { replace: true }); }) .catch(() => { localStorage.removeItem('refboard_token'); setError('Invalid or expired token'); }); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // If we're mounted on a /board/:id path, keep that path as redirect target const redirectPath = location.pathname !== '/magic' ? location.pathname : params.get('redirect') || '/'; if (error) { return (
Magic-Login fehlgeschlagen: {error}
); } return (
Anmelden...
); }