hypothetical/app/Http/Controllers/Auth/RegisterController.php

94 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Auth;
2019-10-31 17:24:53 -04:00
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Dashboard;
2019-10-31 17:24:53 -04:00
use Illuminate\Foundation\Auth\RegistersUsers;
2018-03-05 18:45:02 -05:00
use Illuminate\Support\Facades\Hash;
2016-10-26 11:05:43 -04:00
use Illuminate\Support\Facades\Validator;
2024-03-19 17:11:58 -04:00
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
2024-03-19 17:11:58 -04:00
class RegisterController extends Controller implements HasMiddleware
{
/*
|--------------------------------------------------------------------------
2016-08-19 16:38:49 -04:00
| Register Controller
|--------------------------------------------------------------------------
|
2016-08-19 16:38:49 -04:00
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
2016-08-19 16:38:49 -04:00
use RegistersUsers;
/**
2017-01-26 19:17:37 -05:00
* Where to redirect users after registration.
2016-01-03 23:55:13 -05:00
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
2024-03-19 17:11:58 -04:00
* Get the middleware that should be assigned to the controller.
*/
2024-03-19 17:11:58 -04:00
public static function middleware(): array
{
2024-03-19 17:11:58 -04:00
return [ 'guest' ];
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
2019-03-19 17:40:13 -04:00
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
if (Dashboard::canRegister()) {
return User::create([
2016-08-19 16:38:49 -04:00
'name' => $data['name'],
'email' => $data['email'],
2018-03-05 18:45:02 -05:00
'password' => Hash::make($data['password']),
'api_token' => str_random(60)
]);
} else {
abort(404);
}
}
2016-08-19 16:38:49 -04:00
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
if (Dashboard::canRegister()) {
2016-08-19 16:38:49 -04:00
return view('auth.register');
} else {
header('Location: /login');
}
}
}