Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.controller('AuthController', function($auth, $state,$http,$rootScope, $scope) {
- $scope.email='';
- $scope.password='';
- $scope.newUser={};
- $scope.loginError=false;
- $scope.loginErrorText='';
- $scope.login = function() {
- var credentials = {
- email: $scope.email,
- password: $scope.password
- }
- $auth.login(credentials).then(function() {
- return $http.get('api/authenticate/user');
- }, function(error) {
- $scope.loginError = true;
- $scope.loginErrorText = error.data.error;
- }).then(function(response) {
- // var user = JSON.stringify(response.data.user);
- // localStorage.setItem('user', user);
- $rootScope.authenticated = true;
- $rootScope.currentUser = response.data.user;
- $scope.loginError = false;
- $scope.loginErrorText = '';
- $state.go('todo');
- });
- }
- $scope.register = function () {
- $http.post('/api/register',$scope.newUser)
- .success(function(data){
- $scope.email=$scope.newUser.email;
- $scope.password=$scope.newUser.password;
- $scope.login();
- })
- };
- });
- angular.module('apiFromApp')
- .factory('AuthServices', AuthServices);
- AuthServices.inject = ['$http','$q', '$sanitize', '$auth', '$rootScope', '$state', 'toastr', 'CONFIG'];
- function AuthServices($http, $q, $sanitize, $auth, $rootScope, $state, toastr, CONFIG) {
- var service = {};
- // Service logic
- var sanitizeCredentials = function (credentials) {
- return {
- email: $sanitize(credentials.email),
- password: $sanitize(credentials.password)
- };
- };
- service.login = function (data) {
- data = sanitizeCredentials(data);
- $auth.login(data).then(function() {
- return $http.get(CONFIG.APIURL + 'api/authenticate/user').then(function(response) {
- var user = JSON.stringify(response.data.user);
- localStorage.setItem('user', user);
- $rootScope.authenticated = true;
- $rootScope.currentUser = response.data.user;
- $state.go('main');
- });
- // Handle errors
- }, function(error) {
- toastr.error(error.data.error, error.statusText);
- });
- }
- var sanitizeDatesRegister = function (newUser) {
- return {
- username: $sanitize(newUser.username),
- email: $sanitize(newUser.email),
- password: $sanitize(newUser.password)
- };
- };
- /**
- * [register description]
- * @param {[type]} data [description]
- * @return {[type]} [description]
- */
- service.register = function (data) {
- var userNew = sanitizeDatesRegister(data);
- $http.post(CONFIG.APIURL + 'api/authenticate/sinup',userNew)
- .success(function(data){
- }).error(function (err) {
- console.log(err);
- })
- }
- return service;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement