Skip to content

started feat(rest): Add Passport support - Issue #25 #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
ba1281f
added the question dependant on the mongo choice
kjellski Dec 13, 2013
925e89b
added the user model I've modified from mean.io to use with bcrypt
kjellski Dec 13, 2013
d76a425
removed constants for login attempts, added passport configuration, r…
kjellski Dec 13, 2013
0fd6843
moved files arround and fixed the dependencies issues
kjellski Dec 13, 2013
a3c5a07
added login link, not working yet, removed unneccesary spacing for se…
kjellski Dec 13, 2013
2a42fa7
moved model to singular, added login and signup links
kjellski Dec 14, 2013
df2210a
Merge branch 'master' of github.com:DaftMonk/generator-angular-fullstack
kjellski Dec 14, 2013
3fc8516
added settable links to navbar
kjellski Dec 14, 2013
57ca3ba
added the routes to all app.js templates
kjellski Dec 16, 2013
53a9c46
found no better way then using this environment variable for app.js, …
kjellski Dec 17, 2013
580e85e
cleaned up the mess I made.
kjellski Dec 17, 2013
c882d83
removed username from user model, made email unique, now using this i…
kjellski Dec 17, 2013
cc1cd1a
fixed navigation and navbar links, added auth controller, TODO: missi…
kjellski Dec 17, 2013
6ffa239
added the view partials for login, TODO: signup is missing
kjellski Dec 17, 2013
a31ed69
completely overhauled the server side in favour of angular-passport p…
kjellski Dec 18, 2013
8cf06f9
moved the navbar into the controllers templates folder too, made the …
kjellski Jan 1, 2014
b6c2008
added a bit more verbose spelling to the login failures, made the aut…
kjellski Jan 1, 2014
8ee4c72
minor formatting fixes and removed login and logout links from normal…
kjellski Jan 1, 2014
44fff94
added cookieParser to express config to be able to read the sessions
kjellski Jan 1, 2014
b588b99
not very clean, but working version for navbar logins, now needs jade…
kjellski Jan 1, 2014
6d58898
Merge remote-tracking branch 'remotes/origin/pr/36' into passport
DaftMonk Jan 1, 2014
e2623a5
merged against upstream master
kjellski Jan 1, 2014
f060eb9
Merge remote-tracking branch 'remotes/origin/pr/36' into passport
DaftMonk Jan 1, 2014
80d04f3
merged and fixed against DaftMonk/passport
kjellski Jan 1, 2014
582f9c3
fixing more problems from the merge
DaftMonk Jan 1, 2014
1e1b72a
Merged duplicate changes made to get passport branch working.
DaftMonk Jan 1, 2014
4796c95
refactored server code to have more consistent style
DaftMonk Jan 3, 2014
6b8c763
updated db to export the db connection
DaftMonk Jan 3, 2014
4e99631
removed whitespace
DaftMonk Jan 3, 2014
1d99b5d
refactor api to increase consistency
DaftMonk Jan 7, 2014
96f7567
auth service now uses promises
DaftMonk Jan 7, 2014
c1b11e8
Add a session get method for checking the users status
DaftMonk Jan 7, 2014
5ff133b
fixed show method on user controller
DaftMonk Jan 7, 2014
e1e26b1
removed unnecessary if statement
DaftMonk Jan 7, 2014
31b0207
added name field to user model
DaftMonk Jan 7, 2014
ec2c3e3
fixed client side validation and cleaned up partial templates
DaftMonk Jan 7, 2014
a475568
update comment
DaftMonk Jan 7, 2014
5614861
changed the filenames of Services to uppercase, was case sensitive
kjellski Jan 7, 2014
bf0623e
added angular routing check to see if a route requires authentication
DaftMonk Jan 9, 2014
91b1545
added settings page that allows you to change password
DaftMonk Jan 9, 2014
efa0cab
configure routing
DaftMonk Jan 9, 2014
3187a07
added mongoose unique validator
DaftMonk Jan 9, 2014
3270725
updated angular controllers
DaftMonk Jan 9, 2014
d7bcf4f
made signup/login views easier to read
DaftMonk Jan 9, 2014
f5a9b25
get method on user correctly points to /me
DaftMonk Jan 9, 2014
ef69336
added settings route to app, fixed jshint warning
DaftMonk Jan 9, 2014
7531d8a
fixed http interceptor using un-minsafe format
DaftMonk Jan 9, 2014
f49c3cc
added test user info to login view
DaftMonk Jan 9, 2014
7554382
remove unnecessary tags
DaftMonk Jan 9, 2014
11e1ffd
added jade templates
DaftMonk Jan 9, 2014
1f98962
- fixed the wrong uppercase services filenames to be lowercase
kjellski Jan 9, 2014
19a40ce
fixed small bracket error in navbar.coffee
kjellski Jan 9, 2014
a2d0051
added all missing code and formatting to coffeescript
DaftMonk Jan 10, 2014
a13d1cb
- fixed navbar.coffee, was based on older variable
kjellski Jan 10, 2014
f9b621d
Merge branch 'master' of github.com:DaftMonk/generator-angular-fullstack
kjellski Jan 11, 2014
fed51c7
Merge branch 'passport' of github.com:DaftMonk/generator-angular-full…
kjellski Jan 11, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"node": true,
"esnext": true,
"bitwise": false,
"curly": false,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"undef": true,
"strict": false,
"trailing": true,
"smarttabs": true
"node": true,
"esnext": true,
"bitwise": false,
"curly": false,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"undef": true,
"strict": false,
"trailing": true,
"smarttabs": true
}
78 changes: 70 additions & 8 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,17 @@ Generator.prototype.askForMongo = function askForMongo() {
name: 'mongo',
message: 'Would you like to include MongoDB with Mongoose?',
default: false
}, {
type: 'confirm',
name: 'mongoPassportUser',
message: 'Would you like to include a Passport authentication boilerplate?',
default: false,
when: function (props) {
return props.mongo;
}
}], function (props) {
this.mongo = props.mongo;
this.mongoPassportUser = props.mongoPassportUser;

cb();
}.bind(this));
Expand Down Expand Up @@ -335,21 +344,24 @@ function appendFilesToJade(jadeOrOptions, fileType, optimizedPath, sourceFileLis
return updatedContent;
}

