mirror of
https://gitlab.com/Alamantus/Readlebee.git
synced 2025-04-17 17:10:10 +02:00
Add super basic fake login pages and conditions
This commit is contained in:
parent
621ee61144
commit
f05ff1e0ef
7 changed files with 93 additions and 46 deletions
29
routes/account.js
Normal file
29
routes/account.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
async function routes(fastify, options) {
|
||||
fastify.get('/login', async (request, reply) => {
|
||||
reply.view('login.hbs', { text: request.isLoggedInUser ? JSON.stringify(fastify.jwt.decode(request.cookies.token)) : 'you are NOT logged in' });
|
||||
});
|
||||
|
||||
fastify.post('/login-validate', async (request, reply) => {
|
||||
if (typeof request.body.email === "undefined" || typeof request.body.password === "undefined") {
|
||||
reply.redirect('/login', 400);
|
||||
}
|
||||
|
||||
const token = fastify.jwt.sign({ email: request.body.email, password: request.body.password });
|
||||
const expireTime = fastify.siteConfig.tokenExpireDays * (24 * 60 * 60e3); // The section in parentheses is milliseconds in a day
|
||||
reply
|
||||
.setCookie('token', token, {
|
||||
path: '/',
|
||||
expires: new Date(Date.now() + expireTime),
|
||||
maxAge: new Date(Date.now() + expireTime), // Both are set as a "just in case"
|
||||
httpOnly: true, // Prevents JavaScript on the front end from grabbing it
|
||||
sameSite: true, // Prevents the cookie from being used outside of this site
|
||||
})
|
||||
.redirect('/', 200);
|
||||
});
|
||||
|
||||
fastify.get('/logout', async (request, reply) => {
|
||||
reply.clearCookie('token', { path: '/' }).redirect('/?loggedout');
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = routes
|
|
@ -1,24 +1,16 @@
|
|||
async function routes(fastify, options) {
|
||||
fastify.get('/', async (request, reply) => {
|
||||
reply.view('home.hbs', { text: request.isLoggedInUser ? JSON.stringify(fastify.jwt.decode(request.cookies.token)) : 'you are NOT logged in' });
|
||||
});
|
||||
|
||||
fastify.get('/loggedin', async (request, reply) => {
|
||||
const token = fastify.jwt.sign({ loggedin: true });
|
||||
const expireTime = fastify.siteConfig.tokenExpireDays * (24 * 60 * 60e3); // The section in parentheses is milliseconds in a day
|
||||
reply
|
||||
.setCookie('token', token, {
|
||||
path: '/',
|
||||
expires: new Date(Date.now() + expireTime),
|
||||
maxAge: new Date(Date.now() + expireTime), // Both are set as a "just in case"
|
||||
httpOnly: true, // Prevents JavaScript on the front end from grabbing it
|
||||
sameSite: true, // Prevents the cookie from being used outside of this site
|
||||
})
|
||||
.view('home-logged-in.hbs', { statuses: [{ title: 'books' }, { title: 'fun' }] });
|
||||
});
|
||||
|
||||
fastify.get('/loggedout', async (request, reply) => {
|
||||
reply.clearCookie('token', { path: '/' }).redirect('/');
|
||||
const viewData = {};
|
||||
if (typeof request.query.loggedOut !== 'undefined') {
|
||||
viewData.message = 'You have been logged out';
|
||||
} else {
|
||||
viewData.message = request.isLoggedInUser ? JSON.stringify(fastify.jwt.decode(request.cookies.token)) : 'you are NOT logged in';
|
||||
}
|
||||
if (request.isLoggedInUser) {
|
||||
viewData.loggedIn = true;
|
||||
viewData.statuses = [{ title: 'books' }, { title: 'fun' }];
|
||||
}
|
||||
reply.view('home.hbs', viewData);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ fastify.addHook('onRequest', (request, reply, done) => {
|
|||
// Routes
|
||||
fastify.register(require('./routes/resources'));
|
||||
fastify.register(require('./routes/home'));
|
||||
fastify.register(require('./routes/account'));
|
||||
fastify.register(require('./routes/search'));
|
||||
|
||||
// Start the server
|
||||
|
|
|
@ -2,28 +2,18 @@
|
|||
|
||||
{{#*inline "page-content-block" }}
|
||||
<section>
|
||||
<h2 class="subtitle">An attempt at a viable alternative to Goodreads</h2>
|
||||
|
||||
<article class="flex two">
|
||||
<div class="half">
|
||||
<div class="card">
|
||||
<header>
|
||||
<p>Still gotta figure out a design.</p>
|
||||
</header>
|
||||
</div>
|
||||
</div>
|
||||
<div class="half">
|
||||
<div class="card">
|
||||
<header>
|
||||
<p>It's early days, my friends!</p>
|
||||
</header>
|
||||
</div>
|
||||
</div>
|
||||
{{#if message }}
|
||||
<article class="card" style="background-color:cornflowerblue;color:white;width:100%;">
|
||||
{{ message }}
|
||||
</article>
|
||||
{{/if}}
|
||||
|
||||
<article class="test">
|
||||
{{ text }}
|
||||
</article>
|
||||
{{#if loggedIn }}
|
||||
{{> home-logged-in }}
|
||||
{{ else }}
|
||||
{{> home-logged-out }}
|
||||
{{/if}}
|
||||
|
||||
</section>
|
||||
{{/inline}}
|
||||
|
||||
|
|
23
views/login.hbs
Normal file
23
views/login.hbs
Normal file
|
@ -0,0 +1,23 @@
|
|||
{{#> layout }}
|
||||
|
||||
{{#*inline "page-content-block" }}
|
||||
<section>
|
||||
|
||||
<article class="card">
|
||||
<form action="/login-validate" method="POST">
|
||||
<label>
|
||||
<span>Email</span>
|
||||
<input type="email" name="email">
|
||||
</label>
|
||||
<label>
|
||||
<span>Password</span>
|
||||
<input type="password" name="password">
|
||||
</label>
|
||||
<input type="submit" value="Log In!">
|
||||
</form>
|
||||
</article>
|
||||
|
||||
</section>
|
||||
{{/inline}}
|
||||
|
||||
{{/layout}}
|
|
@ -1,6 +1,3 @@
|
|||
{{#> layout }}
|
||||
|
||||
{{#*inline "page-content-block" }}
|
||||
<section>
|
||||
<h2 class="subtitle">You are logged in!</h2>
|
||||
|
||||
|
@ -16,7 +13,4 @@
|
|||
</article>
|
||||
{{/each}}
|
||||
|
||||
</section>
|
||||
{{/inline}}
|
||||
|
||||
{{/layout}}
|
||||
</section>
|
18
views/partials/home-logged-out.hbs
Normal file
18
views/partials/home-logged-out.hbs
Normal file
|
@ -0,0 +1,18 @@
|
|||
<h2 class="subtitle">An attempt at a viable alternative to Goodreads</h2>
|
||||
|
||||
<article class="flex two">
|
||||
<div class="half">
|
||||
<div class="card">
|
||||
<header>
|
||||
<p>Still gotta figure out a design.</p>
|
||||
</header>
|
||||
</div>
|
||||
</div>
|
||||
<div class="half">
|
||||
<div class="card">
|
||||
<header>
|
||||
<p>It's early days, my friends!</p>
|
||||
</header>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
Loading…
Add table
Reference in a new issue