Compare commits

...

2 Commits

Author SHA1 Message Date
b1ek d106ba956a
feat: /api/users/register 2024-08-29 19:48:29 +10:00
b1ek f32cb1832a
feat: User model 2024-08-29 19:48:10 +10:00
5 changed files with 69 additions and 2 deletions

View File

@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Hash;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Request;
use Validator;
class PublicUserController extends Controller
{
public function register(Request $request)
{
$data = $request->all()['user'];
if (User::where([ 'email' => $data['email'] ])->count() != 0) {
return response('email_taken', 400);
}
/**
* @var User
*/
$user = User::create([
...$data,
'password' => Hash::make($request->input('password'))
]);
$user->save();
session('user', $user->id);
session()->save();
}
}

View File

@ -3,13 +3,15 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\UuidId;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Infocyph\UID\UUID;
class User extends Authenticatable
{
use HasFactory, Notifiable;
use HasFactory, Notifiable, UuidId;
/**
* The attributes that are mass assignable.
@ -21,6 +23,8 @@ class User extends Authenticatable
'name',
'middle_name',
'email',
'phone',
'password',
];
/**

20
app/UuidId.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace App;
use Ramsey\Uuid\Uuid;
trait UuidId
{
protected static function boot() {
parent::boot();
static::creating(function ($model) {
$model->id = Uuid::uuid6()->toString();
});
}
public function getIncrementing(): bool {
return false;
}
public function getKeyType(): string {
return 'string';
}
}

View File

@ -17,6 +17,7 @@ return new class extends Migration
$table->string('name');
$table->string('middle_name');
$table->string('email')->unique();
$table->string('phone');
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();

View File

@ -1,7 +1,18 @@
<?php
use App\Http\Controllers\PublicUserController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
Route::get('/', function() {
return view('welcome');
});
Route::prefix('/api')->group(function() {
Route::controller(PublicUserController::class)->prefix('/users')->group(function() {
Route::put('/register', 'register');
});
});
Route::get('/session', function() {
exit(print_r(session()->all(), true));
});