client/app/auth/auth_controller.js
author rougeronj
Thu, 04 Jun 2015 20:09:20 +0200
changeset 113 b8400d8efac8
parent 100 537d330ad7f0
child 169 76fb2dd72ce4
permissions -rw-r--r--
uodate authentication to log with email

(function(){
    'use strict';

    angular.module('ammicoAuth',['ngRoute'])
    .controller('authCtrl', function($location, $rootScope, $routeParams, $modal){
        $modal.open({
            templateUrl: 'auth/' + $routeParams.action + '_modal.html',
            controller: 'auth' + $routeParams.action + 'Ctrl'
        }).result.then(function (result) {
            $location.path(result);
        }, function () {
            $location.path('/');
        });
    })
    .controller('authloginCtrl', function($modalInstance, $scope, authApi, $window, $http, $rootScope){
        $scope.login = function(){
            authApi.login.save({email:$scope.email, password:$scope.password}, function(data){
                $rootScope.globals.userLogged = true;
                localStorage.setItem('token', data.token);
                $http.defaults.headers.common.Authorization = 'Token ' + localStorage.getItem('token');
                $modalInstance.close('/');
            });
        };
        $scope.register = function () {
            $modalInstance.close('/auth/register');
        };
        $scope.cancel = function () {
            $modalInstance.close('/');
        };
    })
    .controller('authlogoutCtrl', function($modalInstance, $scope, $window, $http, $rootScope){
        localStorage.removeItem('token');
        $rootScope.globals.userLogged = false;
        delete $http.defaults.headers.common.Authorization;
        $scope.cancel = function () {
            $modalInstance.close('/');
        };
    })
    .controller('authregisterCtrl', function($scope, $modalInstance, authApi){
        $scope.register = function () {
            authApi.user.save({
                email: $scope.email,
                password: $scope.password,
            }, function(){
                $modalInstance.close('/auth/login');
            });
        };
        $scope.back = function () {
            $modalInstance.close('/auth/login');
        };
    });
})();