2016-05-14 132 views
0

所以,我有symfony3 + FOSRestBundle + FOSUserBundle + AngularJS。我創建了AuthenticationHandler例如:Symfony2 AJAX LoginAngularJS Symfony3授權

和其他控制器,它獲取POST,然後通過表單驗證數據,最後返回成功或錯誤字段。當然,我在AngularJS控制器中創建了這個表單。

SF控制器:

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

SF處理程序:

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; 
    } 
} 

下一步我創建形式登錄用戶和控制器在AngularJS和我加入access_controll以形成(postPostmanAction)。

現在,當我嘗試添加郵差,我看到錯誤403,所以沒關係。

當我登錄,JSON返回:

{"success":true} 

,並刷新選項卡,在profiller的symfony我看到:驗證:是和用戶名。

現在我可以在登錄後添加郵差。

我有問題,AngularJS不知道用戶何時通過身份驗證。用戶應該看到登錄後可以訪問的視圖。

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', 
    }); 
    } 
]); 

我如何檢查授權和Angular的權限?

回答

0

您可以在您的JSONResponse中置入一個變量'isAuthenticated',該變量的身份驗證爲TRUE,未通過身份驗證時爲FALSE。

而你在angularJS代碼中使用條件。

此外,我建議你使用https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

+0

我想在單頁上做我的應用程序,但我發現這是問題。我認爲雙方將是很好的解決方案.... – viko