Symfony 2 FOSUserBundle(具有其余的登录名和注册名) [英] Symfony 2 FOSUserBundle with rest login and registration

查看:76
本文介绍了Symfony 2 FOSUserBundle(具有其余的登录名和注册名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于stackoveflow的问题和文章,但是找不到合适的答案.

I have gone through lots of stackoveflow question and articles, but can't find a suitable answer.

我正在使用fosuserbundle,hwiouthbundle和lexikjwt捆绑包.

I'm using fosuserbundle, hwiouthbundle and lexikjwt bundle.

我正在开发基于symfony的api,它将由android应用程序和angular应用程序使用.

I'm developing an api based on symfony which will be consumed by an android app and angular app.

现在,我需要使用fosuserbundle的注册和登录系统以及使用hwiouthbundle的facebook登录和使用lexikjwt软件包的api保护.

Now I need the register and login system with fosuserbundle facebook login with hwiouthbundle and api protection with lexikjwt bundle.

我已经实现了fosuserbundle和hwiouthbundke,并且即使没有编写用户控制器也都可以工作.但是我需要休息而不是形式.但是我不能输入:在路由器中休息.

I have implemented fosuserbundle and hwiouthbundke and both working without even writing user controller. But I need this with rest not with form. But I can't out type : rest in router.

现在我该如何登录,休息时向fosuserbundle注册用户?我不想使用fosouth服务器.只需要使用api进行注册和登录就可以从网络上休息了.

Now how can I login, register user with fosuserbundle with rest? I don't want to use fosouth server. Just need registration and login with api not rest from web.

推荐答案

因此,如果要使用FOSUserBundle手动注册用户,请创建一个控制器并添加一个注册方法:

So, if you want register user manually using FOSUserBundle, create a controller and add a register method :

// Acme/AppBundle/Controller/SecurityController

public function registerAction(Request $request)
{
    $userManager = $this->get('fos_user.user_manager');
    $entityManager = $this->get('doctrine')->getManager();
    $data = $request->request->all();

    // Do a check for existing user with userManager->findByUsername

    $user = $userManager->createUser();
    $user->setUsername($data['username']);
    // ...
    $user->setPlainPassword($data['password']);
    $user->setEnabled(true);

    $userManager->updateUser($user);

    return $this->generateToken($user, 201);
}

然后,generateToken方法

And, the generateToken method

protected function generateToken($user, $statusCode = 200)
{
    // Generate the token
    $token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user)

    $response = array(
        'token' => $token,
        'user'  => $user // Assuming $user is serialized, else you can call getters manually
    );

    return new JsonResponse($response, $statusCode); // Return a 201 Created with the JWT.
}

路线

security_register:
    path: /api/register
    defaults: { _controller: AcmeAppBundle:Security:registerAction }
    methods: POST

配置与登录名相同的防火墙

Configure the firewall same as login

// app/config/security.yml

firewalls:
    // ...
    register:
        pattern: ^/api/register
        anonymous: true
        stateless: true
    // ...

access_control:
    // ...
    - { path: ^/api/register, role: IS_AUTHENTICATED_ANONYMOUSLY }

要登录,请使用FOSUser登录防火墙的check_path.

For login, juste use the check_path of your FOSUser login firewall.

有关令牌生成的更多信息,请参见 JWTManager . 希望对您有帮助.

For more information about the token generation, see JWTManager. Hope this help you.

编辑

如果您想要LexikJWTAuthenticationBundle + FOSUserBundle + FOSRestBundle实现的完整示例,请参阅我的 symfony-rest-api

If you want a full example of LexikJWTAuthenticationBundle + FOSUserBundle + FOSRestBundle implementation see my symfony-rest-api

这篇关于Symfony 2 FOSUserBundle(具有其余的登录名和注册名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