Skip to content
This repository has been archived by the owner on May 8, 2021. It is now read-only.

Add login with Google #35

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

namespace Adrenth\CoffeeManager;

use Adrenth\CoffeeManager\Models\Participant;
use Adrenth\CoffeeManager\ServiceProviders\CoffeeManager;
use Backend\Helpers\Backend;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Socialite\SocialiteServiceProvider;
use System\Classes\PluginBase;
use Adrenth\CoffeeManager\Components;
use Adrenth\CoffeeManager\Console;
Expand Down Expand Up @@ -40,6 +40,7 @@ public function pluginDetails(): array
public function register(): void
{
$this->app->register(CoffeeManager::class);
$this->app->register(SocialiteServiceProvider::class);

$this->registerConsoleCommand('adrenth.coffeemanager.finish-rounds', Console\FinishRounds::class);
$this->registerConsoleCommand('adrenth.coffeemanager.serve-rounds', Console\ServeRounds::class);
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,24 @@ COFFEE_MANAGER_PUSHER_APP_ID = ""
COFFEE_MANAGER_PUSHER_CLUSTER = "eu"
```

Session time
### Socialite Configuration

Add the Google service to `services.php`:

```
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT'),
],
```

Add these environment variables to the `.env` file of your project:

```
GOOGLE_CLIENT_ID=xxxxxxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT=https://xxxxx.xxx/google/callback
```

TODO: Configuring Google API
142 changes: 0 additions & 142 deletions components/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace Adrenth\CoffeeManager\Components;

use Adrenth\CoffeeManager\Models\Group;
use Adrenth\CoffeeManager\Models\Participant;
use Cms\Classes\ComponentBase;
use Cms\Classes\Page;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Session\Store;
use October\Rain\Database\Collection;
use October\Rain\Flash\FlashBag;

/**
* Class Join
Expand All @@ -21,21 +13,6 @@
*/
class Join extends ComponentBase
{
/**
* @var Collection
*/
public $groups;

/**
* @var Collection
*/
public $participants;

/**
* @var FlashBag
*/
private $flashBag;

/**
* {@inheritdoc}
*/
Expand All @@ -46,123 +23,4 @@ public function componentDetails(): array
'description' => '',
];
}

/**
* {@inheritdoc}
*/
public function defineProperties(): array
{
return [
'clientPage' => [
'label' => 'Coffee Manager Client Page',
],
];
}

/**
* {@inheritdoc}
*/
public function init()
{
$this->flashBag = resolve('flash');
}

/** @noinspection PhpMissingParentCallCommonInspection */

/**
* This method is used the first time the component is rendered into the page.
*
* {@inheritdoc}
*/
public function onRun(): void
{
$this->prepareVars();
}

/**
* @return mixed
*/
public function onJoin()
{
/** @var Request $request */
$request = resolve(Request::class);

/** @var Store $session */
$session = resolve(Store::class);

$groupId = (int) $request->get('participantGroupId');

try {
/** @var Participant $participant */
$participant = Participant::query()
->findOrFail((int) $request->get('participantId'));

if ($participant->group->getKey() !== $groupId) {
$participant->update([
'group_id' => $groupId
]);
}

$session->put('coffeemanager.participantId', $participant->getKey());

return redirect()->to(Page::url($this->property('clientPage')));
} catch (ModelNotFoundException $e) {
$this->flashBag->warning('Geef aan wie je bent en selecteer jouw koffiegroep.');
}
}

/**
* @return array
* @throws ModelNotFoundException
*/
public function onChangeParticipant(): array
{
/** @var Request $request */
$request = resolve(Request::class);

/** @var Participant $participant */
$participant = Participant::query()->findOrFail(
(int) $request->get('participantId')
);

return [
'#participantGroupWrapper' => $this->renderPartial($this->alias . '::_group', [
'selectedGroupId' => $participant ? $participant->group->getKey() : null,
'groups' => $this->getGroups(),
])
];
}

/**
* Prepare variables for use in AJAX handlers.
*
* @return void
*/
protected function prepareVars(): void
{
$this->groups = $this->getGroups();
$this->participants = $this->getParticipants();
}

/**
* @return Collection
*/
private function getGroups(): Collection
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return Group::query()
->orderBy('name')
->get();
}

/**
* @return Collection
*/
private function getParticipants(): Collection
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return Participant::query()
->orderBy('name')
->get();
}
}
38 changes: 6 additions & 32 deletions components/join/default.htm
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
{% spaceless %}
<form method="post"
data-request="{{ __SELF__ ~ '::onJoin' }}"
data-request-flash>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Koffie Manager</h4>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label" for="participantId">
Jij bent:
</label>
<select class="form-control"
id="participantId"
name="participantId"
data-request="{{ __SELF__ ~ '::onChangeParticipant' }}"
data-attach-loading>
<option value="">-- Selecteer deelnemer --</option>
{% for participant in __SELF__.participants %}
<option value="{{ participant.id }}">{{ participant.name }}</option>
{% endfor %}
</select>
</div>
<div id="participantGroupWrapper">
{% partial __SELF__ ~ '::_group' groups=__SELF__.groups %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Koffie Manager</h4>
</div>
<div class="form-group">
<button class="btn btn-block btn-primary btn-lg" type="submit" data-attach-loading>
Doe mee!
</button>
<div class="panel-body">
<a href="{{ url('/login/google') }}" class="btn btn-primary">Login met Google</a>
</div>
</form>
</div>
{% endspaceless %}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"require": {
"php": ">=7.1",
"laravel/socialite": "^3.0",
"php-ai/php-ml": "^0.7",
"pusher/pusher-php-server": "^3.3"
}
Expand Down
33 changes: 33 additions & 0 deletions routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use Adrenth\CoffeeManager\Models\Participant;
use Illuminate\Routing\Router;
use Illuminate\Session\Store;
use Laravel\Socialite\Contracts\Factory;

$router = resolve(Router::class);

$router->group(['middleware' => ['web']], static function () use ($router) {
// TODO: Move to controller
$router->get('/login/google', static function (Factory $socialiteFactory) {
return $socialiteFactory->driver('google')->redirect();
});

// TODO: Move to controller
$router->get('/google/callback', static function (Factory $socialiteFactory, Store $store) {
try {
$user = $socialiteFactory->driver('google')->user();
} catch (Throwable $e) {
return redirect()->to('/');
}

// TODO: Add email to participant table (migration)
$existingUser = Participant::query()->where('email', $user->getEmail())->firstOrFail();

$store->put('coffeemanager.participantId', $existingUser->getKey());

return redirect()->to('/');
});
});