well need send username , password generate token login, problem send client (angular) response error, when send postman work fine.
public function initialize() { parent::initialize(); $this->auth->allow(['add', 'token', 'me']); } public function token() { $user = $this->auth->identify(); if (!$user) { throw new unauthorizedexception('invalid username or password'); } $this->set([ 'success' => true, 'data' => [ 'user_id' => $user['id'], 'token' => jwt::encode([ 'sub' => $user['id'], 'exp' => time() + 604800 ], security::salt()) ], '_serialize' => ['success', 'data'] ]); }
in angular how send it
$scope.dologin = function() { datalogin = { username: vm.username, password: vm.password }; console.log(datalogin); // call auth.login() function //$scope.processing = true; $scope.error = ''; auth.login(datalogin) .then(function(data) { if (data.success) $state('home'); else console.log(data); $scope.error = data.message; }); };
and
authfactory.login = function(datalogin) { return apiservice.request('post', '/api/users/token', datalogin).then(function(data) { authtoken.settoken(data.token); return data; }); };
apiservice app angular
(function(){ angular .module('appdekma') .factory('apiservice', apiservice); apiservice.$inject = ['$http']; function apiservice($http) { return { request: sendrequest }; function sendrequest(method, endpoint, data) { var api_host = 'http://localhost/dekma_backend'; var req = { method: method || 'get', url: api_host + endpoint, data: data || '', headers: { 'accept': 'application/json', 'content-type': 'application/json', 'access-control-allow-origin' : '*', 'access-control-allow-headers': 'accept, authorization, cache-control, content-type, x-requested-with, x-csrf-token', 'access-control-max-age': '3600' } }; return $http(req) .then(successcallback, errorcallback); function successcallback(response) { return response.data; } function errorcallback(error) { var message = 'something terrible happened!'; if (error.data && error.data.code) { switch (error.data.code) { case 500: break; case 401: break; case 403: break; case 404: message = 'could not find content'; break; default: break; } } return error.data; } } } })();
angular.module('app').config(['$routeprovider', '$httpprovider', function ($routeprovider, $httpprovider, urls) { $httpprovider.interceptors.push(['$q', '$location', '$localstorage', function ($q, $location, $localstorage) { return { 'request': function (config) { config.headers = config.headers || {}; if ($localstorage.token) { config.headers.authorization = 'bearer ' + $localstorage.token;// jwt token stored in localstorage config.headers.accept = 'application/json'; } return config; }, 'responseerror': function (response) { console.log(response); if (response.status === 401 || response.status === 403 || response.status === 500) { var login_url = urls.base+'users/login'; } return $q.reject(response); } }; }]); $routeprovider .when('/', { templateurl: "public/html/" + 'your_html_page.html', controller: 'your_anuglar_controller' }) .otherwise({ redirectto: '/' }); }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
Comments
Post a Comment