Generator.prototype.navBarScript = function navBarScript() {
var ext = 'js';
var folder = 'javascript';
var minsafe = '';
var copyScriptWithEnvOptions = function copyScriptWithEnvOptions(that, fileToCopy, destinationFolder) {
var ext = 'js',
minsafe = '',
sourceFolder = 'javascript';

if(this.env.options.coffee) {
if(that.env.options.coffee) {
ext = 'coffee';
folder = 'coffeescript';
sourceFolder = 'coffeescript';
}

if(this.env.options.minsafe) {
if(that.env.options.minsafe) {
minsafe = '-min';
}
that.copy('../../templates/' + sourceFolder + minsafe + '/' + fileToCopy + '.' + ext, destinationFolder + fileToCopy + '.' + ext);
};

this.copy('../../templates/' + folder + minsafe + '/navbar.' + ext, 'app/scripts/controllers/navbar.' + ext);
Generator.prototype.navBarScript = function navBarScript() {
copyScriptWithEnvOptions(this, 'controllers/navbar', 'app/scripts/');
};

Generator.prototype.appJs = function appJs() {
Expand All @@ -360,6 +372,18 @@ Generator.prototype.appJs = function appJs() {
sourceFileList: ['scripts/app.js', 'scripts/controllers/main.js', 'scripts/controllers/navbar.js'],
searchPath: ['.tmp', 'app']
};

// only reference authentication controllers when required
if (this.mongoPassportUser) {
appendOptions.sourceFileList.push('scripts/controllers/login.js');
appendOptions.sourceFileList.push('scripts/controllers/signup.js');
appendOptions.sourceFileList.push('scripts/controllers/settings.js');
appendOptions.sourceFileList.push('scripts/services/auth.js');
appendOptions.sourceFileList.push('scripts/services/session.js');
appendOptions.sourceFileList.push('scripts/services/user.js');
appendOptions.sourceFileList.push('scripts/directives/mongooseError.js');
}

if (this.jade) {
this.indexFile = appendFilesToJade(appendOptions);
} else {
Expand All @@ -380,6 +404,11 @@ Generator.prototype.addJadeViews = function addHtmlJade() {
if(this.jade) {
this.copy('../../templates/views/jade/partials/main.jade', 'app/views/partials/main.jade');
this.copy('../../templates/views/jade/partials/navbar.jade', 'app/views/partials/navbar.jade');
if(this.mongoPassportUser) {
this.copy('../../templates/views/jade/partials/login.jade', 'app/views/partials/login.jade');
this.copy('../../templates/views/jade/partials/signup.jade', 'app/views/partials/signup.jade');
this.copy('../../templates/views/jade/partials/settings.jade', 'app/views/partials/settings.jade');
}
this.copy('../../templates/views/jade/404.jade', 'app/views/404.jade');
}
};
Expand All @@ -388,6 +417,11 @@ Generator.prototype.addHtmlViews = function addHtmlViews() {
if(!this.jade) {
this.copy('../../templates/views/html/partials/main.html', 'app/views/partials/main.html');
this.copy('../../templates/views/html/partials/navbar.html', 'app/views/partials/navbar.html');
if(this.mongoPassportUser) {
this.copy('../../templates/views/html/partials/login.html', 'app/views/partials/login.html');
this.copy('../../templates/views/html/partials/signup.html', 'app/views/partials/signup.html');
this.copy('../../templates/views/html/partials/settings.html', 'app/views/partials/settings.html');
}
this.copy('../../templates/views/html/404.html', 'app/views/404.html');
}
};
Expand Down Expand Up @@ -443,11 +477,39 @@ Generator.prototype.serverFiles = function () {
};

Generator.prototype.mongoFiles = function () {

if (!this.mongo) {
return; // Skip if disabled.
}
this.env.options.mongo = this.mongo;

this.template('../../templates/express/mongo/mongo.js', 'lib/db/mongo.js');
this.template('../../templates/express/mongo/dummydata.js', 'lib/db/dummydata.js');
this.template('../../templates/express/mongo/thing.js', 'lib/models/thing.js');

if(!this.mongoPassportUser) {
return; // Skip if disabled.
}
this.env.options.mongoPassportUser = this.mongoPassportUser;

// frontend
copyScriptWithEnvOptions(this, 'controllers/login', 'app/scripts/');
copyScriptWithEnvOptions(this, 'controllers/signup', 'app/scripts/');
copyScriptWithEnvOptions(this, 'controllers/settings', 'app/scripts/');

copyScriptWithEnvOptions(this, 'services/auth', 'app/scripts/');
copyScriptWithEnvOptions(this, 'services/session', 'app/scripts/');
copyScriptWithEnvOptions(this, 'services/user', 'app/scripts/');

copyScriptWithEnvOptions(this, 'directives/mongooseError', 'app/scripts/');

// middleware
this.template('../../templates/express/middleware.js', 'lib/middleware.js');
// config
this.template('../../templates/express/config/passport.js', 'lib/config/passport.js');
// models
this.template('../../templates/express/mongo/user.js', 'lib/models/user.js');
// controllers
this.template('../../templates/express/session.js', 'lib/controllers/session.js');
this.template('../../templates/express/users.js', 'lib/controllers/users.js');
};
2 changes: 2 additions & 0 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ util.inherits(Generator, ScriptBase);

Generator.prototype.createAppFile = function createAppFile() {
this.angularModules = this.env.options.angularDeps;
this.mongo = this.env.options.mongo;
this.mongoPassportUser = this.env.options.mongoPassportUser;
this.ngRoute = this.env.options.ngRoute;
this.appTemplate('app', 'scripts/app');
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Yeoman generator for AngularJS with Express server",
"keywords": [
"yeoman-generator",
"mean",
"scaffold",
"express",
"fullstack",
Expand Down
10 changes: 10 additions & 0 deletions templates/coffeescript-min/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ angular.module('<%= scriptAppName %>', [<%= angularModules %>])<% if (ngRoute) {
.when '/',
templateUrl: 'partials/main'
controller: 'MainCtrl'
<% if(mongo && mongoPassportUser) {%>
.when '/login',
templateUrl: 'partials/login'
controller: 'LoginCtrl'
.when '/logout',
controller: 'LogoutCtrl'
.when '/signup',
templateUrl: 'partials/signup'
controller: 'SignupCtrl'
<% } %>
.otherwise
redirectTo: '/'
$locationProvider.html5Mode(true)
Expand Down
Empty file.
35 changes: 21 additions & 14 deletions templates/coffeescript-min/navbar.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
'use strict'

angular.module('<%= scriptAppName %>')
.controller 'NavbarCtrl', ['$scope', '$location', ($scope, $location) ->
$scope.menu = [
title: 'Home'
link: '/'
,
title: 'About'
link: '#'
,
title: 'Contact'
link: '#'
]
$scope.isActive = (route) ->
route is $location.path()
]
.controller 'NavbarCtrl', ['$scope', '$location', ($scope, $location) ->
$scope.menu = [
title: 'Home'
link: '/'
,
title: 'About'
link: '#'
,
title: 'Contact'
link: '#'
<% if(mongo && mongoPassportUser) { %>,
title: 'Sign Up'
link: '#/signup'
,
title: 'Login'
link: '#/login'
}<% } %>
]
$scope.isActive = (route) ->
route is $location.path()
]
30 changes: 28 additions & 2 deletions templates/coffeescript/app.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
'use strict'

angular.module('<%= scriptAppName %>', [<%= angularModules %>])<% if (ngRoute) { %>
.config ($routeProvider, $locationProvider) ->
.config ($routeProvider, $locationProvider<% if (mongoPassportUser) { %>, $httpProvider<% } %>) ->
$routeProvider
.when '/',
templateUrl: 'partials/main'
controller: 'MainCtrl'
<% if(mongoPassportUser) {%>
.when '/login',
templateUrl: 'partials/login'
controller: 'LoginCtrl'
.when '/signup',
templateUrl: 'partials/signup'
controller: 'SignupCtrl'
.when '/settings',
templateUrl: 'partials/settings'
controller: 'SettingsCtrl'
authenticate: true<% } %>
.otherwise
redirectTo: '/'
$locationProvider.html5Mode(true)<% } %>

$locationProvider.html5Mode true<% if (mongoPassportUser) { %>

$httpProvider.interceptors.push ['$q', '$location', ($q, $location) ->
responseError: (response) ->
if response.status is 401 or response.status is 403
$location.path '/login'
$q.reject response
else
$q.reject response
]
.run ($rootScope, $location, Auth) ->

# Redirect to login if route requires auth and you're not logged in
$rootScope.$on '$routeChangeStart', (event, next) ->
$location.path '/login' if next.authenticate and not Auth.isLoggedIn()<% } %><% } %>
21 changes: 21 additions & 0 deletions templates/coffeescript/controllers/login.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

angular.module('<%= scriptAppName %>')
.controller 'LoginCtrl', ($scope, Auth, $location) ->
$scope.user = {}
$scope.errors = {}

$scope.login = (form) ->
$scope.submitted = true

if form.$valid
Auth.login(
email: $scope.user.email
password: $scope.user.password
)
.then ->
# Logged in, redirect to home
$location.path '/'
.catch (err) ->
err = err.data;
$scope.errors.other = err.message;
19 changes: 19 additions & 0 deletions templates/coffeescript/controllers/navbar.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

angular.module('<%= scriptAppName %>')
.controller 'NavbarCtrl', ($scope, $location<% if(mongoPassportUser) { %>, Auth<% } %>) ->
$scope.menu = [
title: 'Home'
link: '/'
<% if(mongoPassportUser) { %>,
title: 'Settings',
link: '/settings'
<% } %>]
<% if(mongoPassportUser) { %>
$scope.logout = ->
Auth.logout()
.then ->
$location.path('/login')
<% } %>
$scope.isActive = (route) ->
route is $location.path()
17 changes: 17 additions & 0 deletions templates/coffeescript/controllers/settings.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

angular.module('<%= scriptAppName %>')
.controller 'SettingsCtrl', ($scope, User, Auth) ->
$scope.errors = {}

$scope.changePassword = (form) ->
$scope.submitted = true

if form.$valid
Auth.changePassword($scope.user.oldPassword, $scope.user.newPassword)
.then(->
$scope.message = 'Password successfully changed.'
).catch( ->
form.password.$setValidity 'mongoose', false
$scope.errors.other = 'Incorrect password'
)
27 changes: 27 additions & 0 deletions templates/coffeescript/controllers/signup.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

angular.module('<%= scriptAppName %>')
.controller 'SignupCtrl', ($scope, Auth, $location) ->
$scope.user = {}
$scope.errors = {}

$scope.register = (form) ->
$scope.submitted = true

if form.$valid
Auth.createUser(
name: $scope.user.name
email: $scope.user.email
password: $scope.user.password
).then( ->
# Account created, redirect to home
$location.path '/'
).catch( (err) ->
err = err.data
$scope.errors = {}

# Update validity of form fields that match the mongoose errors
angular.forEach err.errors, (error, field) ->
form[field].$setValidity 'mongoose', false
$scope.errors[field] = error.type
)
13 changes: 13 additions & 0 deletions templates/coffeescript/directives/mongooseError.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

###
Removes server error when user updates input
###
angular.module('<%= scriptAppName %>')
.directive 'mongooseError', ->
restrict: 'A'
require: 'ngModel'
link: (scope, element, attrs, ngModel) ->
element.on 'keydown', ->
ngModel.$setValidity 'mongoose', true

Loading