diff --git a/app/Http/Controllers/Auth/AuthenticatedSessionController.php b/app/Http/Controllers/Auth/AuthenticatedSessionController.php index a33d282..2014617 100644 --- a/app/Http/Controllers/Auth/AuthenticatedSessionController.php +++ b/app/Http/Controllers/Auth/AuthenticatedSessionController.php @@ -4,35 +4,56 @@ use App\Http\Controllers\Controller; use App\Http\Requests\Auth\LoginRequest; +use App\Models\User; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Str; class AuthenticatedSessionController extends Controller { - /** - * Handle an incoming authentication request. - */ - public function store(LoginRequest $request): Response - { - $request->authenticate(); - - $request->session()->regenerate(); - - return response()->noContent(); - } - - /** - * Destroy an authenticated session. - */ - public function destroy(Request $request): Response - { - Auth::guard('web')->logout(); - - $request->session()->invalidate(); - - $request->session()->regenerateToken(); - - return response()->noContent(); - } + /** + * Handle an incoming authentication request. + */ + public function store(LoginRequest $request): Response + { + $request->authenticate(); + + $request->session()->regenerate(); + + return response()->noContent(); + } + + /** + * Destroy an authenticated session. + */ + public function destroy(Request $request): Response + { + Auth::guard('web')->logout(); + + $request->session()->invalidate(); + + $request->session()->regenerateToken(); + + return response()->noContent(); + } + + public function socialMediaLogin(Request $request) + { + $user = User::updateOrCreate( + [ + 'provider_user_id' => $request->id + ], + [ + 'name' => $request->name, + 'email' => $request->email, + 'provider' => $request->provider, + 'provider_user_id' => $request->id, + ] + ); + + Auth::login($user); + + return response()->json(Auth::user(), 201); + } } diff --git a/app/Models/User.php b/app/Models/User.php index e81749e..4c7b4d7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -24,6 +24,8 @@ class User extends Authenticatable implements MustVerifyEmail 'name', 'email', 'password', + 'provider', + 'provider_user_id', ]; /** @@ -34,6 +36,8 @@ class User extends Authenticatable implements MustVerifyEmail protected $hidden = [ 'password', 'remember_token', + 'provider', + 'provider_user_id', ]; /** diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 444fafb..601b424 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -16,7 +16,7 @@ public function up(): void $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); - $table->string('password'); + $table->string('password')->nullable(); $table->rememberToken(); $table->timestamps(); }); diff --git a/database/migrations/2024_02_18_222612_add_column_created_by_to_sales.php b/database/migrations/2024_02_18_222612_add_column_created_by_to_sales.php index 3e63613..68cda02 100644 --- a/database/migrations/2024_02_18_222612_add_column_created_by_to_sales.php +++ b/database/migrations/2024_02_18_222612_add_column_created_by_to_sales.php @@ -13,7 +13,9 @@ public function up(): void { Schema::table('sales', function (Blueprint $table) { $table->after('price', function (Blueprint $table) { - $table->foreignId('created_by')->constrained('users'); + if (!Schema::hasColumn('document_numbers', 'uuid')) { + $table->foreignId('created_by')->constrained('users'); + } }); }); } diff --git a/database/migrations/2024_03_16_002705_add_column_on_user_table.php b/database/migrations/2024_03_16_002705_add_column_on_user_table.php new file mode 100644 index 0000000..034368e --- /dev/null +++ b/database/migrations/2024_03_16_002705_add_column_on_user_table.php @@ -0,0 +1,29 @@ +after('remember_token', function (Blueprint $table) { + $table->string('provider'); + $table->string('provider_user_id'); + }); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +}; diff --git a/routes/api.php b/routes/api.php index e3882ec..016d5c5 100644 --- a/routes/api.php +++ b/routes/api.php @@ -11,21 +11,28 @@ use App\Http\Controllers\Api\DropdownMenuController; use App\Http\Controllers\Api\PurchaseController; use App\Http\Controllers\Api\SaleController; +use App\Models\User; Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) { return $request->user(); }); -Route::put('/purchases/update-quantity/{id}', [PurchaseController::class, 'updateQuantity']); -Route::post('/purchases/add-product', [PurchaseController::class, 'addProducts']); +Route::get('/test', function () { + return User::query()->where('id', 10)->value('email'); +}); Route::middleware(['auth:sanctum'])->group(function () { Route::apiResource('/brands', BrandController::class); Route::apiResource('/categories', CategoryController::class); Route::apiResource('/products', ProductController::class); - Route::apiResource('/purchases', PurchaseController::class); Route::apiResource('/sales', SaleController::class); + Route::prefix('purchases')->group(function () { + Route::put('/update-quantity/{id}', [PurchaseController::class, 'updateQuantity']); + Route::post('/add-product', [PurchaseController::class, 'addProducts']); + }); + Route::apiResource('/purchases', PurchaseController::class); + Route::get('/dropdown/categories', [DropdownMenuController::class, 'categories']); Route::get('/dropdown/brands', [DropdownMenuController::class, 'brands']); }); diff --git a/routes/web.php b/routes/web.php index 6a18ac5..04d312e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ app()->version(), csrf_token()]; + return ['Laravel' => app()->version()]; }); +Route::post('/auth/callback', [AuthenticatedSessionController::class, 'socialMediaLogin']) + ->middleware('guest'); + require __DIR__ . '/auth.php';