فهرست منبع

Local authentication

NGPixel 8 سال پیش
والد
کامیت
3d20fee5df
8فایلهای تغییر یافته به همراه89 افزوده شده و 41 حذف شده
  1. 3 2
      README.md
  2. 0 0
      assets/css/login.css
  3. 43 1
      client/scss/login.scss
  4. 16 18
      controllers/auth.js
  5. 3 1
      controllers/ws.js
  6. 12 11
      libs/auth.js
  7. 2 3
      models/user.js
  8. 10 5
      views/auth/login.pug

+ 3 - 2
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

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
assets/css/login.css


+ 43 - 1
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%);
 				}

+ 16 - 18
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);
 });
 
 /**

+ 3 - 1
controllers/ws.js

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

+ 12 - 11
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) ;
 				});
-			})
-		);
+			}
+		));
 
 	}
 

+ 2 - 3
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'));
 	});
 };
 

+ 10 - 5
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

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است