Symfony 2 - FOSUserBundle - how to integrate into the API

I am working on a new symfony 2 project. I am studying this structure at the same time. For user management, I use the FOSUserBundle package. My project works very well, I can log in, register, log out and all other available commands.

The thing is, I want to make a smartphone application that will use the API of my Symfony application. In the application, the user will have to log in or register. Can I also use the FOSUserBundle methods for the API?

I studied another package that is very nice for creating an API, this is FOSRestBundle. If there is no solution, do you think that I will have to create my own user method, for example:

    /api/login
    /api/registrer 

Then, inside this method, do I redirect the FOSUserBundle methods? I just wonder what is the best and cleanest way to log in and sign up for FOSUserBundle from a smartphone, so using the API

thanks

+4
source share
3 answers

I used the FOSRestBundle.

This kit is very powerful and easy to implement. The documentation is pretty complete.

Github link for FOSRestBundle here

Hope this helps

+2
source

I have this problem too. I found a better solution, this is

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");
}
}
+5
source

You need to check out WSSE and how to integrate it with symfony. Also check this message. And there is a bundle , which implement authentication WSSE. WSSE is one of the best solutions for your application.

+1
source

All Articles