From 0c85ec3336fb15a895a68212a251c6ae566f8a3a Mon Sep 17 00:00:00 2001 From: Donal Mac An Ri Date: Mon, 30 Jun 2014 14:19:59 -0230 Subject: [PATCH 1/2] added a simple signup / login form for users without GitHub --- Gruntfile.js | 3 +- bower.json | 31 +++---- web/app.html | 2 +- web/index.html | 12 ++- web/js/client.js | 142 ++++++++++++++++--------------- web/js/services/service.auth.js | 89 ++++++++++++++++++++ web/login.html | 144 ++++++++++++++++++++++++++++++++ 7 files changed, 337 insertions(+), 86 deletions(-) create mode 100644 web/js/services/service.auth.js create mode 100644 web/login.html diff --git a/Gruntfile.js b/Gruntfile.js index 0a7939a..2e4fd36 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -16,7 +16,8 @@ module.exports = function(grunt) { 'web/components/firebase/firebase.js', 'web/components/firebase-simple-login/firebase-simple-login.js', 'web/components/angularfire/angularfire.min.js', - 'web/components/angularjs-gravatardirective/dist/angularjs-gravatardirective.min.js' + 'web/components/angularjs-gravatardirective/dist/angularjs-gravatardirective.min.js', + 'web/components/angular-flash/dist/angular-flash.min.js' ] } }, diff --git a/bower.json b/bower.json index fe00b83..452040e 100644 --- a/bower.json +++ b/bower.json @@ -1,17 +1,18 @@ { - "name": "peerperks", - "version": "0.0.1", - "dependencies": { - "angular": "1.2.16", - "angular-route": "1.2.16", - "angular-ui-bootstrap": "0.11.0", - "bootstrap": "3.1.1", - "bootstrap-sass-official": "3.1.1", - "firebase": "1.0.15", - "angularfire": "0.7.1", - "angularjs-gravatardirective": "1.3.2" - }, - "devDependencies": { - "angular-mocks": "1.2.16" - } + "name": "peerperks", + "version": "0.0.1", + "dependencies": { + "angular": "1.2.16", + "angular-route": "1.2.16", + "angular-ui-bootstrap": "0.11.0", + "bootstrap": "3.1.1", + "bootstrap-sass-official": "3.1.1", + "firebase": "1.0.15", + "angularfire": "0.7.1", + "angularjs-gravatardirective": "1.3.2", + "angular-flash": "~0.1.13" + }, + "devDependencies": { + "angular-mocks": "1.2.16" + } } diff --git a/web/app.html b/web/app.html index 5691384..6f4a128 100644 --- a/web/app.html +++ b/web/app.html @@ -5,7 +5,7 @@

All Time on PeerPerks

