Skip to content

Commit

Permalink
Merge branch '#146-adding-login-via-social-media' of https://github.c…
Browse files Browse the repository at this point in the history
…om/blumilksoftware/escooters into #146-adding-login-via-social-media
  • Loading branch information
Lee0z committed Dec 9, 2023
2 parents fdd1947 + bd5cc02 commit 3428cae
Show file tree
Hide file tree
Showing 39 changed files with 1,292 additions and 748 deletions.
10 changes: 9 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ TRAEFIK_ENABLED=true

GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_REDIRECT_URI=http://escooters.blumilk.localhost/login/github/callback
GITHUB_REDIRECT_URI=http://escooters.blumilk.localhost/login/github/redirect

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=http://escooters.blumilk.localhost/login/google/redirect

FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_REDIRECT_URI=http://escooters.blumilk.localhost/login/facebook/redirect
25 changes: 15 additions & 10 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -61,16 +62,20 @@ public function redirectToProvider(string $provider): Response

public function handleProviderRedirect(string $provider): RedirectResponse
{
$user = Socialite::driver($provider)->user();

$user = User::firstOrCreate([
"email" => $user->getEmail(),
], [
"name" => $user->getName(),
"password" => Hash::make($user->getId()),
]);

Auth::login($user);
try {
$user = Socialite::driver($provider)->user();

$user = User::firstOrCreate([
"email" => $user->getEmail(),
], [
"name" => $user->getName(),
"password" => Hash::make(Str::password(8)),
]);

Auth::login($user);
} catch (\Exception $e) {
return redirect()->route("home");
}

return redirect()->route("home");
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/CityOpinionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CityOpinionRequest extends FormRequest
public function rules(): array
{
return [
"rating" => ["required", "numeric", "max:5"],
"rating" => ["required", "numeric", "min:1", "max:5"],
"content" => ["required", "string", "max:250"],
"city_id" => ["required", "numeric", "exists:cities,id"],
];
Expand Down
1 change: 0 additions & 1 deletion app/Importers/BirdDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ protected function load(string $cityName, string $countryName, string $lat = "",
return strval($city->id);
}
$this->countryNotFound($cityName, $countryName);
$this->createImportInfoDetails("420", self::getProviderName());

return "";
}
Expand Down
26 changes: 13 additions & 13 deletions app/Importers/DataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,19 @@ public function translate(string $word, string $language): string

protected function countryNotFound(string $cityName, string $countryName): void
{
CityWithoutAssignedCountry::query()
->withTrashed()
->updateOrCreate(
[
"city_name" => $cityName,
"country_name" => $countryName,
],
[
"city_name" => $cityName,
"country_name" => $countryName,
],
);
$cityAttributes = [
"city_name" => $cityName,
"country_name" => $countryName,
];

$city = CityWithoutAssignedCountry::query()->withTrashed()->updateOrCreate(
$cityAttributes,
$cityAttributes,
);

if ($city->wasRecentlyCreated) {
$this->createImportInfoDetails("420", self::getProviderName());
}
}

protected function createProvider(int $cityId, string $providerName): void
Expand Down Expand Up @@ -149,7 +150,6 @@ protected function load(string $cityName, string $countryName, string $lat = "",
return strval($city->id);
}
$this->countryNotFound($cityName, $countryName);
$this->createImportInfoDetails("420", self::getProviderName());

return "";
}
Expand Down
75 changes: 75 additions & 0 deletions app/Importers/HopDataImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace App\Importers;

use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\DomCrawler\Crawler;

class HopDataImporter extends DataImporter
{
protected Crawler $sections;

public function extract(): static
{
try {
$response = $this->client->get("https://hop.bike/en/location-list.html");
$html = $response->getBody()->getContents();
} catch (GuzzleException) {
$this->createImportInfoDetails("400", self::getProviderName());
$this->stopExecution = true;
}

$crawler = new Crawler($html);

$this->sections = $crawler->filter('section[class="location-list"]');

if (count($this->sections) === 0) {
$this->createImportInfoDetails("204", self::getProviderName());

$this->stopExecution = true;
}

return $this;
}

public function transform(): void
{
$existingCityProviders = [];

if ($this->stopExecution) {
return;
}

$locations = [];

$countryList = $this->sections->filter('div[class="location-list-body-main-title"]');
$citySectionList = $this->sections->filter('div[class="location-list-body-main-list"]');

foreach ($countryList as $index => $country) {
$citySection = $citySectionList->eq($index);
$cities = $citySection->filter('li[class="location-list-body-main-list-title text-body-xl"]');

foreach ($cities as $city) {
$locations[] = [
"country" => $country->textContent,
"city" => $city->textContent,
];
}
}

foreach ($locations as $location) {
$location["country"] = preg_replace('/\s+/', "", $location["country"]);
$location["city"] = preg_replace('/\s+/', "", $location["city"]);

$provider = $this->load($location["city"], $location["country"]);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}

$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
74 changes: 74 additions & 0 deletions app/Importers/HoppDataImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace App\Importers;

use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\DomCrawler\Crawler;

class HoppDataImporter extends DataImporter
{
protected Crawler $sections;

public function extract(): static
{
try {
$response = $this->client->get("https://hopp.bike/locations");
$html = $response->getBody()->getContents();
} catch (GuzzleException) {
$this->createImportInfoDetails("400", self::getProviderName());
$this->stopExecution = true;
}

$crawler = new Crawler($html);

$this->sections = $crawler->filter('section[class="sc-3ad02d05-0 eZsxrq"]');

if (count($this->sections) === 0) {
$this->createImportInfoDetails("204", self::getProviderName());

$this->stopExecution = true;
}

return $this;
}

public function transform(): void
{
$existingCityProviders = [];

if ($this->stopExecution) {
return;
}

$locations = [];

$countryList = $this->sections->filter('h1[class="sc-188713c8-0 kHXWNs"]');
$citySectionList = $this->sections->filter('div[class="sc-3ad02d05-0 gqLRPj"]');

foreach ($countryList as $index => $country) {
$citySection = $citySectionList->eq($index);
$cities = $citySection->filter('p[class="sc-188713c8-0 bGGtqk"]');

foreach ($cities as $city) {
$locations[] = [
"country" => $country->textContent,
"city" => $city->textContent,
];
}
}

foreach ($locations as $location) {
$location["country"] = preg_replace('/\s+/', "", $location["country"]);
$location["city"] = preg_replace('/\s+/', "", $location["city"]);

$provider = $this->load($location["city"], $location["country"]);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
1 change: 0 additions & 1 deletion app/Importers/LimeDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ protected function load(string $cityName, string $countryName, string $lat = "",
return strval($city->id);
}
$this->countryNotFound($cityName, $countryName);
$this->createImportInfoDetails("420", self::getProviderName());

return "";
}
Expand Down
77 changes: 77 additions & 0 deletions app/Importers/SixtDataImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace App\Importers;

use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\DomCrawler\Crawler;

class SixtDataImporter extends DataImporter
{
protected Crawler $sections;

public function extract(): static
{
try {
$headers = [
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
];
$response = $this->client->get("https://www.sixt.com/share/e-scooter/#/", ["headers" => $headers]);
$html = $response->getBody()->getContents();
} catch (GuzzleException) {
$this->createImportInfoDetails("400", self::getProviderName());

$this->stopExecution = true;

return $this;
}

$crawler = new Crawler($html);

$this->sections = $crawler->filter("div.item div.middle div.content ul");

if ($this->sections->count() === 0) {
$this->createImportInfoDetails("204", self::getProviderName());
$this->stopExecution = true;
}

return $this;
}

public function transform(): void
{
if ($this->stopExecution) {
return;
}

$existingCityProviders = [];
$countryName = "";

foreach ($this->sections as $section) {
$node = $section->parentNode->parentNode->parentNode;

foreach ($node->childNodes as $country) {
if ($country instanceof \DOMElement) {
$class = $country->getAttribute("class");

if ($class === "title") {
$countryName = trim(preg_replace("/[^a-zA-Z ]/", "", $country->nodeValue));
}
}
}

foreach ($section->childNodes as $city) {
if ($city->nodeName === "li") {
$cityName = trim(preg_replace('/\s+/', "", $city->nodeValue));
$provider = $this->load($cityName, $countryName);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}
}
$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
1 change: 0 additions & 1 deletion app/Importers/SpinDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ protected function load(string $cityName, string $countryName, string $lat = "",
return strval($city->id);
}
$this->countryNotFound($cityName, $countryName);
$this->createImportInfoDetails("420", self::getProviderName());

return "";
}
Expand Down
15 changes: 15 additions & 0 deletions app/Jobs/HopDataImporterJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Importers\HopDataImporter;

class HopDataImporterJob extends DataImporterJob
{
public function handle(HopDataImporter $importer): void
{
$importer->setImportInfo($this->importInfoId)->extract()->transform();
}
}
15 changes: 15 additions & 0 deletions app/Jobs/HoppDataImporterJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Importers\HoppDataImporter;

class HoppDataImporterJob extends DataImporterJob
{
public function handle(HoppDataImporter $importer): void
{
$importer->setImportInfo($this->importInfoId)->extract()->transform();
}
}
Loading

0 comments on commit 3428cae

Please sign in to comment.