-
+
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..985ca43 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,84 @@ 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) {
+ $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 +107,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 @@
+
+
+
+
+
+
+
+
{{loginform.errordetail}}
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file