Compare commits

..

No commits in common. "b98e5458260c4668ad3da0f5714018b045a9d3b0" and "6722c9a75d5fd598d51e269201336d6099af337a" have entirely different histories.

5 changed files with 41 additions and 100 deletions

View File

@ -2,34 +2,62 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Requests\LoginRequest;
use App\Http\Requests\RegisterRequest; use App\Http\Requests\RegisterRequest;
use App\Models\User; use App\Models\User;
use Hash; use Hash;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Services\UserService; use Validator;
class PublicUserController extends Controller class PublicUserController extends Controller
{ {
public function __construct(
private UserService $userService
) { }
public function register(RegisterRequest $request) public function register(RegisterRequest $request)
{ {
$this->userService->create($request->all()); $data = $request->all();
if (User::where([ 'email' => $data['email'] ])->count() != 0) {
return response()
->json('email_taken', 400);
} }
public function login(LoginRequest $request) /**
* @var User
*/
$user = User::create([
...$data,
'password' => Hash::make($request->input('password'))
]);
$user->save();
session()->put('user', $user->id);
session()->save();
}
public function login(Request $request)
{ {
if (!$this->userService->login($request->all())) { $user = User::where([ 'email' => $request->input('email') ])->get();
if ($user->count() == 0) {
return response() return response()
->json('bad_password', 400); ->json('bad_password', 400);
} }
$user = $user[0];
if (Hash::check($request->input('password'), $user->password)) {
session()->put('user', $user->id);
session()->save();
return;
}
return response()
->json('bad_password', 400);
} }
public function reset(LoginRequest $request) public function reset(Request $request)
{ {
$this->userService->reset($request->all()); $user = User::where([ 'email' => $request->input('email') ])->get();
if ($user->count() == 0) {
return;
}
$user = $user[0];
$user->password = Hash::make($request->input('new_pass'));
$user->save();
} }
} }

View File

@ -1,31 +0,0 @@
<?php
namespace App\Http\Requests;
use App\Rules\ZxcvbnRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
class LoginRequest extends RestRequest
{
/**
* 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'],
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule ])],
];
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace App\Services;
use App\Models\User;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class UserService
{
/**
* Create(register) new user
* @param array $data This is expected to be validated already
* @return User
*/
public function create($data, $autoLogin = true): User
{
if (User::where([ 'email' => $data['email'] ])->count() !== 0) {
throw new HttpResponseException(response()->json('email_taken', 400));
}
$data['password'] = Hash::make($data['password']);
$user = User::create($data);
if ($autoLogin) {
Auth::login($user);
}
return $user;
}
/**
* Login to user
* @param array $data This is expected to be validated already
* @return User
*/
public function login($data): bool
{
if (Auth::attempt($data)) {
request()->session()->regenerate();
return true;
}
return false;
}
public function reset($data)
{
$user = User::where([ 'email' => $data['email'] ])->first();
if ($user === null) {
return;
}
$user->password = Hash::make($data['password']);
$user->save();
}
}

View File

@ -35,7 +35,7 @@ return new class extends Migration
Schema::create('sessions', function (Blueprint $table) { Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary(); $table->string('id')->primary();
$table->string('user_id')->nullable()->index(); $table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable(); $table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable(); $table->text('user_agent')->nullable();
$table->longText('payload'); $table->longText('payload');

View File

@ -111,7 +111,7 @@ paths:
email: email:
type: string type: string
example: 'jdoe@example.com' example: 'jdoe@example.com'
password: new_pass:
type: string type: string
example: 'very_strong_password123456' example: 'very_strong_password123456'
responses: responses: