2

I'm trying to develop a login with cookies check if a user is already logged or not.

In order, I include the app:

angular.module('ELOAuthentication', [
    'AuthenticationService',
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])

Then the service

angular.module('ELOAuthentication').factory('AuthenticationService', function ($rootScope, $scope, $http) {

    var service = {};

    service.Login = function (email, password, callback) {
        var Url = '/api/user/GetLoginByEmailPassword';

        $http.then(Url, { email: email, password: password }).success(
            function (response) {
                var data = response.data
                callback(data);
            }).catch(function (error) {
                console.log('ERROR GetLoginByEmailPassword: ' + error);
            });
    }

    service.SetCookie = function (email, password) {

    };

    service.ClearCookie = function () {

    };
});

Finally the AngularJS controller.

angular.module('ELOAuthentication').controller('LoginController', function ($rootScope, $scope, $http) {

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});

I receive the error:

Uncaught Error: [$injector:modulerr]

. What is wrong?

5
  • 3
    angular.module('ELOAuthentication', [ 'ngRoute', 'ngCookies', 'angular-loading-bar' ]) no need to include AuthenticationService in here Commented Dec 4, 2017 at 13:01
  • 1
    Services have to be injected in controllers Commented Dec 4, 2017 at 13:02
  • 1
    Also, you would have to return service at the end of factory. Commented Dec 4, 2017 at 13:02
  • 1
    and angular.module('ELOAuthentication').controller('LoginController', function ($rootScope, $scope, $http,AuthenticationService) { AuthenticationService you should add this Commented Dec 4, 2017 at 13:03
  • this is some old syntax, but shouldn't $http.then(...) be $http.post(...) instead? Commented Dec 4, 2017 at 13:14

1 Answer 1

5

You don't need to inject Services in your module.

app.js

angular.module('ELOAuthentication', [
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])

LoginController.js

angular.module('ELOAuthentication').controller('LoginController', function ($rootScope, $scope, $http, AuthenticationService) {

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});

As mentioned by Sajal, don't forget to return your service object:

AuthenticationService.js

angular.module('ELOAuthentication').factory('AuthenticationService', function ($rootScope, $scope, $http) {

    var service = {};

    service.Login = function (email, password, callback) {
        var Url = '/api/user/GetLoginByEmailPassword';

        $http.then(Url, { email: email, password: password }).success(
            function (response) {
                var data = response.data
                callback(data);
            }).catch(function (error) {
                console.log('ERROR GetLoginByEmailPassword: ' + error);
            });
    }

    service.SetCookie = function (email, password) {

    };

    service.ClearCookie = function () {

    };

    return service;
});

Official documentation

Sign up to request clarification or add additional context in comments.

11 Comments

Hi. Thanks a lot to all! Still have error: Error: [$injector:unpr] http://errors.angularjs.org/1.6.4/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20AuthenticationService. I don't know why is unknown.
Did you loaded it in your index.html? Something like : <script src="js/authentication.service.js"></script>
Yes, load in order: jquery, angularjs, app, service, controller.
Do you use a module bundler? Gulp/Grunt/Webpack or other?
Ok, kind strange if you have to place run there, but glad it works :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.