Symfony 2 - FOSUserBundle - how to integrate in API
Asked Answered
H

3

5

I work on a new Symfony 2 project. I'm learning this framework at the same time. For the user management, I use the bundle FOSUserBundle. My project works very well, I can login, register, logout and all other commands available.

The thing is that I want to make smartphone app which will use the API of my Symfony app. In the app, the user will have to sign in, or to sign up. Is it possible to use FOSUserBundle methods for API too?

I studied another bundle for making an API, it's FOSRestBundle. If there are not solution, do you think that I will have to create my own users method like :

    /api/login
    /api/register 

Then, inside this method, I redirect to FOSUserBundle methods? I'm just wondering what is the best, and the cleanest way to login, and register with FOSUserBundle from smartphone, so by using API

Hatchett answered 24/10, 2013 at 18:53 Comment(3)
You might take a look to Silex, there is a quick documentation that explains how to build a simple RESTFul service: silex.sensiolabs.org/doc/cookbook/json_request_body.html Also, check out this repo: github.com/emedlr/restful-appRemonaremonetize
FosRestBundle is the tool/bundle to fix your problemSlaughter
@WouterJ, FosRestBundle... So you mean that I should do the solution I put on my description ? "If there are not solution, do you think that I will have to create my own users method like .... Then, inside this method, I redirect to FOSUserBundle methods ? " Thanks for your helpHatchett
H
2

I used the FOSRestBundle.

This bundle is very powerful and simple to implement. The documentation is pretty complete.

The github link of FOSRestBundle here

Hope that it helps

Hatchett answered 26/1, 2014 at 19:25 Comment(0)
A
5

I have this problem too. I found the best solution is this

use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class YourController extends Controller{
//Other Methods..

public function loginAction(Request $request){
    try{
        $token = $this->get('security.authentication.manager')->authenticate(new UsernamePasswordToken('username', 'password', 'firewall'));
        $this->get('security.context')->setToken($token);
    }
    catch(BadCredentialsException $e){
        return new Response("Bad credentials", 403);
    }
    return new Response("success");
}
}
Alloplasm answered 22/2, 2015 at 15:7 Comment(2)
When given bad credentials, $this->get('security.authentication.manager')->authenticate returns a redirect response to login page in Symfony 2.7Ster
Perfect solution for me, I implemented API login system under FOSUserBundle with this approach. But you forgot to add this: $event = new InteractiveLoginEvent($request, $token); $this->get("event_dispatcher")->dispatch("security.interactive_login", $event); Refer here for more infoWrit
H
2

I used the FOSRestBundle.

This bundle is very powerful and simple to implement. The documentation is pretty complete.

The github link of FOSRestBundle here

Hope that it helps

Hatchett answered 26/1, 2014 at 19:25 Comment(0)
L
1

You need to check WSSE and how to integrate it to symfony. Also check this post. And there is a bundle that implementing WSSE authentication. WSSE one of the best solutions for your app.

Longsufferance answered 25/10, 2013 at 18:32 Comment(1)
I used FOSRestBundle, very powerful toolHatchett

© 2022 - 2024 — McMap. All rights reserved.