0

I try to create a login app using Angularjs that storing information in $cookies. Once the user is logged in, they don't have to log in again:

This is login form:

<form action="/" id="login">
    <div class="form-group">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" class="form-control" ng-model="username" required />

    </div>
    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" class="form-control" ng-model="password" required />
    </div>
    <div class="form-actions">
        <button id="submit_btn" type="button" ng-click="submit()" class="btn btn-danger">Login</button>

    </div>
</form> 

This is the app.js

var app = angular.module('mailApp', ['angular.chosen','ngRoute','ngCookies'], function(){});

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    $routeProvider.
        when('/', {
            templateUrl: 'login.html',
        }).
        when('/logs/', {
            templateUrl: 'index.html',
            controller: 'mailController',
        }).
        otherwise({
            redirectTo: '/'
        });

    }]);

app.controller('mailController', function($scope, $http,$routeParams,$location,domainService, $rootScope, $location, $cookies)
{
    $rootScope = false;
    $scope.submit = function (info) {

        if($scope.username == '123' && $scope.password == '123') {
            $rootScope.loggedIn = true;
            $location.path('/logs/');
        }
        else{
           alert('Wrong authentication!');
            }
        }
//... 
}

But I am new to angularjs and not really got to $cookies.

Any help would be appreciated!

Thanks, guys!

4

0 回答 0