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

62 lines
1.5 KiB
PHP
Executable File

<?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()
->json('email_taken', 400);
}
/**
* @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 ($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);
}
public function reset(Request $request)
{
$user = User::where([ 'email' => $request->input('email') ])->get();
if ($user->count() == 0) {
return;
}
$user->password = Hash::make($request->input('password'));
$user->save();
}
}