mrmadou
mrmadou

Reputation: 101

pass an object from controller to another controller angularjs

after Signup, how to be automatically redirected to the authentificate page and put the login and the password in the form of authenficate.html ?

the signupCtrl

demoApp.controller('signupCtrl',function($scope,$http,$location,User) {

    User=$scope.user;
    $scope.verification = function(isValid,User) {

        User=$scope.user;

        if (isValid) {
            $scope.message = "Submitted ";
            console.log(User);
            console.log(User.type);
            if(User.type=="recruteur")
            {
                window.alert("bonjour recruteur "+ User.email);
                $location.path("/auth");

            }
            else {

                window.alert("bonjour condidat "+ User.email);
                $location.path("/auth");
            }

        } else {
            $scope.message = "There are still invalid fields below";

        }
    };
});

the SINGUP.html

<body ng-controller="signupCtrl">
    <form name="registrationForm" novalidate
          ng-submit="submit(registrationForm.$valid)">

        <div class="form-group">
            <label>Email</label>
            <input type="email" name="email" class="form-group"
                   ng-model="user.email"
                   required />
        </div>

        <div>
            <label>Password</label>
            <input type="password" name="password"
                   ng-model="user.password"
                   required/>
        </div>
        <div>
            <button type="submit"               ng-click="verification(registrationForm.$valid,registrationForm.user)">Register!</button>
        </div>
    </form>
</body>

controller authentificate:

demoApp.controller('personCtrl',function ($scope, $http,$location,User) {
    console.log(User);
     var person=User;
    $scope.emaill=person.email;
    $scope.pwd=person.password;
   console.log(person);
});

authentificate page:

<body>
<form>
    <input type="text" ng-model="emaill">
    <input type="password" ng-model="pwd">
    <input type="submit" value="Connexion" ng-click="connexion(person)">

</form>
</body>

the route provider

demoApp.config(['$routeProvider',

    function($routeProvider){
        //systeme de routage
        console.log('rederction');
        $routeProvider.when ('/auth',{templateUrl: 'authentficate.html',
            controller: 'personCtrl'})

            .otherwise({redirectTo: 'Signup.html'}
        );
    }
]);

Upvotes: 0

Views: 51

Answers (1)

Mathias F
Mathias F

Reputation: 15931

You can put the data that is needed for the session in a Service.

https://docs.angularjs.org/guide/services

But dont put the Password there, only the Username and an Authentication token.

Check here for a complete sample for an token based authentication

https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec

Upvotes: 1

Related Questions