- +
diff --git a/web/index.html b/web/index.html index 22ff00e..3a10d1e 100644 --- a/web/index.html +++ b/web/index.html @@ -16,7 +16,7 @@ - PeerPerks + PeerPerks
- + +
+ {{flash.message}} +
+ +
+ {{flash.message}} +
+
diff --git a/web/js/client.js b/web/js/client.js index 25e1af6..b4f3491 100644 --- a/web/js/client.js +++ b/web/js/client.js @@ -5,10 +5,13 @@ var app = angular.module('ngPeerPerks', [ 'config.app', 'ngRoute', 'service.lodash', + 'service.auth', 'service.participant', 'service.activity', 'directive.reward', - 'directive.perk' + 'directive.perk', + 'angular-flash.service', + 'angular-flash.flash-alert-directive' ]) .config(function ($routeProvider, $locationProvider) { @@ -17,22 +20,85 @@ var app = angular.module('ngPeerPerks', [ ; $routeProvider + .when('/login', {controller: 'LoginCtrl', templateUrl: '/login.html'}) .when('/', {controller: 'AppCtrl', templateUrl: '/app.html'}) .otherwise({templateUrl: '/404.html'}) ; }) - - .controller('AppCtrl', function ($scope, $firebaseSimpleLogin, $firebase, _, ParticipantService, ActivityService, API_URL) { - var loginRef = new Firebase(API_URL); - var auth; - + + .controller('LoginCtrl', function($scope, AuthService, ParticipantService, flash, $location) { + $scope.showform = 'login'; + + $scope.loginform = {}; + $scope.signupform = {}; + + $scope.login = function() { + AuthService.login( + $scope.loginform.email, + $scope.loginform.password, + $scope.loginform.rememberme + ).then(function(user) { + flash.success = 'Welcome back, ' + ParticipantService.$child(user.uid).name + '!'; + $location.path('/'); + }, function(error) { + $scope.loginform.errordetail = error.message.replace(/FirebaseSimpleLogin\: /g, ''); + }); + }; + + $scope.signup = function() { + AuthService.signup( + $scope.signupform.name, + $scope.signupform.email, + $scope.signupform.password + ).then(function(user) { + flash.success = 'Welcome to PeerPerks, ' + $scope.signupform.name + '. Thank you for signing up.'; + $location.path('/'); + }, function(error) { + console.log(error); + $scope.signupform.errordetail = error.message.replace(/FirebaseSimpleLogin\: /g, ''); + }); + }; + + $scope.githubLogin = function() { + AuthService.githubLogin() + .then(function(user) { + flash.success = 'Welcome back, ' + ParticipantService.$child(user.uid).name + '!'; + $location.path('/'); + }, function(error) { + flash.error = error.message.replace(/FirebaseSimpleLogin\: /g, ''); + }); + }; + + $scope.forgotPassword = function() { + AuthService.sendPasswordResetEmail($scope.forgotpasswordform.email) + .then(function() { + flash.success = 'We have sent a link to your email that you can use to reset your password.'; + }, function(error) { + flash.error = error.message.replace(/FirebaseSimpleLogin\: /g, ''); + }); + } + + }) + + .controller('AppCtrl', function ($scope, AuthService, $firebase, _, ParticipantService, ActivityService, API_URL) { $scope.error = null; + $scope.participants = ParticipantService; + $scope.participants.$bind($scope, 'remoteParticipants').then(function() { + AuthService.getCurrentUser().then(function(user) { + $scope.user = user; + + if ($scope.user) { + $scope.presence(); + } + }); + }); + $scope.presence = function() { if ($scope.user) { var amOnline = new Firebase(API_URL + '/.info/connected'); - var presenceRef = new Firebase(API_URL + '/participants/' + $scope.user.username + '/status'); - + var presenceRef = new Firebase(API_URL + '/participants/' + $scope.user.uid + '/status'); + amOnline.on('value', function(snapshot) { if (snapshot.val()) { presenceRef.onDisconnect().set('offline'); @@ -42,68 +108,10 @@ var app = angular.module('ngPeerPerks', [ } }; - $scope.participants = ParticipantService; - $scope.participants.$bind($scope, 'remoteParticipants').then(function() { - auth = $firebaseSimpleLogin(loginRef); - auth.$getCurrentUser().then(function(user) { - $scope.user = user; - - if ($scope.user) { - $scope.presence(); - } - }); - }); - $scope.activities = ActivityService; - $scope.login = function() { - auth.$login('github', { - rememberMe: true, - scope: 'user:email' - }).then(function(user) { - $scope.user = user; - - $scope.presence(); - - // successful login - $scope.error = null; - - var participant = _.find($scope.participants, function(participant) { - return (participant.username === $scope.user.username); - }); - - // if not participating yet, then add the user - if (!participant) { - $scope.participants.$child($scope.user.username).$set({ - email: ($scope.user.thirdPartyUserData.email) ? $scope.user.thirdPartyUserData.email : $scope.user.thirdPartyUserData.emails[0].email, - name: ($scope.user.displayName) ? $scope.user.displayName : $scope.user.username, - username: $scope.user.username, - points: { - current: 0, - allTime: 0, - redeemed: 0, - perks: 0 - } - }).then(function() { - $scope.presence(); - - var participantRef = new Firebase(API_URL + '/participants/' + $scope.user.username); - var participant = $firebase(participantRef); - participant.$priority = 0; - participant.$save(); - }); - } - - $scope.error = null; - }, function(error) { - $scope.error = error.message; - }); - }; - $scope.logout = function() { - var presenceRef = new Firebase(API_URL + '/participants/' + $scope.user.username + '/status'); - presenceRef.set('offline'); - auth.$logout(); + AuthService.logout(); $scope.user = null; }; diff --git a/web/js/services/service.auth.js b/web/js/services/service.auth.js new file mode 100644 index 0000000..dca5ca6 --- /dev/null +++ b/web/js/services/service.auth.js @@ -0,0 +1,89 @@ +'use strict'; + +angular + .module('service.auth', [ + 'firebase', + 'config.app', + 'service.participant', + 'angular-flash.service', + 'service.lodash' + ]) + .factory('AuthService', function(_, $firebase, $firebaseSimpleLogin, ParticipantService, API_URL) { + + var loginRef = new Firebase(API_URL); + var auth = $firebaseSimpleLogin(loginRef); + + return { + getCurrentUser: auth.$getCurrentUser, + sendPasswordResetEmail: auth.$sendPasswordResetEmail, + login: function(email, password, rememberMe) { + return auth.$login('password', { + email: email, + password: password, + rememberMe: rememberMe + }); + }, + signup: function (name, email, password) { + return auth.$createUser(email, password) + .then(function(user) { + authHandler(user.uid, name, email); + + return auth.$login('password', { + email: email, + password: password + }); + }); + }, + githubLogin: function() { + return auth.$login('github', { + rememberMe: true, + scope: 'user:email' + }).then(function(user) { + var name, email; + + email = (user.thirdPartyUserData.email) ? user.thirdPartyUserData.email : user.thirdPartyUserData.emails[0].email; + name = (user.displayName) ? user.displayName : user.username; + + authHandler(user.uid, name, email, user.username); + + return user; + }); + }, + logout: function() { + auth.$getCurrentUser().then(function(user) { + var presenceRef = new Firebase(API_URL + '/participants/' + user.uid + '/status'); + presenceRef.set('offline'); + auth.$logout(); + }); + } + }; + + function authHandler(uid, name, email, username) { + var participant = _.find(ParticipantService, function(participant) { + return (participant.email === email || participant.username === username); + }); + + // if not participating yet, then add the user + if (!participant) { + ParticipantService.$child(uid).$set({ + email: email, + name: name, + username: username, + points: { + current: 0, + allTime: 0, + redeemed: 0, + perks: 0 + } + }).then(function() { + // make sure logged in user appears on top + var participantRef = new Firebase(API_URL + '/participants/' + uid); + var participant = $firebase(participantRef); + participant.$priority = 0; + participant.$save(); + }); + } + } + + + }); \ No newline at end of file diff --git a/web/login.html b/web/login.html new file mode 100644 index 0000000..68c9c91 --- /dev/null +++ b/web/login.html @@ -0,0 +1,144 @@ +
+
+
+
+
Sign In
+ +
+ +
+ +
{{loginform.errordetail}}
+ +
+ +
+ + +
+ +
+ + +
+ +
+
+ +
+
+ + +
+ + + +
+ + +
+
+
+ Don't have an account! + + Sign Up Here + +
+
+
+
+ + + +
+
+
+
+
+
+
Sign Up
+ +
+
+
+ +
{{signupform.errordetail}}
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+ +
+ + or +
+
+ +
+ +
+ +
+ +
+ + + +
+
+
+
+
+
+
+
Forgot Password?
+
+ +
+ +
+ +

Enter your email address below:

+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+
+
+
\ No newline at end of file From 580f2cd2565b922ef8a7aa43e37f580bb49d3155 Mon Sep 17 00:00:00 2001 From: Donal Mac An Ri Date: Mon, 30 Jun 2014 14:24:43 -0230 Subject: [PATCH 2/2] - removed console.log - bower json switched back to tabs --- bower.json | 32 ++++++++++++++++---------------- web/js/client.js | 1 - 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/bower.json b/bower.json index 452040e..8e44d42 100644 --- a/bower.json +++ b/bower.json @@ -1,18 +1,18 @@ { - "name": "peerperks", - "version": "0.0.1", - "dependencies": { - "angular": "1.2.16", - "angular-route": "1.2.16", - "angular-ui-bootstrap": "0.11.0", - "bootstrap": "3.1.1", - "bootstrap-sass-official": "3.1.1", - "firebase": "1.0.15", - "angularfire": "0.7.1", - "angularjs-gravatardirective": "1.3.2", - "angular-flash": "~0.1.13" - }, - "devDependencies": { - "angular-mocks": "1.2.16" - } + "name": "peerperks", + "version": "0.0.1", + "dependencies": { + "angular": "1.2.16", + "angular-route": "1.2.16", + "angular-ui-bootstrap": "0.11.0", + "bootstrap": "3.1.1", + "bootstrap-sass-official": "3.1.1", + "firebase": "1.0.15", + "angularfire": "0.7.1", + "angularjs-gravatardirective": "1.3.2", + "angular-flash": "0.1.13" + }, + "devDependencies": { + "angular-mocks": "1.2.16" + } } diff --git a/web/js/client.js b/web/js/client.js index b4f3491..985ca43 100644 --- a/web/js/client.js +++ b/web/js/client.js @@ -54,7 +54,6 @@ var app = angular.module('ngPeerPerks', [ flash.success = 'Welcome to PeerPerks, ' + $scope.signupform.name + '. Thank you for signing up.'; $location.path('/'); }, function(error) { - console.log(error); $scope.signupform.errordetail = error.message.replace(/FirebaseSimpleLogin\: /g, ''); }); };