Вопрос

I am trying to setup Symfony2.1 with HWIOAuthBundle to handle google oauth authentication.

I have followed manual instructions in bundle description, but still something is missing. When I visit an action which requires authenticated user (or just type url /connect/google) I am redirected to google authentication page - this part is working. But after redirection back (after logging to Google and agree for sharing my data) new user is not created in DB and I am redirected into connect/fail action. Google authentication should be only one way to authenticate user. Any idea what could be wrong?

This is my routing:

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /connect

google_login:
    pattern: /login/check-google

security.yml

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    secured_area:
        pattern:    ^/
        form_login:
            provider: fos_userbundle
            login_path: /connect/google
            check_path: /login/login_check
        logout: true
        anonymous: true
        oauth:
            resource_owners:
                google: "/login/check-google"
            login_path:        /connect/google
            failure_path:      /connect-fail
            # FOSUB integration
            oauth_user_provider:
                service: hwi_oauth.user.provider.fosub_bridge

and config

hwi_oauth:
    firewall_name: secured_area
    resource_owners:
            google:
                type:                google
                client_id:           ....
                client_secret:       ......
                scope:               "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
    fosub:
        username_iterations: 5
        properties:
            google: googleId

google redirectURL

http://xxxx/login/check-google

my User entity:

class User extends BaseUser
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var integer $id
     *
     * @ORM\Column(name="google_id", type="integer")
     */ 
    protected $googleId;

//edit- update

I just found in Symfony logs that I had one request from google redirect: GET http://xxxxx.dk/login/check-google?code=4%2F8tiDAhI45D2g7BvrdtwoF7wJ.Eu7i2fmkYOl05ti8ZT3YDrXXXXXfakeXXXE4hcwI just before redirect to /connect-file
Unfortunately it looks that request only tried to retrieve user from my db based on google_id SELECT * FROM app_user t0 WHERE t0.googleId = ? LIMIT 1 : ['123456789'] but that user does not exist there.

Should I create user befor I tryied to authenticate him/her?

Это было полезно?

Решение

Sadly, HWIOAuthBundle doesn't handle automatic registration by default. If you want to login now you'll have to create the user manually.

Другие советы

As beliveyourdream says... HWIOAuth does NOT provide you automatic user registration. You need to create your own user provider and use that. I suggest you go with FOSUserBundle as it does most of the job and it is pretty well integrated in HWIOAuth. For how to set up (as it is a bit of work to do on connecting them), please use this Gist. It does setup your application with both user registration using data from PROVIDERS (gacebook, google, github, etc.) and account connection (an already registered user may connect his/her account with accouns from PROVIDERS). Also, upon creation/connection, the users will be able to connect to your site with the PROVIDERS' credentials (duh... it's OAuth, after all).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top