-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user authentication & registration implemented inside Dashboard
- Loading branch information
Showing
28 changed files
with
666 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const express = require("express"), | ||
router = express.Router(), | ||
logger = require("@logger"), | ||
config = require("@config"), | ||
store = require("@store"); | ||
|
||
function signin(req, res) { | ||
req.signin(function (err, user, info) { | ||
if (user) { | ||
return res.json({}); | ||
} | ||
return res.status(400).json(info); | ||
}); | ||
} | ||
|
||
async function refreshRegistrationEnabled(app) { | ||
let registrationEnabled = config.registrationMode === "open" || await store.users.count() === 0; | ||
app.set("registration-enabled", registrationEnabled); | ||
} | ||
|
||
router.get("/", function (req, res) { | ||
return res.json({ | ||
isAuthenticated: req.isAuthenticated(), | ||
registrationEnabled: req.app.get("registration-enabled") | ||
}); | ||
}); | ||
|
||
router.get("/check-email/:email", async function (req, res) { | ||
var email = req.params.email, | ||
user = await store.users.getByEmail(email); | ||
return res.json(!user); | ||
}); | ||
|
||
router.get("/check-username/:username", async function (req, res) { | ||
var username = req.params.username, | ||
user = await store.users.getByUsername(username); | ||
return res.json(!user); | ||
}); | ||
|
||
router.post("/signin", signin); | ||
|
||
router.post("/register", async function (req, res) { | ||
if (req.isAuthenticated()) { | ||
return res.status(400).json({ message: "User is authenticated"}); | ||
} | ||
|
||
if (!req.app.get("registration-enabled")) { | ||
return res.sendStatus(404); | ||
} | ||
|
||
let name = req.body.name, | ||
email = req.body.email, | ||
username = req.body.username, | ||
password = req.body.password, | ||
passwordConfirm = req.body.passwordConfirm; | ||
|
||
logger.info(`Register new user ${name} (${email})`); | ||
|
||
if (password !== passwordConfirm) { | ||
return res.status(400).json({ | ||
message: "Password and confirm password does not match", | ||
}); | ||
} | ||
|
||
// check username | ||
if (await store.users.getByUsername(username)) { | ||
return res.status(400).json({ message: "This username is already taken" }); | ||
} | ||
|
||
// check email | ||
if (await store.users.getByEmail(email)) { | ||
return res.status(400).json({ message: "This email is already taken" }); | ||
} | ||
|
||
let security = require("@lib/security"); | ||
let user = { | ||
name, | ||
email, | ||
username | ||
}; | ||
|
||
let usersCount = await store.users.count(); | ||
if (usersCount === 0) { | ||
user.roles = ["owner"]; | ||
} | ||
|
||
switch (config.passwordHashAlgorithm) { | ||
case "md5": | ||
user.password = security.md5(password); | ||
await store.users.insert(user); | ||
refreshRegistrationEnabled(req.app); | ||
signin(req, res); | ||
break; | ||
case "bcrypt": | ||
security.bcryptHash(password, async function (err, passwordHash) { | ||
user.password = passwordHash; | ||
await store.users.insert(user); | ||
refreshRegistrationEnabled(req.app); | ||
signin(req, res); | ||
}); | ||
break; | ||
default: | ||
logger.error("Incorrect passwordHashAlgorithm specified in config.json"); | ||
break; | ||
} | ||
}); | ||
|
||
router.get("/signout", function (req, res) { | ||
req.signout(); | ||
req.session.destroy(); | ||
return res.json(true); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width,initial-scale=1.0"> | ||
<link rel="icon" href="favicon.ico"> | ||
<title>Unknown Error</title> | ||
</head> | ||
<style> | ||
* { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
html, body { | ||
height: 100%; | ||
} | ||
body { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
img { | ||
max-width: 80%; | ||
max-height: 80%; | ||
margin: 0 auto 20px; | ||
} | ||
h1 { | ||
font-family: 'Roboto',-apple-system,BlinkMacSystemFont,'Segoe UI','Oxygen','Ubuntu','Fira Sans','Droid Sans','Helvetica Neue',sans-serif; | ||
font-size: 22px; | ||
text-align: center; | ||
} | ||
</style> | ||
<body> | ||
<img src="img/error.svg" /> | ||
<h1>Unknown error occurred</h1> | ||
<div id="app"></div> | ||
<!-- built files will be auto injected --> | ||
</body> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.