TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sivakumar
NA
551
210.5k
How to store access token after login in angularjs
Nov 18 2015 9:15 AM
Hi,
I am getting access token after login, but I want to store access token using angularjs.
This is my angular controllor :
LoginController :
(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;
});
})();
This is my login view :
<body ng-app="MyApp">
<div class="container" ng-controller="LoginController">
<section class="main">
<div class="col-lg-5 col-md-6 col-sm-12">
<form class="form-login" novalidate name="f1" data-ng-submit="Login()">
<div style="color:#8e0202">{{Message}}</div>
<h1><span class="log-in">Log in</span></h1>
<p class="float col-md-12">
<label for="login"><i class="icon-user"></i>Username</label>
<input type="text" data-ng-model="User.UserName" name="login" placeholder="Username or email" ng-class="Submitted?'ng-dirty':''" required autofocus />
<span class="error" ng-show="(f1.login.$dirty || Submitted) && f1.login.$error.required">Username required</span>
</p>
<p class="float col-md-12">
<label for="password"><i class="icon-lock"></i>Password</label>
<input type="password" data-ng-model="User.Password" name="password" placeholder="Password" class="showpassword" ng-class="Submitted?'ng-dirty':''" required autofocus />
<span class="error" ng-show="(f1.password.$dirty || Submitted) && f1.password.$error.required">Password required</span>
</p>
<p class="clearfix"></p>
<p class="clearfix">
<span class="col-lg-6 pull-left">
<input type="checkbox" name="remember" id="remember"><label for="remember">Remember Password</label>
</span>
<span class="col-lg-6 text-right"> <input type="submit" value="Login" class="login-btn"></span>
</p>
<p class="clearfix"></p>
<p class="clearfix"> <span class="col-lg-6"><a href="#">Forgot Password?</a></span> <span class="col-lg-6 text-right">New user? <a href="#"> Register</a></span> </p>
<div class="text-center">
<h3> <em>OR</em> </h3>
</div>
<hr />
<p class="clearfix"> <span class="col-lg-6"><a href="#" class="log-twitter"><i class="fa icon-facebook"></i> Log in with Facebook</a></span> <span class="col-lg-6"><a href="#" class="log-fb"><i class="fa icon-twitter"></i> Log in with Twitter</a></span> </p>
</form>
</div>
<br style="clear:both;" />
</section>
</div>
@section scripts{
<script src="~/app/Controllers/LoginController.js"></script>
}
Please tell me how to store access token after login.
Thank you.
Reply
Answers (
0
)
javascript
Nunit test design