Compare commits
2 Commits
6722c9a75d
...
b98e545826
Author | SHA1 | Date |
---|---|---|
b1ek | b98e545826 | |
b1ek | 5d3c41cfa0 |
|
@ -2,62 +2,34 @@
|
||||||
|
|
||||||
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 Validator;
|
use App\Services\UserService;
|
||||||
|
|
||||||
class PublicUserController extends Controller
|
class PublicUserController extends Controller
|
||||||
{
|
{
|
||||||
|
public function __construct(
|
||||||
|
private UserService $userService
|
||||||
|
) { }
|
||||||
|
|
||||||
public function register(RegisterRequest $request)
|
public function register(RegisterRequest $request)
|
||||||
{
|
{
|
||||||
$data = $request->all();
|
$this->userService->create($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)
|
|
||||||
{
|
{
|
||||||
$user = User::where([ 'email' => $request->input('email') ])->get();
|
if (!$this->userService->login($request->all())) {
|
||||||
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(Request $request)
|
public function reset(LoginRequest $request)
|
||||||
{
|
{
|
||||||
$user = User::where([ 'email' => $request->input('email') ])->get();
|
$this->userService->reset($request->all());
|
||||||
if ($user->count() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$user = $user[0];
|
|
||||||
$user->password = Hash::make($request->input('new_pass'));
|
|
||||||
$user->save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?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 ])],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
<?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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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->foreignId('user_id')->nullable()->index();
|
$table->string('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');
|
||||||
|
|
|
@ -111,7 +111,7 @@ paths:
|
||||||
email:
|
email:
|
||||||
type: string
|
type: string
|
||||||
example: 'jdoe@example.com'
|
example: 'jdoe@example.com'
|
||||||
new_pass:
|
password:
|
||||||
type: string
|
type: string
|
||||||
example: 'very_strong_password123456'
|
example: 'very_strong_password123456'
|
||||||
responses:
|
responses:
|
||||||
|
|
Loading…
Reference in New Issue