Skip to content

Commit

Permalink
commit untuk fitur sign up, login, dan logout
Browse files Browse the repository at this point in the history
  • Loading branch information
hitsamtiammar123 committed Feb 27, 2020
1 parent 46c5299 commit 1be1a4a
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 15 deletions.
6 changes: 5 additions & 1 deletion controller/indexController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class indexController{
index(req,res){
res.render('ejs/index');
res.render('ejs/index',{session:req.session});
}

session(req,res){
res.send(req.session);
}
}
module.exports=indexController;
38 changes: 37 additions & 1 deletion controller/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,43 @@ const {User}=require('../models');
class userController{

create(req,res){

User.hash(req.body.password)
.then((resultHash)=>{
let inputs=User.validateInput(req.body);
User.create({
name:inputs.name,
username:inputs.username,
email:inputs.email,
role:inputs.role,
password:resultHash
})
.then(()=>res.redirect('/'))
.catch((err)=>res.send(err))
})
.catch((err)=>res.send(err));
}

login(req,res){
let password=req.body.password;
let email=req.body.email;
User.checkHash(email,password)
.then((result)=>{
if(result){
req.session.user={
email
}
}
res.redirect('/');

})
.catch((err)=>{
res.send(err)
})
}

logout(req,res){
req.session.user=null;
res.redirect('/');
}

}
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ app.use(express.static(__dirname+'/public'));
app.use(express.urlencoded({extended:true}));
app.use(session({
secret: 'hehehehe',
resave: false,
resave: true,
saveUninitialized: true,
cookie: { secure: true }
cookie: { secure: false }
}));

app.use('/',require('./router/routerindex'));
Expand Down
36 changes: 35 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,42 @@
module.exports = (sequelize, DataTypes) => {

const Model=sequelize.Sequelize.Model;
const bcrypt=require('bcrypt');

class User extends Model{}
class User extends Model{

static hash(myPlaintextPassword){
const saltRounds = 10;

return bcrypt.hash(myPlaintextPassword, saltRounds);
}

static validateInput(body){
let obj={};
obj.name=body.name;
obj.username=body.username;
obj.email=body.email;
obj.role='user';

return obj


}

static checkHash(email,plainPassword){
return User.findOne({
where:{
email:email
}
})
.then((user)=>{
let hash=user.password;
return bcrypt.compare(plainPassword, hash);
})
.catch((err)=>Promise.reject(err))
}

}

User.init({
name: DataTypes.STRING,
Expand Down
8 changes: 8 additions & 0 deletions router/routerUser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const express=require("express")
const router = express.Router();
const Controller=require('../controller/userController');

const controller=new Controller();

router.post('/create',controller.create);
router.post('/login',controller.login);
router.post('/logout',controller.logout);

module.exports=router;
1 change: 1 addition & 0 deletions router/routerindex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const router = express.Router();
const controller=new Controller();

router.get('/',controller.index);
router.get('/session',controller.session);

module.exports=router;
12 changes: 6 additions & 6 deletions views/ejs/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@
<%-include("partial/source-head")%>
</head>
<body>
<%-include("partial/navbar")%>
<%-include("partial/navbar",{session:session})%>

<div class="jumbotron main-jumbotron jumbotron-fluid">
<div class="container ">
<div class="row">
<div class="col-sm-8"></div>
<div class="col-sm-4">
<form class="form-horizontal" action="/action_page.php">
<form class="form-horizontal" method="POST" action="/user/create">


<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="pwd" placeholder="Name">
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
</div>

<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="pwd" placeholder="Username">
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
</div>
</div>

<div class="form-group">
<div class="col-sm-10">
<input type="email" class="form-control" id="pwd" placeholder="Email">
<input type="email" class="form-control" id="email" name="email" placeholder="Email">
</div>
</div>

<div class="form-group">
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Password">
<input type="password" class="form-control" id="pwd" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
Expand Down
24 changes: 20 additions & 4 deletions views/ejs/partial/navbar.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
<a class="navbar-brand" href="#">Inggris senang-senang</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="#" data-toggle="modal" data-target="#login"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
<li><a href="/courses">View All Course</a></li>
<% if(!session.user){%>
<li><a href="#" data-toggle="modal" data-target="#login"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
<%}else{%>
<li><a href="#" onclick="logout()"><span class="glyphicon glyphicon-log-out"></span>Logout</a></li>
<%}%>
</ul>
</div>
</nav>

<% if(!session.user){%>
<div class="modal fade" id="login" role="dialog">
<div class="modal-dialog">
Expand All @@ -19,7 +24,7 @@
<h4 class="modal-title">Silahkan login jika anda sudah punya</h4>
</div>
<div class="modal-body">
<form action="">
<form action="/user/login" method="POST">
<div class="form-group">
<label for="email">Email</label>
<input id="email" type="email" class="form-control" name="email" >
Expand All @@ -37,4 +42,15 @@
</div>
</div>
</div>
</div>
<%}else{%>
<form id="logout" action="/user/logout" method="POST">
</form>
<script>
function logout(){
var logoutElem=document.getElementById('logout');
logoutElem.submit();
}
</script>
<%}%>

0 comments on commit 1be1a4a

Please sign in to comment.