mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Implement redux.
This commit is contained in:
parent
9d8a2406e1
commit
5d4ff5cfa4
31 changed files with 341 additions and 85 deletions
|
|
@ -1,7 +1,15 @@
|
|||
import React from 'react';
|
||||
import { Provider } from 'react-redux';
|
||||
import { useStore } from 'redux/store';
|
||||
import 'styles/bootstrap-grid.css';
|
||||
import 'styles/index.css';
|
||||
|
||||
export default function App({ Component, pageProps }) {
|
||||
return <Component {...pageProps} />;
|
||||
const store = useStore();
|
||||
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<Component {...pageProps} />
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { serialize } from 'cookie';
|
||||
import { checkPassword, createSecureToken } from 'lib/crypto';
|
||||
import { getAccount } from 'lib/db';
|
||||
import { AUTH_COOKIE_NAME } from 'lib/constants';
|
||||
|
||||
export default async (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
|
|
@ -10,7 +11,7 @@ export default async (req, res) => {
|
|||
if (account && (await checkPassword(password, account.password))) {
|
||||
const { user_id, username, is_admin } = account;
|
||||
const token = await createSecureToken({ user_id, username, is_admin });
|
||||
const cookie = serialize('umami.auth', token, {
|
||||
const cookie = serialize(AUTH_COOKIE_NAME, token, {
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
maxAge: 60 * 60 * 24 * 365,
|
||||
16
pages/api/auth/logout.js
Normal file
16
pages/api/auth/logout.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { serialize } from 'cookie';
|
||||
import { AUTH_COOKIE_NAME } from 'lib/constants';
|
||||
|
||||
export default async (req, res) => {
|
||||
const cookie = serialize(AUTH_COOKIE_NAME, '', {
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
maxAge: 0,
|
||||
});
|
||||
|
||||
res.statusCode = 303;
|
||||
res.setHeader('Set-Cookie', [cookie]);
|
||||
res.setHeader('Location', '/login');
|
||||
|
||||
return res.end();
|
||||
};
|
||||
11
pages/api/auth/verify.js
Normal file
11
pages/api/auth/verify.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { useAuth } from 'lib/middleware';
|
||||
|
||||
export default async (req, res) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
if (req.auth) {
|
||||
return res.status(200).json(req.auth);
|
||||
}
|
||||
|
||||
return res.status(401).end();
|
||||
};
|
||||
|
|
@ -1,33 +1,18 @@
|
|||
import React from 'react';
|
||||
import { parse } from 'cookie';
|
||||
import Layout from 'components/Layout';
|
||||
import { verifySecureToken } from 'lib/crypto';
|
||||
import WebsiteList from '../components/WebsiteList';
|
||||
import WebsiteList from 'components/WebsiteList';
|
||||
import useUser from 'hooks/useUser';
|
||||
|
||||
export default function HomePage() {
|
||||
const { loading } = useUser();
|
||||
|
||||
if (loading) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export default function HomePage({ username }) {
|
||||
return (
|
||||
<Layout>
|
||||
<WebsiteList />
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export async function getServerSideProps({ req, res }) {
|
||||
const token = parse(req.headers.cookie || '')['umami.auth'];
|
||||
|
||||
try {
|
||||
const payload = await verifySecureToken(token);
|
||||
|
||||
return {
|
||||
props: {
|
||||
...payload,
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
res.statusCode = 303;
|
||||
res.setHeader('Location', '/login');
|
||||
res.end();
|
||||
}
|
||||
|
||||
return { props: {} };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,9 @@
|
|||
import React from 'react';
|
||||
import { serialize } from 'cookie';
|
||||
import Layout from 'components/Layout';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export default function LogoutPage() {
|
||||
return (
|
||||
<Layout title="Logout">
|
||||
<h2>You've successfully logged out..</h2>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export async function getServerSideProps({ res }) {
|
||||
const cookie = serialize('umami.auth', '', {
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
maxAge: 0,
|
||||
});
|
||||
|
||||
res.statusCode = 303;
|
||||
res.setHeader('Set-Cookie', [cookie]);
|
||||
res.setHeader('Location', '/login');
|
||||
|
||||
res.end();
|
||||
|
||||
return { props: {} };
|
||||
useEffect(() => {
|
||||
fetch('/api/auth/logout').then(() => (window.location.href = '/login'));
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
18
pages/settings.js
Normal file
18
pages/settings.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import React from 'react';
|
||||
import Layout from 'components/Layout';
|
||||
import Settings from 'components/Settings';
|
||||
import useUser from 'hooks/useUser';
|
||||
|
||||
export default function SettingsPage() {
|
||||
const { loading } = useUser();
|
||||
|
||||
if (loading) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<Settings />
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
|
@ -1,13 +1,15 @@
|
|||
import React from 'react';
|
||||
import { useRouter } from 'next/router';
|
||||
import Layout from 'components/Layout';
|
||||
import WebsiteDetails from '../../components/WebsiteDetails';
|
||||
import WebsiteDetails from 'components/WebsiteDetails';
|
||||
import useUser from 'hooks/useUser';
|
||||
|
||||
export default function DetailsPage() {
|
||||
const { loading } = useUser();
|
||||
const router = useRouter();
|
||||
const { id } = router.query;
|
||||
|
||||
if (!id) {
|
||||
if (!id || loading) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue