如何在使用angularjs登录后存储访问令牌 [英] how to store access token after login using angularjs
问题描述
我在登录后获得访问令牌,但我想在登录angularjs后存储访问令牌。
这是angularjs中的LoginController:
(function(){
' use strict';
angular.module(' MyApp') // 从先前创建的角度模块扩展在第一部分
.controller(' LoginController',函数($ scope,$ location,$ window,LoginService)
{
$ scope.IsLogedIn = false;
$ scope.Message = ' ';
$ scope.Submitted = false;
$ scope.IsFormValid = false;
$ scope.User = {
UserName:' ',
密码:' '
};
// 检查表格是否有效//这里f1是我们的表格姓名
$ 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){
调试器;
如果(d.data.AccessToken!= null){
$ scope.IsLogedIn = true;
$ scope .Message = 成功登录。欢迎;
$ window.location.href = / Home / index;
}
else {
$ scope.Message = 无效的用户名或密码;
}
});
}
};
})
.factory(' LoginService',function($ http,$ q,$ window){
var fac = {};
debugger;
fac.GetUser = function(d){
return $ http({
url:' api / users / login',
方法:' POST',
data:JSON.stringify(d),
header:{' content-type' :' application / json'}
});
};
返回 fac;
});
})();
这个是我登录的观点:
< body ng-app =MyApp>
< input type =submitname =submitvalue =了解更多类=btn-more>
< form class =form-loginnovalidate name =f1data-ng-submit =Login()>
登录
< label for =login > 用户名< / label>
< input type =textdata-ng-model =User.UserNamename =loginplaceholder =用户名或电子邮件ng-class =已提交?'ng-dirty':''需要自动对焦/>
需要用户名 >
< label for =password> 密码< / label>
< input type =passworddata-ng-model =User.Passwordname =password占位符=密码class =showpasswordng-class =已提交?'ng-dirty':''需要自动对焦/>
需要密码
< input type =checkboxname =rememberid =remember>< label for =remember >记住密码< / label>
< input type =提交value =登录class =login-btn>
OR
< / form>
@section scripts {
< script src =〜 /app/Controllers/LoginController.js\"></script>
}
< / body>
请给我正确的存储访问令牌代码。
取值cope,
location,
window,LoginService)
{
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()">
Log in
<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.
scope,
location,
window, LoginService) {
这篇关于如何在使用angularjs登录后存储访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!