I have created a factory service in angularjs, which has an authentication service in it. If the authentication succeeded then it generates a token which is required for the dashboard services. I am able to generate the token but not understand how to use it.
Is it a good idea to create a factory service to get a token so I should inject in controllers as needed?
login.html:
<div ng-app="loginFormApp" ng-controller="loginFormCtrl">
<form method="post" action="" id="login_form" class="row">
<input type="text" placeholder="Login ID" ng-model="loginId" >
<input type="password" placeholder="Password" ng-model="password" >
<input type="button" class="btn btn-theme" ng-click="loginCall()" value="Login">
<input type="button" class="btn btn-theme" ng-click="loginCall()" value="Register Here">
</form>
</div>
My controller and factory service (authService.js):
var app = angular.module('loginFormApp', []);
app.controller('loginFormCtrl', function ($scope, AuthService) {
$scope.loginCall = function () {
var token= AuthService.authentication($scope.loginId, $scope.password);
alert(token);
};
});
app.factory('AuthService', function ($http) {
return {
authentication: function (UserName, Password) {
$http.post("http://103.19.89.152:8080/ccp-services/authenticate", {
'userName': UserName,
'password': Password
})
.then(function (response) {
window.location.href = "http://192.168.1.144:2000/angular/dashboard.html";
var getToken = response.data.httpHeaders.h5cAuthToken;
// alert(token);
},
// Error Handling
function (response) {
console.log(response.datas);
});
}
}
});