Skip to content

Commit

Permalink
update migration and adding google signin
Browse files Browse the repository at this point in the history
  • Loading branch information
glenmoreilagan committed Mar 15, 2024
1 parent fdfdeb4 commit e25c783
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 31 deletions.
71 changes: 46 additions & 25 deletions app/Http/Controllers/Auth/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class User extends Authenticatable implements MustVerifyEmail
'name',
'email',
'password',
'provider',
'provider_user_id',
];

/**
Expand All @@ -34,6 +36,8 @@ class User extends Authenticatable implements MustVerifyEmail
protected $hidden = [
'password',
'remember_token',
'provider',
'provider_user_id',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
});
}
Expand Down
29 changes: 29 additions & 0 deletions database/migrations/2024_03_16_002705_add_column_on_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->after('remember_token', function (Blueprint $table) {
$table->string('provider');
$table->string('provider_user_id');
});
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
13 changes: 10 additions & 3 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
6 changes: 5 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -14,7 +15,10 @@
*/

Route::get('/', function () {
return ['Laravel' => app()->version(), csrf_token()];
return ['Laravel' => app()->version()];
});

Route::post('/auth/callback', [AuthenticatedSessionController::class, 'socialMediaLogin'])
->middleware('guest');

require __DIR__ . '/auth.php';

0 comments on commit e25c783

Please sign in to comment.