davidbaiguini
10/23/2015 - 2:53 AM

How to authenticate using AngularJS

How to authenticate using AngularJS

<?php

class Api_V1_Auth_Controller extends Base_Controller
{
  public function get_index()
	{
		try {
			$user = array();
			if (Auth::check()) {
				$user = Auth::user();
				$user->image = read_image($user->image);
			}

			if ($user)
				$user->password = null;

			$data = array(
				'user' => to_json($user),
			);

			return json($data);
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	public function get_logout()
	{
		try {
			return Auth::logout();
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	public function post_login()
	{
		try {
			$s = extend(array(
				'username' => '',
				'password' => '',
			), sr());

			if (! Auth::attempt($s))
				throw new Exception("Incorrect email or password.");

			if (Auth::user()->account_user_id == 0 || Auth::user()->deleted)
				throw new Exception("We can't find the account you are associated with.");

			$data = array(
				'success' => ''
			);

			return json($data);
		} catch(Exception $e) {
			Auth::logout();

			$data = array(
				'error' => $e->getMessage()
			);

			return json($data);
		}
	}

	public function post_register()
	{
		try {
			$s = extend(array(
				'serie_id' => 0,
				'name' => '',
				'email' => '',
				'password' => '',
				'terms' => '0',
			), sr());

			$v = Validator::make(array(
				'email' => $s['email'],
				'password' => $s['password'],
				'terms' => $s['terms'],
			), array(
				'email' => 'email|required|unique:users',
				'password' => 'required|min:6',
				'terms' => 'accepted|required',
			));

			if ($v->fails()) {
				foreach ($v->errors->messages as $key => $value) {
					$error = $value[0];
					break;
				}

				throw new Exception($error);
			}

			$user = User::create(array(
				'name' => $s['name'],
				'email' => $s['email'],
				'password' => Hash::make($s['password']),
				'type' => 'account',
				'perm_contacts' => 0,
			));

			$user->account_user_id = $user->id;
			$user->save();
			Auth::login($user->id, 1);

			$serie = Serie::find($s['serie_id']);
			if (! is_null($serie))
				if (! $serie->is_member($user->id) && ! $serie->is_expert($user->id))
					$serie->create_member($user->id);

			$data = array(
				'success' => true,
			);

			return json($data);
		} catch(Exception $e) {
			$data = array(
				'error' => $e->getMessage()
			);

			return json($data);
		}
	}

	public function get_locations()
	{
		try {
			$data = array(
				'locations' => to_json(Auth::user()->locations),
			);

			return json($data);
		} catch(Exception $e) {
			Report::log($e->getMessage());
		}
	}

	public function get_check()
	{
		try {
			$data = array(
				'check' => Auth::check(),
			);

			return json($data);
		} catch(Exception $e) {
			Report::log($e->getMessage());
		}
	}
}
services.factory('Auth', function($http){
  return {
	    load: function() {
			return $http.get('/api/v1/auth');
		},
	    logout: function() {
			return $http.get('/api/v1/auth/logout');
		},
		login: function(inputs) {
			return $http.post('/api/v1/auth/login', inputs);
		},
		register: function(inputs) {
			return $http.post('/api/v1/auth/register', inputs);
		},
		locations: function() {
			return $http.get('/api/v1/auth/locations');
		},
		check: function() {
			return $http.get('/api/v1/auth/check');
		}
	}
});
controllers.controller('MainCtrl', function($scope, $location, Facebook, $rootScope, $http, $location, Upload, Auth, User, Question, Category, Serie, Record, Location, Popup, Process, Card, Question) {
  $scope.$on('authLoaded', function() {
		$scope.isExpert($scope.main.serieId);
		$scope.isMember($scope.main.serieId);
	});

	$scope.loadAuth = function() {
		Auth.load().success(function(data) {
			$scope.main.user = data.user;
			$scope.$broadcast("authLoaded");
			Popup.close();
		});
	}
  
  
	$scope.logoutUser = function() {
		Auth.logout().success(function(data) {
			toastr.info("You have been logged out.");
			$scope.main.user = {};
		});
	}

	$scope.loginUser = function() {
		Auth.login({
			username: $scope.main.credentials.email,
			password: $scope.main.credentials.password
		}).success(function(data) {
			if (data.error) {
				toastr.error(data.error);
			} else {
				toastr.success("You are signed in!");
				$scope.loadAuth();
				$scope.main.credentials = {};
				Popup.close();
			}
		});
	}

	$scope.registerUser = function() {
		Auth.register({
			serie_id: $scope.main.serieId,
			email: $scope.newUser.email,
			password: $scope.newUser.password,
			terms: $scope.newUser.terms,
			name: $scope.newUser.name,
		}).success(function(data) {
			if (data.error) {
				toastr.error(data.error);
			}

			if (data.success) {
				toastr.success("Welcome to " + $scope.main.serie.name + "!");
				$scope.loadAuth();
				$scope.newUser = {};
				Popup.close();
			}
		});
	}
 	
 	$scope.loadAuth();
	$scope.loadSerie();
});