Skip to content

Commit

Permalink
new file: app/Http/Controllers/RegisterController.php
Browse files Browse the repository at this point in the history
	modified:   app/Http/Controllers/UserController.php
	modified:   app/Http/Kernel.php
	new file:   app/Http/Middleware/HandleInertiaRequests.php
	new file:   app/Http/Requests/BlogCreateRequest.php
	modified:   app/Http/Requests/UserDetailsRequest.php
	new file:   app/Http/Requests/UserLoginRequest.php
	new file:   app/Models/Blog.php
	modified:   composer.json
	modified:   composer.lock
	new file:   database/migrations/2023_11_20_215425_blog-migration-file.php
	new file:   package-lock.json
	modified:   package.json
	new file:   postcss.config.js
	modified:   resources/css/app.css
	new file:   resources/js/Pages/Dashboard.jsx
	new file:   resources/js/Pages/Login.jsx
	new file:   resources/js/Pages/Register.jsx
	deleted:    resources/js/app.js
	new file:   resources/js/app.jsx
	new file:   resources/js/components/FormGroup.jsx
	new file:   resources/views/app.blade.php
	new file:   resources/views/blog.blade.php
	modified:   resources/views/welcome.blade.php
	modified:   routes/web.php
	new file:   tailwind.config.js
	modified:   vite.config.js
  • Loading branch information
cjeccles committed Nov 24, 2023
1 parent 2c84159 commit f411467
Show file tree
Hide file tree
Showing 27 changed files with 2,993 additions and 12 deletions.
14 changes: 14 additions & 0 deletions app/Http/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Controllers;

use Inertia\Inertia;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RegisterController extends Controller
{
public function show(Request $request) {
return Inertia::render('Register');
}
}
62 changes: 60 additions & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,72 @@

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Blog;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\UserLoginRequest;
use App\Http\Requests\BlogCreateRequest;
use App\Http\Requests\UserDetailsRequest;



class UserController extends Controller
{
public function register(UserDetailsRequest $request)
{
return "User created with email: {$request->email}";
$validatedData = $request->validated();

//create
$user = User::create([
'name' => $validatedData['name'],
'email' => $validatedData['email'],
'password' => $validatedData['password']
]);

//sql
//insert into table 'users' values (email, password) ....

return redirect()->back();
}

public function login(UserLoginRequest $request)
{
$validatedData = $request->validated();

// $user = User::where('email', $validatedData['email'])->first();
// $isSamePassword = Hash::check($validatedData['password'], $user->password);

// if ($user && $isSamePassword)
// {
// return true
// }

$isCorrectUser = Auth::attempt(['email' => $validatedData['email'], 'password' => $validatedData['password']]);

if ($isCorrectUser) {
return redirect('/dashboard');
}
else {
return redirect()->back();
}
}


public function createBlogArticle(BlogCreateRequest $request)
{
$validatedData = $request->validated();

$user = User::where('email', $validatedData['email'])->first();


Blog::create([
'title' => $validatedData['title'],
'article' => $validatedData['article'],
'user_id' => $user->id
]);

return redirect()->back();
}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\HandleInertiaRequests::class,
],

'api' => [
Expand Down
43 changes: 43 additions & 0 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
/**
* The root template that's loaded on the first page visit.
*
* @see https://inertiajs.com/server-side-setup#root-template
* @var string
*/
protected $rootView = 'app';

/**
* Determines the current asset version.
*
* @see https://inertiajs.com/asset-versioning
* @param \Illuminate\Http\Request $request
* @return string|null
*/
public function version(Request $request): ?string
{
return parent::version($request);
}

/**
* Defines the props that are shared by default.
*
* @see https://inertiajs.com/shared-data
* @param \Illuminate\Http\Request $request
* @return array
*/
public function share(Request $request): array
{
return array_merge(parent::share($request), [
//
]);
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/BlogCreateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BlogCreateRequest 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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'article' => ['required', 'string', 'max:5000'],
'email' => ['required', 'email:rfc,dns', 'exists:users,email']
];
}
}
4 changes: 3 additions & 1 deletion app/Http/Requests/UserDetailsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function authorize(): bool
public function rules(): array
{
return [
"email" => ['required', 'email:rfc,dns'],
"name" => ['required'],
"email" => ['required', 'email:rfc,dns', 'unique:users,email'],
"password" => ['required', 'min:6', 'max:12', '']
];
}
Expand All @@ -32,6 +33,7 @@ public function messages(): array
return [
"email.required" => "Please enter your email",
"email.email" => "Please enter a real email address",
"email.unique" => "User already registered with this email address",
"password.required" => "Please enter a pa55word",
"password.min" => "You must enter at least 6 characters for your password, please :)",
"password.max" => "Oops, your password is too long!"
Expand Down
29 changes: 29 additions & 0 deletions app/Http/Requests/UserLoginRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserLoginRequest 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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
"email" => ['required', 'email:rfc,dns', 'exists:users,email'],
"password" => ['required', 'min:6', 'max:12', '']
];
}
}
13 changes: 13 additions & 0 deletions app/Models/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
use HasFactory;

protected $guarded = [];
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": "^8.1",
"guzzlehttp/guzzle": "^7.2",
"inertiajs/inertia-laravel": "^0.6.11",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8"
Expand Down
74 changes: 73 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions database/migrations/2023_11_20_215425_blog-migration-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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::create('blogs', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->longText('article');
$table->foreignId('user_id')->constrained();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('blogs');
}
};
Loading

0 comments on commit f411467

Please sign in to comment.