Publish container image / build-and-push (push) Canceled after 0s
- POST /api/external/session: bridge auth via X-API-Key, JIT user provisioning - GET /api/external/health: capability report - /magic frontend route: consumes ?token= JWT, redirects - docs/EXTERNAL-API.md: bridge contract spec
78 lines
2.5 KiB
TypeScript
78 lines
2.5 KiB
TypeScript
/**
|
|
* MagicLogin — consumes `?token=<JWT>` on ANY route.
|
|
*
|
|
* Bridges (AYON addon, Nextcloud app, ...) send users to:
|
|
* https://<refboard>/<any-path>?token=<jwt>
|
|
*
|
|
* 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<string | null>(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 (
|
|
<div style={{
|
|
display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
|
|
height: '100vh', background: '#1a1a1a', color: '#e0e0e0', fontSize: '16px', gap: '12px',
|
|
}}>
|
|
<div>Magic-Login fehlgeschlagen: {error}</div>
|
|
<button
|
|
onClick={() => navigate('/login', { replace: true })}
|
|
style={{
|
|
padding: '8px 16px', background: '#2d2d2d', color: '#e0e0e0',
|
|
border: '1px solid #444', borderRadius: '6px', cursor: 'pointer',
|
|
}}
|
|
>
|
|
Zum Login
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div style={{
|
|
display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
height: '100vh', background: '#1a1a1a', color: '#e0e0e0', fontSize: '18px',
|
|
}}>
|
|
Anmelden...
|
|
</div>
|
|
);
|
|
}
|