Last active
August 29, 2015 14:02
-
-
Save aaronroberson/86391ce601dbaa001bb2 to your computer and use it in GitHub Desktop.
Geekwise Day 10 Assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular) { | |
var app = angular.module('MyStore'); | |
app.controller('AppController', function($scope, $state, $timeout, Auth) { | |
function successCallback() { | |
$state.go('login'); | |
$scope.alert = { | |
type: 'success', | |
message: 'You have been logged out.' | |
}; | |
$timeout(function() { | |
$scope.alert = undefined; | |
}, 3000); | |
} | |
$scope.logout = function() { | |
Auth.logout(successCallback); | |
} | |
}); | |
})(window.angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular) { | |
"use strict"; | |
var app = angular.module('MyStore', ['ngCookies', 'ngMessages', 'ui.router']); | |
})(window.angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular) { | |
"use strict"; | |
var app = angular.module('MyStore'); | |
app.factory('Auth', function($http, $rootScope, $cookieStore) { | |
$rootScope.currentUser = $cookieStore.get('user'); | |
$cookieStore.remove('user'); | |
return { | |
login: function(user, success, error) { | |
return $http.post('/api/login', user) | |
.success(function(data) { | |
$rootScope.currentUser = data; | |
success(); | |
}) | |
.error(error); | |
}, | |
signup: function(user, success, error) { | |
return $http.post('/api/signup', user) | |
.success(success) | |
.error(error); | |
}, | |
logout: function(success) { | |
return $http.get('/api/logout').success(function() { | |
$rootScope.currentUser = null; | |
$cookieStore.remove('user'); | |
success(); | |
}); | |
} | |
}; | |
}); | |
})(window.angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Update to Angular Beta 11 and include cookies and messages --> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular-cookies.min.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular-messages.min.js"></script> | |
<!-- Add the appController to the body tag --> | |
<body ng-controller="AppController"></body> | |
<!-- Add to navbar --> | |
<ul class="nav navbar-nav pull-right" ng-if="!currentUser"> | |
<li ui-sref-active="active"> | |
<a ui-sref="login">Login</a> | |
</li> | |
<li ui-sref-active="active"> | |
<a ui-sref="signup">Sign up</a> | |
</li> | |
</ul> | |
<ul class="nav navbar-nav pull-right" ng-if="currentUser"> | |
<li class="navbar-text" ng-bind="currentUser.email"> | |
</li> | |
<li> | |
<a href="javascript:void(0);" ng-click="logout()">Logout</a> | |
</li> | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular) { | |
var app = angular.module('MyStore'); | |
app.controller('LoginController', function($scope, $state, $timeout, Auth) { | |
function successCallback() { | |
$scope.alert = { | |
type: 'success', | |
message: 'You have successfully logged in.' | |
}; | |
$timeout(function() { | |
$state.go('home'); | |
$scope.alert = undefined; | |
}, 3000); | |
} | |
function errorCallback() { | |
$scope.alert = { | |
type: 'danger', | |
message: 'Invalid username and/or password' | |
}; | |
$timeout(function() { | |
$scope.alert = undefined; | |
}, 3000); | |
} | |
$scope.login = function() { | |
Auth.login({ | |
email: $scope.email, | |
password: $scope.password | |
}, successCallback, errorCallback); | |
}; | |
}); | |
})(window.angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="row"> | |
<div class="col-md-4 col-md-offset-4"> | |
<div class="center-form panel"> | |
<div class="panel-body"> | |
<h2 class="text-center">Login</h2> | |
<div class="alert alert-{{alert.type}}" ng-if="alert" ng-bind="alert.message"></div> | |
<form method="post" ng-submit="login()" name="loginForm"> | |
<div class="form-group"> | |
<input class="form-control input-lg" type="text" name="email" | |
ng-model="email" placeholder="Email" required autofocus> | |
</div> | |
<div class="form-group"> | |
<input class="form-control input-lg" type="password" name="password" | |
ng-model="password" placeholder="Password" required> | |
</div> | |
<button type="submit" ng-disabled="loginForm.$invalid" | |
class="btn btn-lg btn-block btn-success">Sign In | |
</button> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Add the dependency to passport after the mongoose require decleration */ | |
var passport = require('passport'); | |
/* Add the following routes after the products routes */ | |
// logout API route | |
app.get('/api/logout', function(req, res, next) { | |
req.logout(); | |
res.send(200); | |
}); | |
// login API route | |
app.post('/api/login', passport.authenticate('local'), function(req, res) { | |
res.cookie('user', JSON.stringify(req.user)); | |
res.send(req.user); | |
}); | |
// signup API route | |
app.post('/api/signup', function(req, res, next) { | |
var User = mongoose.model('User'); | |
var user = new User({ | |
email: req.body.email, | |
password: req.body.password | |
}); | |
user.save(function(err) { | |
if (err) return next(err); | |
res.send(200); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Add the following to the depenecies (after bodyParser) */ | |
var passport = require('passport'); | |
var session = require('express-session'); | |
var LocalStrategy = require('passport-local').Strategy; | |
var bcrypt = require('bcrypt-nodejs'); | |
/* Add the following after the MODELS */ | |
/* ===================== PASSPORT ========================= */ | |
passport.serializeUser(function(user, done) { | |
done(null, user.id); | |
}); | |
passport.deserializeUser(function(id, done) { | |
var User = mongoose.model('User'); | |
User.findById(id, function(err, user) { | |
done(err, user); | |
}); | |
}); | |
passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) { | |
var User = mongoose.model('User'); | |
User.findOne({ email: email }, function(err, user) { | |
if (err) return done(err); | |
if (!user) return done(null, false); | |
function cb(err, isMatch) { | |
if (err) return done(err); | |
if (isMatch) return done(null, user); | |
return done(null, false); | |
} | |
bcrypt.compare(password, user.password, function(err, isMatch) { | |
if (err) return cb(err); | |
cb(null, isMatch); | |
}); | |
}); | |
})); | |
/* Add the following after express.static in module section */ | |
app.use(session({ secret: 'blackwidow straw' })); // Encryption key/salt | |
app.use(passport.initialize()); // Initializes passport | |
app.use(passport.session()); // Creates a passport session | |
/* Add the following after express.static */ | |
app.use(function(req, res, next) { | |
if (req.user) { | |
res.cookie('user', JSON.stringify(req.user)); | |
} | |
next(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular) { | |
"use strict"; | |
var app = angular.module('MyStore'); | |
app.controller('SignupController', function($scope, $state, $timeout, Auth) { | |
function successCallback() { | |
$scope.alert = { | |
type: 'success', | |
message: 'Your account has been created.' | |
}; | |
$timeout(function() { | |
$state.go('login'); | |
$scope.alert = undefined; | |
}, 3000); | |
} | |
function errorCallback() { | |
$scope.alert = { | |
type: 'danger', | |
message: 'There was an error created your account. Please try again.' | |
}; | |
$timeout(function() { | |
$scope.alert = undefined; | |
}, 3000); | |
} | |
$scope.signup = function() { | |
Auth.signup({ | |
email : $scope.email, | |
password: $scope.password | |
}, successCallback, errorCallback); | |
}; | |
}); | |
})(window.angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="row"> | |
<div class="col-md-4 col-md-offset-4"> | |
<div class="center-form panel"> | |
<form method="post" ng-submit="signup()" name="signupForm"> | |
<div class="panel-body"> | |
<h2 class="text-center">Sign up</h2> | |
<div class="alert alert-{{alert.type}}" ng-if="alert" ng-bind="alert.message"></div> | |
<div class="form-group" | |
ng-class="{ 'has-success' : signupForm.email.$valid && signupForm.email.$dirty, 'has-error' : signupForm.email.$invalid && signupForm.email.$dirty }"> | |
<input class="form-control input-lg" type="email" id="email" | |
name="email" ng-model="email" placeholder="Email" required | |
autofocus> | |
<div class="help-block text-danger" ng-if="signupForm.email.$dirty" | |
ng-messages="signupForm.email.$error"> | |
<div ng-message="required">Your email address is required.</div> | |
<div ng-message="email">Your email address is invalid.</div> | |
</div> | |
</div> | |
<div class="form-group" | |
ng-class="{ 'has-success' : signupForm.password.$valid && signupForm.password.$dirty, 'has-error' : signupForm.password.$invalid && signupForm.password.$dirty }"> | |
<input class="form-control input-lg" type="password" name="password" | |
ng-model="password" placeholder="Password" required> | |
<div class="help-block text-danger" | |
ng-if="signupForm.password.$dirty" | |
ng-messages="signupForm.password.$error"> | |
<div ng-message="required">Password is required.</div> | |
</div> | |
</div> | |
<div class="form-group" | |
ng-class="{ 'has-success' : signupForm.confirmPassword.$valid && signupForm.confirmPassword.$dirty, 'has-error' : signupForm.confirmPassword.$invalid && signupForm.confirmPassword.$dirty }"> | |
<input class="form-control input-lg" type="password" | |
name="confirmPassword" ng-model="confirmPassword" | |
repeat-password="password" placeholder="Confirm Password" | |
required> | |
<div class="help-block text-danger my-special-animation" | |
ng-if="signupForm.confirmPassword.$dirty" | |
ng-messages="signupForm.confirmPassword.$error"> | |
<div ng-message="required">You must confirm password.</div> | |
<div ng-message="repeat">Passwords do not match.</div> | |
</div> | |
</div> | |
<button type="submit" ng-disabled="signupForm.$invalid" | |
class="btn btn-lg btn-block btn-primary">Create Account | |
</button> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require mongoose dependency | |
var mongoose = require('mongoose'); | |
var bcrypt = require('bcrypt-nodejs'); | |
var userSchema = new mongoose.Schema({ | |
email: { type: String, unique: true }, | |
password: String | |
}); | |
var User = mongoose.model('User', userSchema); | |
userSchema.pre('save', function(next) { | |
var user = this; | |
if (!user.isModified('password')) return next(); | |
bcrypt.genSalt(10, function(err, salt) { | |
if (err) return next(err); | |
bcrypt.hash(user.password, salt, function(){}, function(err, hash) { | |
if (err) return next(err); | |
user.password = hash; | |
next(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment