Browse Source

Local authentication

pull/33/head
NGPixel 8 years ago
parent
commit
3d20fee5df
8 changed files with 90 additions and 42 deletions
  1. 5
      README.md
  2. 2
      assets/css/login.css
  3. 44
      client/scss/login.scss
  4. 34
      controllers/auth.js
  5. 4
      controllers/ws.js
  6. 23
      libs/auth.js
  7. 5
      models/user.js
  8. 15
      views/auth/login.pug

5
README.md

@ -19,11 +19,12 @@
- [Installation Guide](https://requarks-wiki.readme.io/docs/prerequisites)
##### Milestones
- [ ] Account Management
- [ ] Assets Management
- [x] Images
- [ ] Files/Documents
- [ ] Authentication
- [ ] Local
- [x] Authentication
- [x] Local
- [x] Microsoft Account
- [x] Google ID
- [x] Facebook

2
assets/css/login.css
File diff suppressed because it is too large
View File

44
client/scss/login.scss

@ -1,6 +1,8 @@
@import './layout/_base';
@import './layout/_mixins';
@import './libs/animate.min.css';
body {
padding: 0;
margin: 0;
@ -80,6 +82,34 @@ a {
animation: headerIntro 3s ease;
}
h3 {
font-size: 1.25rem;
font-weight: normal;
color: #FB8C00;
padding: 0;
margin: 0;
animation: shake 1s ease;
> .fa {
margin-right: 7px;
}
}
h4 {
font-size: 0.8rem;
font-weight: normal;
color: rgba(255,255,255,0.7);
padding: 0;
margin: 0 0 15px 0;
animation: fadeIn 3s ease;
}
form {
display: flex;
flex-direction: column;
}
input[type=text], input[type=password] {
width: 350px;
max-width: 80vw;
@ -96,7 +126,7 @@ a {
&:focus {
outline: none;
border-color: rgba(255,255,255,0.8);
border-color: #FB8C00;
}
}
@ -148,6 +178,10 @@ a {
background-color: #009688;
border-color: lighten(#009688, 10%);
&:focus {
border-color: #FFF;
}
&:hover {
background-color: darken(#009688, 10%);
}
@ -158,6 +192,10 @@ a {
background-color: #2196F3;
border-color: lighten(#2196F3, 10%);
&:focus {
border-color: #FFF;
}
&:hover {
background-color: darken(#2196F3, 10%);
}
@ -168,6 +206,10 @@ a {
background-color: #673AB7;
border-color: lighten(#673AB7, 10%);
&:focus {
border-color: #FFF;
}
&:hover {
background-color: darken(#673AB7, 10%);
}

34
controllers/auth.js

@ -35,28 +35,26 @@ router.get('/login', function(req, res, next) {
});
router.post('/login', bruteforce.prevent, function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (err) { return next(err); }
if (!user) {
req.flash('alert', {
class: 'error',
title: 'Invalid login',
message: "The email or password is invalid.",
iconClass: 'fa-times'
});
return res.redirect('/login');
}
if (!user) {
req.flash('alert', {
title: 'Invalid login',
message: "The email or password is invalid."
});
return res.redirect('/login');
}
req.logIn(user, function(err) {
if (err) { return next(err); }
req.brute.reset(function () {
return res.redirect('/');
});
});
req.logIn(user, function(err) {
if (err) { return next(err); }
req.brute.reset(function () {
return res.redirect('/');
});
});
})(req, res, next);
})(req, res, next);
});
/**

4
controllers/ws.js

@ -2,7 +2,9 @@
module.exports = (socket) => {
console.log(socket.request.user);
if(!socket.request.user.logged_in) {
return;
}
//-----------------------------------------
// SEARCH

23
libs/auth.js

@ -34,23 +34,24 @@ module.exports = function(passport, appconfig) {
passport.use('local',
new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
passwordField : 'password'
},
function(req, uEmail, uPassword, done) {
db.User.findOne({ 'email' : uEmail }).then((user) => {
if (user) {
user.validatePassword(uPassword).then((isValid) => {
return (isValid) ? done(null, user) : done(null, false);
(uEmail, uPassword, done) => {
db.User.findOne({ email: uEmail, provider: 'local' }).then((user) => {
if(user) {
return user.validatePassword(uPassword).then(() => {
return done(null, user) || true;
}).catch((err) => {
return done(err, null);
});
} else {
return done(null, false);
return done(new Error('Invalid Login'), null);
}
}).catch((err) => {
done(err);
done(err, null) ;
});
})
);
}
));
}

5
models/user.js

@ -78,9 +78,8 @@ userSchema.statics.hashPassword = (rawPwd) => {
};
userSchema.methods.validatePassword = function(rawPwd) {
let self = this;
return bcrypt.hash(rawPwd).then((pwd) => {
return (self.password === pwd) ? true : Promise.reject(new Error('Invalid Password'));
return bcrypt.compare(rawPwd, this.password).then((isValid) => {
return (isValid) ? true : Promise.reject(new Error('Invalid Login'));
});
};

15
views/auth/login.pug

@ -32,10 +32,16 @@ html
#root
h1= appconfig.title
h2 Login required
if appflash.length > 0
h3
i.fa.fa-warning
= appflash[0].title
h4= appflash[0].message
if appconfig.auth.local.enabled
input#login-user(type='text', placeholder='Email address')
input#login-pass(type='password', placeholder='Password')
button Log In
form(method='post', action='/login')
input#login-user(type='text', name='email', placeholder='Email address')
input#login-pass(type='password', name='password', placeholder='Password')
button(type='submit') Log In
if appconfig.authStrategies.socialEnabled
#social
if appconfig.auth.local.enabled
@ -58,5 +64,4 @@ html
= t('footer.poweredby') + ' '
a.icon(href='https://github.com/Requarks/wiki')
i.fa.fa-github
a(href='https://github.com/Requarks/wiki') Requarks Wiki
a(href='https://github.com/Requarks/wiki') Requarks Wiki
Loading…
Cancel
Save