Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
}
},
Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"bootstrap-sass-official": "3.1.1",
"firebase": "1.0.15",
"angularfire": "0.7.1",
"angularjs-gravatardirective": "1.3.2"
"angularjs-gravatardirective": "1.3.2",
"angular-flash": "0.1.13"
},
"devDependencies": {
"angular-mocks": "1.2.16"
}
}
}
2 changes: 1 addition & 1 deletion web/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h1>All Time on PeerPerks</h1>
</div>
<div class="col-sm-5 col-xs-12">
<div class="pull-right">
<button ng-hide="user" type="button" class="btn btn-success btn-lg" ng-click="login()">Login</button>
<a href="/login"><button ng-hide="user" type="button" class="btn btn-success btn-lg">login or sign up</button></a>

<div class="btn-group">
<button ng-hide="!user || selectReward || selectPerk" type="button" class="btn btn-primary btn-lg" ng-click="selectReward = !selectReward">Reward</button>
Expand Down
12 changes: 10 additions & 2 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">PeerPerks <span class="glyphicon glyphicon-thumbs-up"></span></a>
<a class="navbar-brand" href="/">PeerPerks <span class="glyphicon glyphicon-thumbs-up"></span></a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
Expand All @@ -29,7 +29,15 @@
</div>

<div class="container">


<div flash-alert="success" active-class="in alert" class="fade">
<span class="alert-message">{{flash.message}}</span>
</div>

<div flash-alert="error" active-class="in alert" class="fade">
<span class="alert-message">{{flash.message}}</span>
</div>

<ng-view></ng-view>

</div>
Expand Down
141 changes: 74 additions & 67 deletions web/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand All @@ -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;
};

Expand Down
89 changes: 89 additions & 0 deletions web/js/services/service.auth.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
}


});
Loading