php - error page when I add token in localStorage and Interceptors -
i add token in localstorage , interceptors, cannot access page or contents. url of page contains /api
at beginning .
code security.yml:
security: encoders: fos\userbundle\model\userinterface: bcrypt role_hierarchy: role_admin: role_user role_super_admin: role_admin providers: fos_userbundle: id: fos_user.user_provider.username firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false login: pattern: ^/api/login stateless: true anonymous: true form_login: check_path: /api/login_check success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure require_previous_session: false api: pattern: ^/api stateless: true lexik_jwt: authorization_header: enabled: true prefix: bearer query_parameter: enabled: true name: bearer throw_exceptions: false create_entry_point: true main: pattern: ^/ provider: fos_userbundle stateless: true form_login: check_path: /login_check username_parameter: _username password_parameter: _password success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure require_previous_session: false logout: true anonymous: true access_control: - { path: ^/api/login, roles: is_authenticated_anonymously } - { path: ^/api/register, roles: is_authenticated_anonymously } - { path: ^/api, role: is_authenticated_fully }
code app.js:
var app = angular.module("myapp", ["ngroute","ngstorage"]); app.config(['$routeprovider','$httpprovider', function($routeprovider,$httpprovider) { $routeprovider .when("/login", { templateurl: routing.generate('login', {template:"default/login.html.twig"}), controller: 'loginctrl', }) .when("/register", { templateurl: routing.generate('register', {template:"default/registere.html.twig"}), controller: 'httpgetctrl', }); $httpprovider.interceptors.push('authinterceptor'); }]); app.controller("httpgetctrl", function ($scope, $http) { $scope.senddata = function () { // use $.param jquery function serialize data json var data = $.param({ username: $scope.username, email: $scope.email, pass: $scope.password, confir: $scope.confirmationpass }); var config = { headers : { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8;' } } $http.post(routing.generate('register'), data, config) .then(function (response) { console.log("success"); }) .catch(function() { console.log("error"); }) }; }); app.controller("loginctrl", function ($scope, $http,$localstorage) { $scope.login = function () { // use $.param jquery function serialize data json var data = $.param({ username: $scope.user, password: $scope.pass }); var config = { headers : { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8;' } } $http.post(routing.generate('login'), data, config) .then(function (response) { console.log("test"); console.log(response.data.length); $localstorage.token = response.data['token'] ; console.log($localstorage.token); }) .catch(function() { console.log("error"); }) }; }); app.factory('authinterceptor', function($rootscope, $q, $window) { return { request: function (config) { config.headers = config.headers || {}; if ($window.localstorage.token) { config.headers.authorization = 'bearer ' + $window.localstorage.token; console.log('done'); } return config; }, response: function (response) { if (response.status === 401) { // if 401 unauthenticated console.log("error 401"); } return response || $q.when(response); } }; // call factory ... })
code action in controller:
<?php namespace medbundle\controller; use sensio\bundle\frameworkextrabundle\configuration\route; use symfony\component\httpfoundation\request; use symfony\bundle\frameworkbundle\controller\controller; use fos\restbundle\controller\annotations\routeresource; use symfony\component\httpfoundation\jsonresponse; use fos\restbundle\view\viewhandler; use fos\restbundle\view\view; use medbundle\entity\med; use symfony\component\security\core\user\userinterface; use symfony\component\httpfoundation\response; use appbundle\eventlistener\jwtcreatedlistener; class apicontroller extends controller { public function getaction() { $em = $this->getdoctrine()->getmanager(); $test = $em->getrepository('medbundle:med')->findall(); $viewhandler = $this->get('fos_rest.view_handler'); // création d'une vue fosrestbundle $view = view::create($test); $view->setformat('json'); // gestion de la réponse return $viewhandler->handle($view); //return array('test'=>$test); } public function loginaction() { $em = $this->getdoctrine()->getmanager(); $request = $this->getrequest(); $test = null; $token = null; $res = 'null'; if ($request->getmethod() == 'post') { $username = $request->request->get('username'); $password = $request->request->get('password'); $test = $em->getrepository('medbundle:med')->findoneby(array('username' => $username)); if (!($test)) { $res = "error"; } else{ $salt= $test->getsalt(); $pass = crypt($password,$salt); if ( $pass !== $test->getpassword() ) { $res='error password'; } else { $res='success'; $token = $this->get('lexik_jwt_authentication.jwt_manager')->create($test); $test->settoken($token); $em->persist($test); $em->flush(); } } //return new response($res); $test = []; $test['token'] = $token; $test['res'] = $res; return new jsonresponse($test); } return $this->render('default/login.html.twig',array('test' => json_encode($test), 'token' => $token,'res' => json_encode($res))); } public function logoutaction() { } }
now when add url: https://localhost/web/app_dev.php/api/
, cannot access page. ... /api/ :this prefix getaction in controller apicontroller
help me, please, resolve bug. thanks
Comments
Post a Comment