Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ app.locals.title = `${capitalized(projectName)}- Generated with Ironlauncher`;
const index = require('./routes/index');
app.use('/', index);

app.use('/', require("./routes/auth.routes"));

// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require('./error-handling')(app);

Expand Down
13 changes: 8 additions & 5 deletions models/User.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ const { Schema, model } = require("mongoose");
const userSchema = new Schema({
username: {
type: String,
unique: true
unique: true,
required: true
},
password: String
password: {
type: String,
required: true,
unique: true
}
});

const User = model("User", userSchema);

module.exports = User;
module.exports = model("User", userSchema);;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "nodemon server.js"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
Expand Down
45 changes: 42 additions & 3 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
margin:0px;
}

a {
color: #00B7FF;
a{
font-size: 18px;
background-color: white;
color: black;
text-decoration: none;
padding: 10px;
border-radius: 20px;
}

nav{
background-color: lightblue;
padding: 30px;
}

#title{
display: flex;
flex-direction: column;
align-items: center;
margin-top: 100px;
}

h1{
text-align: center;
margin: 50px;
}

#form{
margin: 100px;
display: flex;
flex-direction: column;
align-items: center;
row-gap: 40px;
font-size: 20px;
}

#form button{
font-size: 18px;
background-color: lightblue;
border: 1px solid lightblue;
border-radius: 20px;
padding: 10px;
}
35 changes: 35 additions & 0 deletions routes/auth.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const router = require("express").Router();
const bcryptjs = require("bcryptjs");
const User = require("../models/User.model");

router.get(("/signup"), (req, res, next)=>{
res.render("auth/sign-up")
});

router.post(("/signup"), (req, res, next)=>{
const saltRounds = 10;
const {username, password} = req.body;

bcryptjs
.genSalt(saltRounds)
.then((salt)=>{
return bcryptjs.hash(password, salt);
})
.then((hashedPassword)=>{
return User.create({username, password: hashedPassword});
})
.then((user)=>{
console.log("User was added:", user);
res.redirect("/");
})
.catch((err)=>{
next(err);
})
});



module.exports = router;



16 changes: 16 additions & 0 deletions views/auth/sign-up.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>Sign Up</h1>


<form action="/signup" method="post" id="form">
<div>
<label>Username</label>
<input type="text" name="username" required>
</div>

<div>
<label>Password</label>
<input type="password" name="password" required>
</div>

<button type="submit">Sign Up</button>
</form>
8 changes: 6 additions & 2 deletions views/index.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
<div id="title">
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
</div>


3 changes: 3 additions & 0 deletions views/layout.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
</head>

<body>
<nav>
<a href="/signup" id="sign-up-btn">Sign Up</a>
</nav>

{{{body}}}

Expand Down