71 lines
1.6 KiB
PHP
71 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function create()
|
|
{
|
|
return view('users.register');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$formFields = $request->validate([
|
|
'rz_username' => ['required', Rule::unique('users', 'rz_username')],
|
|
'full_name' => ['required', 'min:3'],
|
|
'organisation_unit' => 'required',
|
|
'password' => 'required|min:6'
|
|
//'password' => 'required|confirmed|min:6'
|
|
]);
|
|
|
|
// Hash Password
|
|
$formFields['hashed_password'] = bcrypt($formFields['password']);
|
|
|
|
$user = User::create($formFields);
|
|
|
|
auth()->login($user);
|
|
|
|
Log::info('User successfully created');
|
|
return redirect('/home');
|
|
}
|
|
|
|
public function login()
|
|
{
|
|
return view('users.login');
|
|
}
|
|
|
|
public function authenticate(Request $request)
|
|
{
|
|
$formFields = $request->validate([
|
|
'rz_username' => 'required',
|
|
'password' => 'required'
|
|
]);
|
|
|
|
if (auth()->attempt($formFields)) {
|
|
$request->session()->regenerate();
|
|
|
|
Log::info('User authenticated successfully');
|
|
|
|
return redirect('/home');
|
|
}
|
|
}
|
|
|
|
public function logout(Request $request)
|
|
{
|
|
auth()->logout();
|
|
|
|
$request->session()->invalidate();
|
|
$request->session()->regenerateToken();
|
|
|
|
Log::info('User successfully logged out');
|
|
|
|
return redirect('/home');
|
|
}
|
|
}
|