0

So, I have symfony3 + FOSRestBundle + FOSUserBundle + AngularJS. I created AuthenticationHandler for example: Symfony2 AJAX Login

And other controller, which gets POST, then validation data by form and finally return success or errors fields. Of course I created in AngularJS controller to servicing this form.

SF Controller:

 public function postPostmanAction(Request $request) {
    $postman = $this->container->get("postman_form.handler");
    return $postman->post($request->request->all());
  }

SF Handler:

class PostmanFormHandler {
// ....
public function post(array $parameters) {
    $postman = new Postman;
    return $this->processForm($postman, $parameters);
  }

  protected function processForm($postman, $parameters) {
    $form = $this->formFactory->create('AppBundle\Form\PostmanType', $postman);
    $form->submit($parameters);
    $em = $this->repository;

    if ($form->isValid()) {
      $em->getRepository("AppBundle:Postman")->save($postman);
      return $postman;
    }
    return $form;
  }
}

Next to I created form to login user and controller in AngularJS and I added access_controll to form(postPostmanAction).

Now when I tried add postman I see error 403, so It's ok.

When I login, JSON returns:

{"success":true}

and refresh to tab, in the profiller symfony I see: Authenticated: Yes and username.

Now I can add postman after login.

I have problem, AngularJS don't know when user is authenticated. User should see view to which have access after login.

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/postman/new', {
      templateUrl: '/MobilePost/web/assets/partials/postman/postman-form.html',
      controller: 'CreatePostmanForm'
    }).when('/login',{
      templateUrl: '/MobilePost/web/assets/partials/user/login-form.html',
      controller: 'LoginUserForm'
    }).otherwise({
      templateUrl: '/MobilePost/web/assets/partials/index.html',
    });
  }
]);

How I can check authorization and permission in Angular?

Community
  • 1
  • 1
viko
  • 491
  • 6
  • 23

1 Answers1

0

You can put a variable 'isAuthenticated' in your JSONResponse who's : TRUE when authenticated, FALSE when not authenticated.

And you play with conditions in your angularJS code.

Also, i advice you to use https://github.com/FriendsOfSymfony/FOSJsRoutingBundle.

KitchenLee
  • 105
  • 1
  • 1
  • 6
  • I would like to do my application on single page, but I see that is problem. I think that the 2 sides will be good solution.... – viko May 15 '16 at 14:04