megahunt.test/app/Http/Controllers/PublicUserController.php

51 lines
1.2 KiB
PHP
Raw Normal View History

2024-08-29 11:48:29 +02:00
<?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) {
2024-08-29 11:52:16 +02:00
return response()
->json('email_taken', 400);
2024-08-29 11:48:29 +02:00
}
/**
* @var User
*/
$user = User::create([
...$data,
'password' => Hash::make($request->input('password'))
]);
$user->save();
2024-08-29 12:34:01 +02:00
session()->put('user', $user->id);
2024-08-29 11:48:29 +02:00
session()->save();
}
2024-08-29 12:34:01 +02:00
public function login(Request $request)
{
$user = User::where([ 'email' => $request->input('email') ])->get();
if ($user->count() == 0) {
return response()
->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);
}
2024-08-29 11:48:29 +02:00
}