Angularjs, value coming in token from backend not working in client side -
what wrong code it's not working, trying request call web service backend written in spring, value passing backend token wrapped, trying run code on client side form not passing value.
auth.js
'use strict'; angular. module('app') .factory('auth', [ '$http', '$rootscope', '$window', 'session', 'auth_events', function($http, $rootscope, $window, session, auth_events) { var authservice = {}; this.isloggedin = function isloggedin(){ return session.getuser() !== null; }; //the login function authservice.login = function(user, success, error) { $http.post('url: http://xxx.xxx.x.xx:xxxx/xxxx/authenticateuser').success(function(authdata) { //user returned data db var users = data.users; if(users[user.username]){ var logindata = users[user.username]; //insert custom login function here if(user.username == logindata.username && user.password == logindata.username){ localstorageservice.set(['userinfo'], { token: result.access_token, username: logindata.username }); //delete password no/t seen clientside delete logindata.password; //update current user session service or $rootscope.currentuser //whatever prefer session.create(logindata); //or $rootscope.currentuser = logindata; //fire event of successful login $rootscope.$broadcast(auth_events.loginsuccess); //run success function success(logindata); } else{ //or else //unsuccessful login, fire login failed event //the according functions run $rootscope.$broadcast(auth_events.loginfailed); error(); } } }); }; //check if user authenticated authservice.isauthenticated = function() { return !!session.user; }; //check if user authorized access next route //this function can used on element level //e.g. <p ng-if="isauthorized(authorizedroles)">show admins</p> authservice.isauthorized = function(authorizedroles) { if (!angular.isarray(authorizedroles)) { authorizedroles = [authorizedroles]; } return (authservice.isauthenticated() && authorizedroles.indexof(session.userrole) !== -1); }; //log out user , broadcast logoutsuccess event authservice.logout = function(){ session.destroy(); localstorageservice.removeitem("userinfo"); $rootscope.$broadcast(auth_events.logoutsuccess); } return authservice; } ]);
authinterceptor
(function () { 'use strict'; var app = angular.module('app'); var factoryid = 'authinterceptor'; app.factory(factoryid, authinterceptor); authinterceptor.$inject = ['$q', '$location', 'localstorageservice', $rootscope, $http]; function authinterceptor($q, $location, localstorageservice) { var service = { request: request, responseerror: responseerror, }; return service; function request(config) { config.headers = config.headers || {}; var authdata = localstorageservice.get('authorizationdata'); if (authdata) { config.headers.authorization = 'bearer ' + authdata.token; } return config; } function responseerror(error) { var loggedin = false; var authdata = localstorageservice.get('authorizationdata'); if (authdata) { loggedin = true; } //we want go login page if user not //logged in. if user logged in , 401 //because don't have access resource requested. if (error.status === 401 && !loggedin) { $location.path('/login').replace(); } return $q.reject(error); } } })();
Comments
Post a Comment