Click here to Skip to main content
14,691,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am getting access token after login but I want to store access token after login in angularjs.

This is LoginController in angularjs :

(function () {
'use strict';
angular.module('MyApp') // extending from previously created angular module in the First Part
.controller('LoginController',function ($scope,$location,$window, LoginService)
{

    $scope.IsLogedIn = false;
    $scope.Message = '';
    $scope.Submitted = false;
    $scope.IsFormValid = false;

    $scope.User = {
        UserName: '',
        Password: ''
    };

    //Check is Form Valid or Not // Here f1 is our form Name
    $scope.$watch('f1.$valid', function (newVal) {
        $scope.IsFormValid = newVal;
    });

    $scope.Login = function () {
        $scope.Submitted = true;
        if ($scope.IsFormValid) {
            LoginService.GetUser($scope.User).then(function (d) {
                debugger;
                if (d.data.AccessToken != null) {
                    $scope.IsLogedIn = true;
                    $scope.Message = "Successfully login done. Welcome ";
                    $window.location.href = "/Home/index";
                   }
                else {
                    $scope.Message = "Invalid UserName or Password";
                }
            });
        }
    };

})

.factory('LoginService', function ($http,$q,$window) {
    var fac = {};
    debugger;
    fac.GetUser = function (d) {
        return $http({
            url: 'api/users/login',
            method: 'POST',
            data: JSON.stringify(d),
            headers: { 'content-type': 'application/json' }
        });
    };
    return fac;

});


})();



And This is my view for login :

<body ng-app="MyApp">





<input type="submit" name="submit" value="Know More" class="btn-more">



<form class="form-login" novalidate name="f1" data-ng-submit="Login()">
{{Message}}



<label for="login">Username</label>
<input type="text" data-ng-model="User.UserName" name="login" placeholder="Username or email" ng-class="Submitted?'ng-dirty':''" required autofocus />
Username required



<label for="password">Password</label>
<input type="password" data-ng-model="User.Password" name="password" placeholder="Password" class="showpassword" ng-class="Submitted?'ng-dirty':''" required autofocus />
Password required





<input type="checkbox" name="remember" id="remember"><label for="remember">Remember Password</label>

<input type="submit" value="Login" class="login-btn">




Forgot Password? New user? Register



OR





Log in with Facebook Log in with Twitter


</form>





@section scripts{
<script src="~/app/Controllers/LoginController.js"></script>

}


</body>

Please give me correct code for storing access token.
Posted

Html 5 supports application cache. You could store this data in the cache[^].
Alternately, you could store data in cookies.

However, both these options come with security risks that need to be taken care of during implementation.
   
User to existing session storage for that:

$window.sessionStorage.token = 'your token'

It will remain till the time browser is not closed or session timeout occur.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900