Skip to content

Instantly share code, notes, and snippets.

@ishaadX
Forked from aaronksaunders/index.html
Last active August 29, 2015 14:27
Show Gist options
  • Save ishaadX/df4b68f61e627f3e254d to your computer and use it in GitHub Desktop.
Save ishaadX/df4b68f61e627f3e254d to your computer and use it in GitHub Desktop.
PARSE.COM AND ANGULARJS FOR EASY SETUP OF USER AUTHENTICATION - http://popdevelop.com/2013/11/parse-com-and-angularjs-for-easy-setup-of-user-authentication/
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="http://www.parsecdn.com/js/parse-1.2.12.min.js"></script>
<meta charset=utf-8 />
<title>Example</title>
</head>
<body ng-app="AuthApp">
<div ng-hide="currentUser">
<form ng-show="scenario == 'Sign up'">
<h2>Sign up</h2>
Email: <input type="email" ng-model="user.email" /><br />
Username: <input type="text" ng-model="user.username" /><br />
Password: <input type="password" ng-model="user.password" /><br />
<button ng-click="signUp(user)">Sign up</button>
or <a href="#" ng-click='scenario="Log in"'>Log in</a>
</form>
<form ng-show="scenario == 'Log in'">
<h2>Log in</h2>
Username: <input type="text" ng-model="user.username" /><br />
Password: <input type="password" ng-model="user.password" /><br />
<button ng-click="logIn(user)">Log in</button>
or <a href="#" ng-click='scenario="Sign up"'>Sign up</a>
</form>
</div>
<div ng-show="currentUser">
<h1>Welcome {{currentUser.get('username')}}</h1>
<p> You have been successfully logged in</p>
<button ng-click="logOut(user)">Log out</button>
</div>
</body>
</html>
Parse.initialize("YJwHjwYF08scjC2SEdl9Ve2YFB3056FQfkUgGu8p", "awDPDR2YNfZNtbJ54VzEZPiWJZmLekU8qkJxnlOw");
angular.module('AuthApp', [])
.run(['$rootScope', function($scope) {
$scope.scenario = 'Sign up';
$scope.currentUser = Parse.User.current();
$scope.signUp = function(form) {
var user = new Parse.User();
user.set("email", form.email);
user.set("username", form.username);
user.set("password", form.password);
user.signUp(null, {
success: function(user) {
$scope.currentUser = user;
$scope.$apply();
},
error: function(user, error) {
alert("Unable to sign up: " + error.code + " " + error.message);
}
});
};
$scope.logIn = function(form) {
Parse.User.logIn(form.username, form.password, {
success: function(user) {
$scope.currentUser = user;
$scope.$apply();
},
error: function(user, error) {
alert("Unable to log in: " + error.code + " " + error.message);
}
});
};
$scope.logOut = function(form) {
Parse.User.logOut();
$scope.currentUser = null;
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment