Skip to content

Commit 570cd61

Browse files
committed
Implement user login
1 parent 629d8e3 commit 570cd61

File tree

7 files changed

+273
-21
lines changed

7 files changed

+273
-21
lines changed

app.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,9 @@ async.series([function createDatabaseDirectory(next) {
6868
}
6969
next();
7070
}, function setupPassport(next) {
71+
//Configure passport
72+
require(__dirname + '/passport_config.js')(app, passport);
7173

72-
passport.use(new Strategy(
73-
function(username, password, cb) {
74-
app.db.users.findOne({username: username}, function(err, user) {
75-
if (err) { return cb(err); }
76-
if (!user) { return cb(null, false, {message:"Invalid credentials"}); }
77-
if (user.password != password) { return cb(null, false, {message:"Invalid credentials"}); }
78-
return cb(null, user);
79-
});
80-
}));
81-
82-
// Configure Passport persistence.
83-
passport.serializeUser(function(user, cb) {
84-
var sessionUser = user;
85-
cb(null, sessionUser);
86-
});
87-
88-
passport.deserializeUser(function(user, cb) {
89-
cb(null, user);
90-
});
9174
next();
9275
}, function setupEverythingElse(next) {
9376
// middleware to use in the app

config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function Config(app) {
9393
function (cb) {
9494
app.db.users.findOne({username:'admin'}, function (err, item) {
9595
if(!item)
96-
app.db.users.insert({username:'admin',password:'admin'});
96+
app.db.admins.insert({username:'admin',password:'admin'});
9797
});
9898

9999
cb();

db.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ module.exports = function(app) {
66
app.db.playlists = new Datastore({ filename: app.get('configDir') + '/dbs/playlists.db', autoload: true });
77
app.db.settings = new Datastore({ filename: app.get('configDir') + '/dbs/settings.db', autoload: true });
88
app.db.users = new Datastore({ filename: app.get('configDir') + '/dbs/users.db', autoload: true });
9+
app.db.admins = new Datastore({ filename: app.get('configDir') + '/dbs/admins.db', autoload: true });
910
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"song-search": "^0.1.0",
3535
"soundcloud-resolver": "*",
3636
"swig": "*",
37+
"validator": "^6.2.1",
3738
"youtube-playlist-info": "^0.1.0",
3839
"ytdl-core": "*"
3940
},

passport_config.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Configuration file for passportJS
2+
3+
var LocalStrategy = require('passport-local').Strategy;
4+
var validate = require('validator');
5+
6+
module.exports = function(app, passport){
7+
8+
// Specify a local strategy for passport normal user login
9+
passport.use('user-login', new LocalStrategy(
10+
function(username, password, cb){
11+
12+
// Validate user data
13+
var error = 0;
14+
username = validate.trim(username);
15+
username = username.toLowerCase();
16+
error += validate.isEmpty(username);
17+
error += validate.isEmpty(password);
18+
19+
if (!error) {
20+
app.db.users.findOne({username: username}, function(err, user) {
21+
if (err) { return cb(err); }
22+
if (!user) { return cb(null, false, {message:"Invalid credentials"}); }
23+
if (user.password != password) { return cb(null, false, {message:"Invalid credentials"}); }
24+
return cb(null, user);
25+
});
26+
27+
}
28+
else
29+
return cb(null, false);
30+
}
31+
));
32+
33+
// Specify a local strategy for passport normal user signup
34+
passport.use('user-signup', new LocalStrategy(
35+
function(username, password, cb){
36+
37+
// Validate user data
38+
var error = 0;
39+
username = validate.trim(username);
40+
username = username.toLowerCase();
41+
error += validate.isEmpty(username);
42+
error += validate.isEmpty(password);
43+
44+
if (!error) {
45+
app.db.users.findOne({username: username}, function(err, user) {
46+
if (err) {
47+
return cb(err);
48+
}
49+
if (user) {
50+
return cb(null, false, {message:"User already registered"});
51+
}
52+
else{
53+
app.db.users.insert({username: username, password:password});
54+
}
55+
});
56+
}else
57+
return cb(null, false);
58+
}
59+
));
60+
61+
// Specify a local strategy for passport admin login
62+
passport.use('admin-login', new LocalStrategy(
63+
function(username, password, cb){
64+
65+
// Validate user data
66+
var error = 0;
67+
username = validate.trim(username);
68+
username = username.toLowerCase();
69+
error += validate.isEmpty(username);
70+
error += validate.isEmpty(password);
71+
72+
if (!error) {
73+
app.db.admins.findOne({username: username}, function(err, user) {
74+
if (err) { return cb(err); }
75+
if (!user) { return cb(null, false, {message:"Invalid credentials"}); }
76+
if (user.password != password) { return cb(null, false, {message:"Invalid credentials"}); }
77+
return cb(null, user);
78+
});
79+
80+
}
81+
else
82+
return cb(null, false);
83+
}
84+
));
85+
86+
// Specify serialize and deserialize methods for passport
87+
passport.serializeUser(function(user, cb){
88+
var sessionUser = user;
89+
cb(null, sessionUser);
90+
});
91+
passport.deserializeUser(function(user, cb){
92+
cb(null, user);
93+
});
94+
};

routes/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ exports.createRoutes = function(app_ref) {
3434
//adds login facility
3535
app.get('/admin',function (req,res) { res.render('admin',{ msg:req.flash('error'), log: req.user? true : false});});
3636
//auth using passport
37-
app.post('/admin', passport.authenticate('local', { failureRedirect: '/admin', failureFlash: true }), function(req, res) {
37+
app.post('/admin', passport.authenticate('admin-login', { failureRedirect: '/admin', failureFlash: true }), function(req, res) {
38+
res.redirect('/');
39+
});
40+
app.get('/login', function (req,res) { res.render('login',{ msg:req.flash('error'), log: req.user? true : false});});
41+
app.post('/login', passport.authenticate('user-login', { failureRedirect: '/login', failureFlash: true }), function(req, res) {
3842
res.redirect('/');
3943
});
4044
app.post('/logout', function(req, res) {

views/login.html

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>User Login</title>
7+
<style>
8+
@import url(https://fonts.googleapis.com/css?family=Roboto:300);
9+
.login-page {
10+
width: 410px;
11+
padding: 8% 0 0;
12+
margin: auto;
13+
}
14+
15+
h1 {
16+
color: white;
17+
font-size: 50px;
18+
font-weight: 130;
19+
text-align: center;
20+
margin: 15px;
21+
}
22+
.form {
23+
position: relative;
24+
z-index: 1;
25+
background: #FFFFFF;
26+
max-width: 360px;
27+
margin: 0 auto 100px;
28+
padding: 45px;
29+
text-align: center;
30+
box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
31+
}
32+
33+
.form input {
34+
font-family: "Roboto", sans-serif;
35+
outline: 0;
36+
background: #f2f2f2;
37+
width: 100%;
38+
border: 0;
39+
margin: 0 0 15px;
40+
padding: 15px;
41+
box-sizing: border-box;
42+
font-size: 14px;
43+
}
44+
45+
.form button {
46+
font-family: "Roboto", sans-serif;
47+
text-transform: uppercase;
48+
outline: 0;
49+
background: #022a50;
50+
width: 100%;
51+
border: 0;
52+
padding: 15px;
53+
color: #FFFFFF;
54+
font-size: 14px;
55+
-webkit-transition: all 0.3 ease;
56+
transition: all 0.3 ease;
57+
cursor: pointer;
58+
}
59+
60+
.form button:hover,
61+
.form button:active,
62+
.form button:focus {
63+
background: #022a50;
64+
}
65+
66+
.form .message {
67+
margin: 15px 0 0;
68+
color: #b3b3b3;
69+
font-size: 12px;
70+
}
71+
72+
.form .message a {
73+
color: #022a50;
74+
text-decoration: none;
75+
}
76+
77+
.form .register-form {
78+
display: none;
79+
}
80+
81+
.container {
82+
position: relative;
83+
z-index: 1;
84+
max-width: 300px;
85+
margin: 0 auto;
86+
}
87+
88+
.container:before,
89+
.container:after {
90+
content: "";
91+
display: block;
92+
clear: both;
93+
}
94+
95+
.container .info {
96+
margin: 50px auto;
97+
text-align: center;
98+
}
99+
100+
.container .info h1 {
101+
margin: 0 0 15px;
102+
padding: 0;
103+
font-size: 36px;
104+
font-weight: 300;
105+
color: #1a1a1a;
106+
}
107+
108+
.container .info span {
109+
color: #4d4d4d;
110+
font-size: 12px;
111+
}
112+
113+
.container .info span a {
114+
color: #000000;
115+
text-decoration: none;
116+
}
117+
118+
.container .info span .fa {
119+
color: #EF3B3A;
120+
}
121+
122+
.alert{
123+
margin: 5px auto;
124+
text-align: center;
125+
color: red;
126+
border: 2px;
127+
}
128+
129+
body {
130+
background: #022a50;
131+
font-family: "Roboto", sans-serif;
132+
-webkit-font-smoothing: antialiased;
133+
-moz-osx-font-smoothing: grayscale;
134+
}
135+
</style>
136+
</head>
137+
138+
<body>
139+
<div class="login-page">
140+
<h1>Login</h1>
141+
{% if msg %}
142+
<div class="alert">
143+
<p>{{msg}}</p>
144+
</div>
145+
{% endif %}
146+
<div class="form">
147+
<!-- <form class="register-form">
148+
<input type="text" placeholder="name" />
149+
<input type="password" placeholder="password" />
150+
<input type="text" placeholder="email address" />
151+
<button>create</button>
152+
<p class="message">Already registered? <a href="#">Sign In</a></p>
153+
</form> -->
154+
{% if log %}
155+
<form action="/logout" method="post" class="login-form">
156+
<button type="submit">Logout</button>
157+
{% else %}
158+
<form action="/login" method="post" class="login-form">
159+
<input type="text" placeholder="username" name="username" />
160+
<input type="password" placeholder="password" name="password" />
161+
<button type="submit">login</button>
162+
<!-- <p class="message">Not registered? <a href="#">Create an account</a></p> -->
163+
{% endif %}
164+
</form>
165+
</div>
166+
</div>
167+
</body>
168+
169+
</html>

0 commit comments

Comments
 (0)