mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Login/logout process.
This commit is contained in:
parent
0f0b1e39e7
commit
f947c7770b
10 changed files with 91 additions and 58 deletions
|
|
@ -1,36 +0,0 @@
|
|||
import React from 'react';
|
||||
import cookies from 'next-cookies';
|
||||
import Layout from 'components/Layout';
|
||||
import { verifySecureToken } from 'lib/crypto';
|
||||
|
||||
export default function Admin({ username }) {
|
||||
return (
|
||||
<Layout title="Admin">
|
||||
<h2>
|
||||
You've successfully logged in as <b>{username}</b>.
|
||||
</h2>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export async function getServerSideProps(context) {
|
||||
const token = cookies(context)['umami.auth'];
|
||||
|
||||
try {
|
||||
const payload = await verifySecureToken(token);
|
||||
|
||||
return {
|
||||
props: {
|
||||
username: payload.username,
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
const { res } = context;
|
||||
|
||||
res.statusCode = 303;
|
||||
res.setHeader('Location', '/');
|
||||
res.end();
|
||||
}
|
||||
|
||||
return { props: {} };
|
||||
}
|
||||
|
|
@ -1,11 +1,8 @@
|
|||
import { serialize } from 'cookie';
|
||||
import { checkPassword, createSecureToken } from 'lib/crypto';
|
||||
import { getAccount } from 'lib/db';
|
||||
import { allowPost } from 'lib/middleware';
|
||||
|
||||
export default async (req, res) => {
|
||||
await allowPost(req, res);
|
||||
|
||||
const { username, password } = req.body;
|
||||
|
||||
const account = await getAccount(username);
|
||||
|
|
@ -21,7 +18,7 @@ export default async (req, res) => {
|
|||
|
||||
res.setHeader('Set-Cookie', [cookie]);
|
||||
|
||||
res.status(200).send({ token });
|
||||
res.status(200).json({ token });
|
||||
} else {
|
||||
res.status(401).end();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { savePageView, saveEvent } from 'lib/db';
|
||||
import { allowPost } from 'lib/middleware';
|
||||
import { useCors } from 'lib/middleware';
|
||||
import checkSession from 'lib/session';
|
||||
import { createToken } from 'lib/crypto';
|
||||
|
||||
export default async (req, res) => {
|
||||
await allowPost(req, res);
|
||||
await useCors(req, res);
|
||||
|
||||
const session = await checkSession(req);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ export default async (req, res) => {
|
|||
|
||||
try {
|
||||
const payload = await verifySecureToken(token);
|
||||
res.status(200).send(payload);
|
||||
res.status(200).json(payload);
|
||||
} catch {
|
||||
res.status(400).end();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,40 @@
|
|||
import React from 'react';
|
||||
import Link from 'next/link';
|
||||
import cookies from 'next-cookies';
|
||||
import Layout from 'components/Layout';
|
||||
import Login from 'components/Login';
|
||||
import { verifySecureToken } from 'lib/crypto';
|
||||
|
||||
export default function Home() {
|
||||
export default function HomePage({ username }) {
|
||||
return (
|
||||
<Layout>
|
||||
<Login />
|
||||
<p>
|
||||
<Link href="/test">
|
||||
<a>Test page 🡒</a>
|
||||
</Link>
|
||||
</p>
|
||||
<h2>
|
||||
You've successfully logged in as <b>{username}</b>.
|
||||
</h2>
|
||||
<Link href="/logout">
|
||||
<a>Logout 🡒</a>
|
||||
</Link>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export async function getServerSideProps(context) {
|
||||
const token = cookies(context)['umami.auth'];
|
||||
|
||||
try {
|
||||
const payload = await verifySecureToken(token);
|
||||
|
||||
return {
|
||||
props: {
|
||||
username: payload.username,
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
const { res } = context;
|
||||
|
||||
res.statusCode = 303;
|
||||
res.setHeader('Location', '/login');
|
||||
res.end();
|
||||
}
|
||||
|
||||
return { props: {} };
|
||||
}
|
||||
|
|
|
|||
17
pages/login.js
Normal file
17
pages/login.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import React from 'react';
|
||||
import Link from 'next/link';
|
||||
import Layout from 'components/Layout';
|
||||
import Login from 'components/Login';
|
||||
|
||||
export default function LoginPage() {
|
||||
return (
|
||||
<Layout title="Login">
|
||||
<Login />
|
||||
<p>
|
||||
<Link href="/test">
|
||||
<a>Test page 🡒</a>
|
||||
</Link>
|
||||
</p>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
27
pages/logout.js
Normal file
27
pages/logout.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import React from 'react';
|
||||
import { serialize } from 'cookie';
|
||||
import Layout from 'components/Layout';
|
||||
|
||||
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: {} };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue