Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into #22-countries
Browse files Browse the repository at this point in the history
# Conflicts:
#	database/seeders/DatabaseSeeder.php
  • Loading branch information
krzysztofrewak committed Oct 4, 2020
2 parents c7c1a36 + 298190b commit b915e13
Show file tree
Hide file tree
Showing 40 changed files with 986 additions and 1,047 deletions.
2 changes: 1 addition & 1 deletion .env.behat
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

TELESCOPE_ENABLED=false
TELESCOPE_ENABLED=true
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120

TELESCOPE_ENABLED=false
TELESCOPE_ENABLED=true

FACEBOOK_ID=
FACEBOOK_SECRET=
26 changes: 26 additions & 0 deletions app/Eloquent/SocialProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Brewmap\Eloquent;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property string $userId
* @property User $user
* @property string $providerName
* @property string $providerId
* @property Carbon $createdAt
* @property Carbon $updatedAt
*/
class SocialProfile extends Model
{
protected $primaryKey = "user_id";

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
13 changes: 11 additions & 2 deletions app/Eloquent/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

/**
* @property string|null $id
* @property string $name
* @property string $email
* @property string|null $emailVerifiedAt
* @property string $password
* @property string|null $password
* @property string $rememberToken
* @property Profile $profile
* @property Collection|SocialProfile[] $socialProfiles
* @property Carbon $createdAt
* @property Carbon $updatedAt
*/
Expand All @@ -31,15 +34,21 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
use Authenticatable;
use Authorizable;
use CanResetPassword;
use HasApiTokens;
use MustVerifyEmail;
use Notifiable;

protected $casts = ["email_verified_at" => "datetime"];
protected $hidden = ["password", "remember_token"];
protected $fillable = ["email", "password"];
protected $fillable = ["email", "password", "name"];

public function profile(): HasOne
{
return $this->hasOne(Profile::class);
}

public function socialProfiles(): HasMany
{
return $this->hasMany(SocialProfile::class);
}
}
13 changes: 13 additions & 0 deletions app/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Brewmap\Exceptions;

use Exception;
use Illuminate\Http\Response;

abstract class ApiException extends Exception
{
protected $code = Response::HTTP_INTERNAL_SERVER_ERROR;
}
14 changes: 14 additions & 0 deletions app/Exceptions/Auth/SocialProviderConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Brewmap\Exceptions\Auth;

use Exception;
use Illuminate\Http\Response;

class SocialProviderConfigurationException extends Exception
{
protected $message = "Attempted social service provider is not configured properly.";
protected $code = Response::HTTP_NOT_IMPLEMENTED;
}
13 changes: 13 additions & 0 deletions app/Exceptions/Auth/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Brewmap\Exceptions\Auth;

use Brewmap\Exceptions\ApiException;
use Illuminate\Http\Response;

class UnauthorizedException extends ApiException
{
protected $code = Response::HTTP_UNAUTHORIZED;
}
14 changes: 11 additions & 3 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@

namespace Brewmap\Exceptions;

use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as LaravelHandler;
use Symfony\Component\HttpFoundation\Response;
use Throwable;

class Handler extends LaravelHandler
{
protected function unauthenticated($request, AuthenticationException $exception): Response
public function render($request, Throwable $exception): Response
{
return response()->json(["message" => $exception->getMessage()], Response::HTTP_UNAUTHORIZED);
if ($request->isJson() || $exception instanceof ApiException) {
return response()->json(
[
"message" => $exception->getMessage(),
],
$exception->getCode() ?? Response::HTTP_INTERNAL_SERVER_ERROR
);
}
return parent::render($request, $exception);
}
}
48 changes: 48 additions & 0 deletions app/Http/Controllers/API/AuthenticationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Controllers\API;

use Brewmap\Exceptions\Auth\SocialProviderConfigurationException;
use Brewmap\Exceptions\Auth\UnauthorizedException;
use Brewmap\Http\Controllers\Controller;
use Brewmap\Http\Requests\User\LoginUserRequest;
use Brewmap\Http\Requests\User\RegisterUserRequest;
use Brewmap\Services\AuthenticationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Laravel\Socialite\Facades\Socialite;

class AuthenticationController extends Controller
{
/**
* @throws UnauthorizedException
*/
public function login(LoginUserRequest $request, AuthenticationService $authenticationService): JsonResponse
{
$token = $authenticationService->login($request);
return response()->json(["token" => $token]);
}

public function register(RegisterUserRequest $request, AuthenticationService $authenticationService): JsonResponse
{
$authenticationService->register($request->validated());
return response()->json(["message" => __("auth.register_success")]);
}

public function redirectToFacebook(): RedirectResponse
{
return Socialite::driver("facebook")->redirect();
}

/**
* @throws SocialProviderConfigurationException
*/
public function handleFacebookCallback(AuthenticationService $authenticationService): JsonResponse
{
$facebookUser = Socialite::driver("facebook")->user();
$token = $authenticationService->getTokenBySocialLogin($facebookUser, "facebook");
return response()->json(["token" => $token]);
}
}
26 changes: 26 additions & 0 deletions app/Http/Requests/BaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Response;

abstract class BaseRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

/**
* @throws HttpResponseException
*/
protected function failedValidation(Validator $validator): void
{
throw new HttpResponseException(response()->json($validator->errors(), Response::HTTP_UNPROCESSABLE_ENTITY));
}
}
15 changes: 15 additions & 0 deletions app/Http/Requests/BaseRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Requests;

class BaseRules
{
protected static array $rules;

public static function rules(array $additionalRules = []): array
{
return array_merge(static::$rules, $additionalRules);
}
}
20 changes: 20 additions & 0 deletions app/Http/Requests/User/LoginUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Requests\User;

use Brewmap\Http\Requests\BaseRequest;
use Brewmap\Http\Requests\User\Rules\EmailRules;
use Brewmap\Http\Requests\User\Rules\PasswordRules;

class LoginUserRequest extends BaseRequest
{
public function rules(): array
{
return [
"email" => EmailRules::rules(),
"password" => PasswordRules::rules(),
];
}
}
26 changes: 26 additions & 0 deletions app/Http/Requests/User/RegisterUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Requests\User;

use Brewmap\Http\Requests\BaseRequest;
use Brewmap\Http\Requests\User\Rules\EmailRules;
use Brewmap\Http\Requests\User\Rules\NameRules;
use Brewmap\Http\Requests\User\Rules\PasswordRules;

class RegisterUserRequest extends BaseRequest
{
public function rules(): array
{
return [
"email" => EmailRules::rules([
"unique:users",
]),
"password" => PasswordRules::rules([
"confirmed",
]),
"name" => NameRules::rules(),
];
}
}
15 changes: 15 additions & 0 deletions app/Http/Requests/User/Rules/EmailRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Requests\User\Rules;

use Brewmap\Http\Requests\BaseRules;

class EmailRules extends BaseRules
{
protected static array $rules = [
"required",
"email",
];
}
17 changes: 17 additions & 0 deletions app/Http/Requests/User/Rules/NameRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Requests\User\Rules;

use Brewmap\Http\Requests\BaseRules;

class NameRules extends BaseRules
{
protected static array $rules = [
"required",
"min:4",
"max:20",
"alpha_dash",
];
}
16 changes: 16 additions & 0 deletions app/Http/Requests/User/Rules/PasswordRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Brewmap\Http\Requests\User\Rules;

use Brewmap\Http\Requests\BaseRules;

class PasswordRules extends BaseRules
{
protected static array $rules = [
"required",
"string",
"min:8",
];
}
Loading

0 comments on commit b915e13

Please sign in to comment.