This commit is contained in:
@@ -6,6 +6,7 @@ use axum::{
|
||||
use axum_login::AuthSession;
|
||||
use sea_orm::DatabaseConnection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tower_sessions::Session;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::auth::AuthBackend;
|
||||
@@ -243,6 +244,7 @@ pub async fn validate_invite_link(
|
||||
)]
|
||||
pub async fn join_family_via_invite(
|
||||
auth_session: AuthSession<AuthBackend>,
|
||||
session: Session,
|
||||
State(db): State<DatabaseConnection>,
|
||||
Path(token): Path<String>,
|
||||
) -> Result<Json<JoinFamilyResponse>, StatusCode> {
|
||||
@@ -266,6 +268,19 @@ pub async fn join_family_via_invite(
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
})?;
|
||||
|
||||
let mut authorized_families: Vec<i32> = session
|
||||
.get("authorized_families")
|
||||
.await
|
||||
.unwrap_or(None)
|
||||
.unwrap_or_default();
|
||||
if !authorized_families.contains(&invite.family_id) {
|
||||
authorized_families.push(invite.family_id);
|
||||
session
|
||||
.insert("authorized_families", authorized_families)
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
|
||||
Ok(Json(JoinFamilyResponse {
|
||||
success: true,
|
||||
family_id: invite.family_id,
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import { useEffect } from 'react';
|
||||
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
|
||||
import { BrowserRouter, Routes, Route, Navigate, useLocation } from 'react-router-dom';
|
||||
import Login from './pages/Login';
|
||||
import FamilyView from './pages/FamilyView';
|
||||
import AdminPanel from './pages/AdminPanel';
|
||||
import NoFamily from './pages/NoFamily';
|
||||
import InvitePage from './pages/InvitePage';
|
||||
import { useStore } from './store/useStore';
|
||||
import { authApi } from './api/client';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
|
||||
function AppContent() {
|
||||
const { user, isAuthenticated, isLoading, setUser, setIsLoading } = useStore();
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
checkAuth();
|
||||
@@ -26,6 +28,14 @@ function AppContent() {
|
||||
}
|
||||
};
|
||||
|
||||
if (location.pathname.startsWith('/invite/')) {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/invite/:token" element={<InvitePage />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center gradient-bg">
|
||||
|
||||
153
frontend/src/pages/InvitePage.tsx
Normal file
153
frontend/src/pages/InvitePage.tsx
Normal file
@@ -0,0 +1,153 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useParams, useNavigate } from 'react-router-dom';
|
||||
import { inviteLinkApi, authApi } from '../api/client';
|
||||
import { useStore } from '../store/useStore';
|
||||
import { Loader2, Users, UserPlus, AlertCircle } from 'lucide-react';
|
||||
|
||||
export default function InvitePage() {
|
||||
const { token } = useParams<{ token: string }>();
|
||||
const navigate = useNavigate();
|
||||
const { isAuthenticated, setUser } = useStore();
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [joining, setJoining] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [familyName, setFamilyName] = useState<string | null>(null);
|
||||
const [isValid, setIsValid] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (token) {
|
||||
validateInvite();
|
||||
}
|
||||
}, [token]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isAuthenticated && isValid && token) {
|
||||
joinFamily();
|
||||
}
|
||||
}, [isAuthenticated, isValid]);
|
||||
|
||||
const validateInvite = async () => {
|
||||
if (!token) return;
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await inviteLinkApi.validate(token);
|
||||
if (response.data.valid) {
|
||||
setIsValid(true);
|
||||
setFamilyName(response.data.family_name);
|
||||
} else {
|
||||
setError('Ссылка недействительна или срок её действия истёк');
|
||||
}
|
||||
} catch (err) {
|
||||
setError('Ссылка не найдена');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const joinFamily = async () => {
|
||||
if (!token) return;
|
||||
|
||||
try {
|
||||
setJoining(true);
|
||||
const response = await inviteLinkApi.join(token);
|
||||
if (response.data.success) {
|
||||
const meResponse = await authApi.me();
|
||||
setUser(meResponse.data);
|
||||
navigate(`/family/${response.data.family_id}`);
|
||||
} else {
|
||||
setError(response.data.message);
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (err.response?.status === 400) {
|
||||
setError('Вы уже состоите в семье');
|
||||
} else {
|
||||
setError('Ошибка при присоединении к семье');
|
||||
}
|
||||
} finally {
|
||||
setJoining(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleGoogleLogin = async () => {
|
||||
if (token) {
|
||||
localStorage.setItem('pendingInviteToken', token);
|
||||
}
|
||||
try {
|
||||
const response = await authApi.getGoogleAuthUrl(window.location.href);
|
||||
window.location.href = response.data.url;
|
||||
} catch (err) {
|
||||
setError('Ошибка при получении ссылки для авторизации');
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center gradient-bg">
|
||||
<div className="flex items-center gap-3 text-white">
|
||||
<Loader2 className="w-8 h-8 animate-spin" />
|
||||
<span className="text-xl font-medium">Проверка приглашения...</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (joining) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center gradient-bg">
|
||||
<div className="flex items-center gap-3 text-white">
|
||||
<Loader2 className="w-8 h-8 animate-spin" />
|
||||
<span className="text-xl font-medium">Присоединение к семье...</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center gradient-bg px-4">
|
||||
<div className="bg-white rounded-3xl shadow-2xl p-8 max-w-md w-full text-center">
|
||||
<div className="p-4 bg-red-100 rounded-2xl inline-block mb-6">
|
||||
<AlertCircle className="w-12 h-12 text-red-500" />
|
||||
</div>
|
||||
<h1 className="text-2xl font-bold text-gray-800 mb-4">Ошибка</h1>
|
||||
<p className="text-gray-600 mb-6">{error}</p>
|
||||
<button
|
||||
onClick={() => navigate('/')}
|
||||
className="px-6 py-3 bg-gradient-to-r from-purple-600 to-blue-600 text-white rounded-2xl font-semibold hover:shadow-xl transition-all"
|
||||
>
|
||||
На главную
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center gradient-bg px-4">
|
||||
<div className="bg-white rounded-3xl shadow-2xl p-8 max-w-md w-full text-center">
|
||||
<div className="p-4 bg-gradient-to-br from-purple-500 to-blue-500 rounded-2xl inline-block mb-6">
|
||||
<Users className="w-12 h-12 text-white" />
|
||||
</div>
|
||||
<h1 className="text-2xl font-bold text-gray-800 mb-2">
|
||||
Приглашение в семью
|
||||
</h1>
|
||||
<p className="text-3xl font-bold text-purple-600 mb-6">
|
||||
{familyName}
|
||||
</p>
|
||||
<p className="text-gray-600 mb-8">
|
||||
Вас пригласили присоединиться к семейному бюджету.
|
||||
Войдите через Google, чтобы принять приглашение.
|
||||
</p>
|
||||
<button
|
||||
onClick={handleGoogleLogin}
|
||||
className="w-full flex items-center justify-center gap-3 px-6 py-4 bg-gradient-to-r from-purple-600 to-blue-600 text-white rounded-2xl font-semibold hover:shadow-xl transition-all"
|
||||
>
|
||||
<UserPlus className="w-5 h-5" />
|
||||
Войти и присоединиться
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useState } from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useStore } from '../store/useStore';
|
||||
import { authApi, familyApi } from '../api/client';
|
||||
import { authApi, familyApi, inviteLinkApi } from '../api/client';
|
||||
import { Users, LogOut, Settings, Plus, Loader2, Eye, EyeOff } from 'lucide-react';
|
||||
|
||||
export default function NoFamily() {
|
||||
@@ -13,6 +13,38 @@ export default function NoFamily() {
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [joiningFamily, setJoiningFamily] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
checkPendingInvite();
|
||||
}, []);
|
||||
|
||||
const checkPendingInvite = async () => {
|
||||
const pendingToken = localStorage.getItem('pendingInviteToken');
|
||||
if (!pendingToken) return;
|
||||
|
||||
localStorage.removeItem('pendingInviteToken');
|
||||
|
||||
try {
|
||||
setJoiningFamily(true);
|
||||
const response = await inviteLinkApi.join(pendingToken);
|
||||
if (response.data.success) {
|
||||
const meResponse = await authApi.me();
|
||||
setUser(meResponse.data);
|
||||
navigate(`/family/${response.data.family_id}`);
|
||||
} else {
|
||||
setError(response.data.message);
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (err.response?.status === 400) {
|
||||
setError('Ссылка-приглашение недействительна или истекла');
|
||||
} else {
|
||||
setError('Ошибка при присоединении к семье');
|
||||
}
|
||||
} finally {
|
||||
setJoiningFamily(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
@@ -68,6 +100,17 @@ export default function NoFamily() {
|
||||
}
|
||||
};
|
||||
|
||||
if (joiningFamily) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center gradient-bg">
|
||||
<div className="flex items-center gap-3 text-white">
|
||||
<Loader2 className="w-8 h-8 animate-spin" />
|
||||
<span className="text-xl font-medium">Присоединение к семье...</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen gradient-bg flex items-center justify-center px-4">
|
||||
<div className="w-full max-w-md">
|
||||
|
||||
Reference in New Issue
Block a user