feat: google login
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\RevokeSocialAuthorizations;
|
||||
use App\Http\Requests\EmailVerificationNotificationRequest;
|
||||
use App\Http\Requests\ForgotPasswordRequest;
|
||||
use App\Http\Requests\LoginRequest;
|
||||
@@ -295,8 +296,11 @@ class AuthController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(Request $request, RevenueCatService $revenueCat): JsonResponse
|
||||
{
|
||||
public function destroy(
|
||||
Request $request,
|
||||
RevenueCatService $revenueCat,
|
||||
RevokeSocialAuthorizations $revokeSocialAuthorizations,
|
||||
): JsonResponse {
|
||||
$user = $request->user();
|
||||
$identityVerificationRequest = $user->identityVerificationRequest()->first();
|
||||
$storagePaths = collect([$user->avatar_url])
|
||||
@@ -305,6 +309,8 @@ class AuthController extends Controller
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$revokeSocialAuthorizations->execute($user);
|
||||
|
||||
try {
|
||||
$revenueCat->deleteCustomer($user);
|
||||
} catch (Throwable $exception) {
|
||||
@@ -388,7 +394,7 @@ class AuthController extends Controller
|
||||
{
|
||||
$user = User::where('email', strtolower((string) $data['email']))->first();
|
||||
|
||||
if (! $user || ! Hash::check((string) $data['password'], $user->password)) {
|
||||
if (! $user || ! is_string($user->password) || ! Hash::check((string) $data['password'], $user->password)) {
|
||||
return response()->json([
|
||||
'code' => 'INVALID_CREDENTIALS',
|
||||
], 401);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\AuthenticateSocialUser;
|
||||
use App\Enums\SocialProvider;
|
||||
use App\Http\Requests\MobileSocialLoginRequest;
|
||||
use App\Http\Requests\MobileSocialNonceRequest;
|
||||
use App\Http\Resources\UserResource;
|
||||
use App\Services\SocialAuthenticationNonce;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class MobileSocialAuthController extends Controller
|
||||
{
|
||||
public function nonce(
|
||||
MobileSocialNonceRequest $request,
|
||||
SocialAuthenticationNonce $socialAuthenticationNonce,
|
||||
): JsonResponse {
|
||||
$provider = SocialProvider::from($request->validated('provider'));
|
||||
|
||||
return response()->json([
|
||||
'nonce' => $socialAuthenticationNonce->issue($provider),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(
|
||||
MobileSocialLoginRequest $request,
|
||||
AuthenticateSocialUser $authenticateSocialUser,
|
||||
): JsonResponse {
|
||||
$data = $request->validated();
|
||||
$user = $authenticateSocialUser->execute($data);
|
||||
$deviceName = $data['device_name'] ?? null;
|
||||
$tokenName = is_string($deviceName) && trim($deviceName) !== ''
|
||||
? trim($deviceName)
|
||||
: 'mobile';
|
||||
|
||||
return response()->json([
|
||||
'token' => $user->createToken($tokenName)->plainTextToken,
|
||||
'user' => new UserResource($user),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\SocialProvider;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MobileSocialLoginRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'authorizationCode' => [
|
||||
'nullable',
|
||||
Rule::requiredIf($this->input('provider') === SocialProvider::Apple->value),
|
||||
'string',
|
||||
'max:4096',
|
||||
],
|
||||
'deviceName' => ['sometimes', 'string', 'max:255'],
|
||||
'device_name' => ['sometimes', 'string', 'max:255'],
|
||||
'idToken' => ['required', 'string', 'max:10000'],
|
||||
'intent' => ['required', Rule::in(['login', 'register'])],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'name' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'nonce' => ['required', 'string', 'size:64', 'regex:/\A[a-f0-9]{64}\z/'],
|
||||
'provider' => ['required', Rule::enum(SocialProvider::class)],
|
||||
'termsAccepted' => ['required', 'boolean'],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('locale')) {
|
||||
$locale = strtolower(str_replace('_', '-', (string) $this->input('locale')));
|
||||
|
||||
$this->merge([
|
||||
'locale' => explode('-', $locale)[0],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->has('deviceName') && ! $this->has('device_name')) {
|
||||
$this->merge([
|
||||
'device_name' => $this->input('deviceName'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\SocialProvider;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MobileSocialNonceRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'provider' => ['required', Rule::enum(SocialProvider::class)],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user