my-idlers/app/Http/Controllers/Auth/RegisteredUserController.php

62 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
2022-03-05 16:58:25 +01:00
use App\Models\Settings;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules;
class RegisteredUserController extends Controller
{
/**
* Display the registration view.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
public function create()
{
$users = User::all();
if ($users->count() >= 1) {//Only allow 1 user for this app
return redirect('/login');
}
return view('auth.register');
}
/**
* Handle an incoming registration request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'api_token' => Str::random(60)
]);
event(new Registered($user));
Auth::login($user);
return redirect('/');
}
}