Skip to content

Instantly share code, notes, and snippets.

@CiscoKidxx
Created November 11, 2015 17:54
Show Gist options
  • Save CiscoKidxx/48ae6fdff531decc6e48 to your computer and use it in GitHub Desktop.
Save CiscoKidxx/48ae6fdff531decc6e48 to your computer and use it in GitHub Desktop.
angular.module('app').controller('Signup', function ($scope, $http, toastr, $location, $auth, AuthService) {
$scope.signupForm = {
loading: false
}
$scope.submitSignupForm = function(){
// Set the loading state (i.e. show loading spinner)
$scope.signupForm.loading = true;
var email = $scope.signupForm.email.toLowerCase();
var password = $scope.signupForm.password;
var company = $scope.signupForm.company.toLowerCase();
var name = $scope.signupForm.name.toLowerCase();
AuthService.signup(email, password, company, name)
.then(function onSuccess(res){
$auth.setToken(res.data.token)
$location.path('/dashboard')
AuthService.currentUser = res.data.user
})
.catch(function onError(res){
// Handle known error type(s).
// If using sails-disk adpater -- Handle Duplicate Key
var emailAddressAlreadyInUse = res.status == 409;
if (emailAddressAlreadyInUse) {
toastr.error('That email address has already been taken, please try again.', 'Error');
return;
}
})
.finally(function eitherWay(){
$scope.signupForm.loading = false;
})
}
$scope.submitLoginForm = function (){
// Set the loading state (i.e. show loading spinner)
$scope.loginForm.loading = true;
// Submit request to Sails.
$http.put('/api/user/login', {
email: $scope.loginForm.email.toLowerCase(),
password: $scope.loginForm.password
})
.then(function onSuccess (res){
// Refresh the page now that we've been logged in.
$auth.setToken(res.data.token)
$location.path('/dashboard')
AuthService.currentUser = "res.data.user";
console.log(res.data.user);
})
.catch(function onError(sailsResponse) {
// Handle known error type(s).
// Invalid username / password combination.
if (sailsResponse.status === 400 || 404) {
// $scope.loginForm.topLevelErrorMessage = 'Invalid email/password combination.';
//
toastr.error('Invalid email/password combination.', 'Error', {
closeButton: true
});
return;
}
toastr.error('An unexpected error occurred, please try again.', 'Error', {
closeButton: true
});
return;
})
.finally(function eitherWay(){
$scope.loginForm.loading = false;
});
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment