Added login redirects

This commit is contained in:
MrFry 2020-04-08 13:37:54 +02:00
parent a03f56028a
commit bcf377ad56
3 changed files with 86 additions and 25 deletions

View file

@ -5,14 +5,16 @@ const dbtools = require('../utils/dbtools.js')
module.exports = function (options) { module.exports = function (options) {
const { userDB, jsonResponse, exceptions } = options const { userDB, jsonResponse, exceptions } = options
const renderLogin = (res) => { const renderLogin = (req, res) => {
if (jsonResponse) { if (jsonResponse) {
res.json({ res.json({
result: 'nouser', result: 'nouser',
msg: 'You are not logged in' msg: 'You are not logged in'
}) })
} else { } else {
res.render('login') res.render('login', {
redirect: 'https://' + req.hostname + req.url
})
} }
} }
@ -37,7 +39,7 @@ module.exports = function (options) {
if (!sessionID) { if (!sessionID) {
logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1) logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1)
renderLogin(res) renderLogin(req, res)
return return
} }
@ -45,7 +47,7 @@ module.exports = function (options) {
if (!user) { if (!user) {
logger.DebugLog(`No user:${req.url}`, 'auth', 1) logger.DebugLog(`No user:${req.url}`, 'auth', 1)
renderLogin(res) renderLogin(req, res)
return return
} }

View file

@ -225,6 +225,7 @@ function GetApp () {
app.post('/login', (req, res) => { app.post('/login', (req, res) => {
logger.LogReq(req) logger.LogReq(req)
const pw = req.body.pw const pw = req.body.pw
const redirectTo = req.body.redirect
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
const user = dbtools.Select(userDB, 'users', { const user = dbtools.Select(userDB, 'users', {
pw: pw pw: pw
@ -268,18 +269,25 @@ function GetApp () {
sameSite: 'none' sameSite: 'none'
}) })
if (redirectTo) {
res.redirect(redirectTo)
} else {
res.json({ res.json({
result: 'success', result: 'success',
sessionID: sessionID msg: 'you are now logged in'
}) })
}
logger.Log(`Successfull login with user ID: #${user.id}`, logger.GetColor('cyan')) logger.Log(`Successfull login with user ID: #${user.id}`, logger.GetColor('cyan'))
} else { } else {
logger.Log(`Login attempt with invalid pw: ${pw}`, logger.GetColor('cyan')) logger.Log(`Login attempt with invalid pw: ${pw}`, logger.GetColor('cyan'))
if (redirectTo) {
res.redirect(redirectTo) // TODO
} else {
res.json({ res.json({
msg: 'invalid pw' msg: 'invalid pw'
}) })
} }
}
}) })
app.post('/logout', (req, res) => { app.post('/logout', (req, res) => {

View file

@ -1,34 +1,85 @@
<html> <html>
<body bgcolor="#212127"> <body bgcolor="#212127">
<head> <head>
<title>login</title> <title>Frylabs</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=0.6" /> <meta name="viewport" content="width=device-width, initial-scale=0.6" />
<style> <style>
.text {
color: white; .center {
width: 340px;
height: 340px;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
max-width: 100%;
max-height: 100%;
overflow: auto;
text-align: center;
} }
.title { .title {
font: normal 28px Verdana; font-size: 50px;
font-weight: bold; font-weight: bold;
color: white; color: white;
} }
.inputContainer {
width: 100%;
}
.showpwContainer {
color: white;
width: 40px;
cursor: pointer;
}
input[type=text], input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 2px solid #fff;
text-align: center;
}
input[type=text], input[type=password]:focus {
border: 2px solid #000;
}
input[type=button], input[type=submit], input[type=reset] {
background-color: #9999ff;
border: none;
color: white;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
</style> </style>
</head> </head>
<center> <div class='center'>
<h2 class='title'> <div class='title'>
Frylabs Login Frylabs
</h2>
<div class='text'>
Jelszó:
</div> </div>
<form action="http://api.frylabs.net/login" method="POST"> <form action="http://api.frylabs.net/login" method="POST">
<input type='text' id='pw' name='pw' /> <div class='inputContainer'>
<input type='password' id='pw' name='pw' autocomplete="off"/>
<!-- <span onclick="ShowHidePW()" class='showpwContainer'>
👁
</span> -->
</div>
<input type='hidden' name='redirect' value=<%= redirect %> autocomplete="off"/>
<input type='submit' value='Submit' formmethod='post' /> <input type='submit' value='Submit' formmethod='post' />
</form> </form>
</center> </div>
</body> </body>
<script> <script>
function ShowHidePW() {
const x = document.getElementById('pw')
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
</script> </script>
</html> </html>