add auth api
This commit is contained in:
43
app/Repos/UserRepo.php
Normal file
43
app/Repos/UserRepo.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repos;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UserRepo
|
||||
{
|
||||
/**
|
||||
* Register new user
|
||||
*/
|
||||
public static function registerUser(Request $request): User
|
||||
{
|
||||
return User::create([
|
||||
'phone_number' => $request->phone_number,
|
||||
'name' => $request->name,
|
||||
'username' => static::generateUsername($request->name),
|
||||
'locale' => app()->getLocale(),
|
||||
'password' => Str::random(6),
|
||||
'active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate random and unique username
|
||||
*/
|
||||
public static function generateUsername(string $name): string
|
||||
{
|
||||
// Convert the full name to lowercase and replace spaces with underscores
|
||||
$username = Str::slug($name, '_');
|
||||
|
||||
$count = DB::table('users')->where('username', $username)->count();
|
||||
|
||||
if ($count > 0) {
|
||||
$username = $username.'_'.($count + 1);
|
||||
}
|
||||
|
||||
return $username;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user