working login and register page
This commit is contained in:
@@ -9,6 +9,7 @@ import Article from './components/Article';
|
|||||||
import Latest from './components/Latest';
|
import Latest from './components/Latest';
|
||||||
import Footer from './components/Footer';
|
import Footer from './components/Footer';
|
||||||
import Login from './components/Login';
|
import Login from './components/Login';
|
||||||
|
import Register from './components/Register';
|
||||||
import { useArticles } from './providers/Article';
|
import { useArticles } from './providers/Article';
|
||||||
import { useAuth } from './providers/Author';
|
import { useAuth } from './providers/Author';
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
|
|||||||
|
|
||||||
const [selectedArticle, setSelectedArticle] = React.useState<number | null>(null);
|
const [selectedArticle, setSelectedArticle] = React.useState<number | null>(null);
|
||||||
const [showLogin, setShowLogin] = React.useState(false);
|
const [showLogin, setShowLogin] = React.useState(false);
|
||||||
|
const [showRegister, setShowRegister] = React.useState(false);
|
||||||
|
|
||||||
const handleSelectArticle = (index: number) => {
|
const handleSelectArticle = (index: number) => {
|
||||||
setSelectedArticle(index);
|
setSelectedArticle(index);
|
||||||
@@ -87,16 +89,35 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
|
|||||||
flexDirection: 'column',
|
flexDirection: 'column',
|
||||||
my: 4,
|
my: 4,
|
||||||
gap: 4,
|
gap: 4,
|
||||||
pb: selectedArticle === null && !showLogin ? 24 : 0,
|
pb: selectedArticle === null && !showLogin && !showRegister ? 24 : 0, // ✅ adjusted footer padding
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{showLogin ? (
|
{showRegister ? (
|
||||||
<Login onBack={handleHideLogin} />
|
<Register
|
||||||
) : selectedArticle === null ? (
|
onBack={() => {
|
||||||
|
setShowRegister(false);
|
||||||
|
setShowLogin(false);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : showLogin ? (
|
||||||
|
<Login
|
||||||
|
onBack={() => setShowLogin(false)}
|
||||||
|
onRegister={() => {
|
||||||
|
setShowLogin(false);
|
||||||
|
setShowRegister(true);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : selectedArticle !== null ? (
|
||||||
|
<Article article={articles[selectedArticle]} onBack={handleBack} />
|
||||||
|
) : (
|
||||||
<>
|
<>
|
||||||
{!currentUser && (
|
{!currentUser && (
|
||||||
<Box sx={{ display: 'flex', justifyContent: 'flex-end', mb: 2 }}>
|
<Box sx={{ display: 'flex', justifyContent: 'flex-end', mb: 2 }}>
|
||||||
<Button variant="outlined" color="primary" onClick={handleShowLogin}>
|
<Button
|
||||||
|
variant="outlined"
|
||||||
|
color="primary"
|
||||||
|
onClick={() => setShowLogin(true)}
|
||||||
|
>
|
||||||
Login
|
Login
|
||||||
</Button>
|
</Button>
|
||||||
</Box>
|
</Box>
|
||||||
@@ -109,8 +130,6 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
|
|||||||
onLoadMore={async () => {}}
|
onLoadMore={async () => {}}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
) : (
|
|
||||||
<Article article={articles[selectedArticle]} onBack={handleBack} />
|
|
||||||
)}
|
)}
|
||||||
</Container>
|
</Container>
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Box, TextField, Button, Typography, IconButton, CircularProgress } from '@mui/material';
|
import { Box, TextField, Button, Typography, IconButton, CircularProgress, Link } from '@mui/material';
|
||||||
import ArrowBackRoundedIcon from '@mui/icons-material/ArrowBackRounded';
|
import ArrowBackRoundedIcon from '@mui/icons-material/ArrowBackRounded';
|
||||||
import { useAuth } from '../providers/Author';
|
import { useAuth } from '../providers/Author';
|
||||||
|
|
||||||
interface LoginProps {
|
interface LoginProps {
|
||||||
onBack: () => void;
|
onBack: () => void;
|
||||||
|
onRegister: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Login({ onBack }: LoginProps) {
|
export default function Login({ onBack, onRegister }: LoginProps) {
|
||||||
const { login, loading, error, currentUser } = useAuth();
|
const { login, loading, error, currentUser } = useAuth();
|
||||||
const [username, setUsername] = React.useState('');
|
const [username, setUsername] = React.useState('');
|
||||||
const [password, setPassword] = React.useState('');
|
const [password, setPassword] = React.useState('');
|
||||||
@@ -17,11 +18,10 @@ export default function Login({ onBack }: LoginProps) {
|
|||||||
await login(username, password);
|
await login(username, password);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (currentUser) {
|
// ✅ Auto-return if already logged in
|
||||||
// ✅ if logged in, auto-return to the article list
|
React.useEffect(() => {
|
||||||
onBack();
|
if (currentUser) onBack();
|
||||||
return null;
|
}, [currentUser]);
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
@@ -84,6 +84,24 @@ export default function Login({ onBack }: LoginProps) {
|
|||||||
{loading ? <CircularProgress size={24} color="inherit" /> : 'Login'}
|
{loading ? <CircularProgress size={24} color="inherit" /> : 'Login'}
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<Typography
|
||||||
|
variant="body2"
|
||||||
|
color="text.secondary"
|
||||||
|
align="center"
|
||||||
|
sx={{ mt: 3 }}
|
||||||
|
>
|
||||||
|
Don’t have an account?{' '}
|
||||||
|
<Link
|
||||||
|
component="button"
|
||||||
|
underline="hover"
|
||||||
|
color="primary"
|
||||||
|
onClick={onRegister}
|
||||||
|
sx={{ fontWeight: 500 }}
|
||||||
|
>
|
||||||
|
Register
|
||||||
|
</Link>
|
||||||
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user