<commit_before>CakePHP SocialAuth Plugin
=========================

[![Total Downloads](https://img.shields.io/packagist/dt/ADmad/cakephp-social-auth.svg?style=flat-square)](https://packagist.org/packages/admad/cakephp-social-auth)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)

A CakePHP plugin which allows you authenticate using social providers like
Facebook/Google/Twitter etc. using [SocialConnect/auth](https://github.com/SocialConnect/auth)
social sign on library.

Requirements
------------

* CakePHP 3.4+.

Installation
------------

Run:

```
composer require admad/cakephp-social-auth
```

Setup
-----

Load the plugin by running following command in terminal:

```
bin/cake plugin load ADmad/SocialAuth -b -r
```

or by manually adding following line to your app's `config/bootstrap.php`:

```php
Plugin::load('ADmad/SocialAuth', ['bootstrap' => true, 'routes' => true]);
```

Database
--------

This plugin requires a migration to generate a `social_profiles` table, and it
can be generated via the official Migrations plugin as follows:

```shell
bin/cake migrations migrate -p ADmad/SocialAuth
```

Usage
-----

The plugin provides a `\ADmad\SocialAuth\Middleware\SocialAuthMiddleware` which handles authentication process.
You can configure the middleware in your `Application::middleware()` method as shown:

```php
$middleware->add(new \ADmad\SocialAuth\Middleware\SocialAuthMiddleware([
    // Request method type use to initiate authentication.
    'requestMethod' => 'POST',
    // URL string or array to redirect to after authentication.
    'loginRedirect' => '/',
    // Boolean indicating user identity should be returned as entity.
    'userEntity' => false,
    // User model.
    'userModel' => 'Users',
    // Finder type.
    'finder' => 'all',
    // Fields.
    'fields' => [
        'password' => 'password'
    ],
    // Session key to which to write identity record to.
    'sessionKey' => 'Auth.User',
    // The methods in user model which should be called in case of new user.
    // It should return a User entity.
    'getUserCallback' => 'getUser',
    // SocialConnect Auth service's providers config. https://github.com/SocialConnect/auth/blob/master/README.md
    'serviceConfig' => [
        'provider' => [
            'facebook' => [
                'applicationId' => '<application id>',
                'applicationSecret' => '<application secret>',
                'scope' => [
                    'email'
                ]
            ],
        ]
    ],
]));
```

On your login page you can create links to initiate authentication using required
providers.

```php
echo $this->Form->postLink(
    'Login with Facebook',
    [
        'plugin' => 'ADmad/SocialAuth',
        'controller' => 'Auth',
        'action' => 'login',
        'provider' => 'facebook'
    ]
);
```

We use a POST link here instead of a normal link to prevent search bots and other
crawlers from following the link. (Adding "nofollow" attribute to link doesn't
suffice as it's often ignored by bots/crawlers.) If you prefer using GET you can
still do so by configuring the middleware with `'requestMethod' => 'GET'`.

Once a user is authenticated through the provider the authenticator gets the user
profile from the identity provider and using that tries to find the corresponding
user record using the user model. If no user is found it calls the `getUser` method
of your user model. The method recieves social profile model entity as argument
and return an entity for the new user. E.g.

```php
// UsersTable.php

public function getUser(EntityInterface $profile) {
    // Make sure here that all the required fields are actually present
    if (empty($profile->email)) {
        throw new \RuntimeException('Could not find email in social profile.');
    }

    $user = $this->newEntity(['email' => $profile->email]);
    $user = $this->save($user);

    if (!$user) {
        throw new \RuntimeException('Unable to save new user');
    }

    return $user;
}
```

Copyright
---------
Copyright 2017 ADmad

License
-------
[See LICENSE](LICENSE.txt)<commit_msg>Update getUserCallback example.<commit_after>CakePHP SocialAuth Plugin
=========================

[![Total Downloads](https://img.shields.io/packagist/dt/ADmad/cakephp-social-auth.svg?style=flat-square)](https://packagist.org/packages/admad/cakephp-social-auth)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)

A CakePHP plugin which allows you authenticate using social providers like
Facebook/Google/Twitter etc. using [SocialConnect/auth](https://github.com/SocialConnect/auth)
social sign on library.

Requirements
------------

* CakePHP 3.4+.

Installation
------------

Run:

```
composer require admad/cakephp-social-auth
```

Setup
-----

Load the plugin by running following command in terminal:

```
bin/cake plugin load ADmad/SocialAuth -b -r
```

or by manually adding following line to your app's `config/bootstrap.php`:

```php
Plugin::load('ADmad/SocialAuth', ['bootstrap' => true, 'routes' => true]);
```

Database
--------

This plugin requires a migration to generate a `social_profiles` table, and it
can be generated via the official Migrations plugin as follows:

```shell
bin/cake migrations migrate -p ADmad/SocialAuth
```

Usage
-----

The plugin provides a `\ADmad\SocialAuth\Middleware\SocialAuthMiddleware` which handles authentication process.
You can configure the middleware in your `Application::middleware()` method as shown:

```php
$middleware->add(new \ADmad\SocialAuth\Middleware\SocialAuthMiddleware([
    // Request method type use to initiate authentication.
    'requestMethod' => 'POST',
    // URL string or array to redirect to after authentication.
    'loginRedirect' => '/',
    // Boolean indicating user identity should be returned as entity.
    'userEntity' => false,
    // User model.
    'userModel' => 'Users',
    // Finder type.
    'finder' => 'all',
    // Fields.
    'fields' => [
        'password' => 'password'
    ],
    // Session key to which to write identity record to.
    'sessionKey' => 'Auth.User',
    // The methods in user model which should be called in case of new user.
    // It should return a User entity.
    'getUserCallback' => 'getUser',
    // SocialConnect Auth service's providers config. https://github.com/SocialConnect/auth/blob/master/README.md
    'serviceConfig' => [
        'provider' => [
            'facebook' => [
                'applicationId' => '<application id>',
                'applicationSecret' => '<application secret>',
                'scope' => [
                    'email'
                ]
            ],
        ]
    ],
]));
```

On your login page you can create links to initiate authentication using required
providers.

```php
echo $this->Form->postLink(
    'Login with Facebook',
    [
        'plugin' => 'ADmad/SocialAuth',
        'controller' => 'Auth',
        'action' => 'login',
        'provider' => 'facebook'
    ]
);
```

We use a POST link here instead of a normal link to prevent search bots and other
crawlers from following the link. (Adding "nofollow" attribute to link doesn't
suffice as it's often ignored by bots/crawlers.) If you prefer using GET you can
still do so by configuring the middleware with `'requestMethod' => 'GET'`.

Once a user is authenticated through the provider the authenticator gets the user
profile from the identity provider and using that tries to find the corresponding
user record using the user model. If no user is found it calls the `getUser` method
of your user model. The method recieves social profile model entity as argument
and return an entity for the new user. E.g.

```php
// UsersTable.php

public function getUser(EntityInterface $profile) {
    // Make sure here that all the required fields are actually present
    if (empty($profile->email)) {
        throw new \RuntimeException('Could not find email in social profile.');
    }

    // Check if user with same email exists. This avoids creating multiple
    // user accounts for different social identities of same user. You should
    // probably skip this check if your system doesn't enforce unique email
    // per user.
    $user = $this->find()
        ->where(['email' => $profile->email])
        ->firt();

    if ($user) {
        return $user;
    }

    // Create new user account
    $user = $this->newEntity(['email' => $profile->email]);
    $user = $this->save($user);

    if (!$user) {
        throw new \RuntimeException('Unable to save new user');
    }

    return $user;
}
```

Copyright
---------
Copyright 2017 ADmad

License
-------
[See LICENSE](LICENSE.txt)
{'fraction_non_alphanumeric': 0.13378502526412495, 'fraction_numerical': 0.0013780431786862655, 'mean_word_length': 3.0506976744186045, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 72, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4451634', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2366, 'n_words': 859}