|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\AdminAuth; |
| 4 | + |
| 5 | +use App\User; |
| 6 | +use Validator; |
| 7 | +use App\Http\Controllers\Controller; |
| 8 | +use Illuminate\Foundation\Auth\ThrottlesLogins; |
| 9 | +use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Illuminate\Support\Facades\Auth; |
| 12 | +use App\Admin; |
| 13 | +class AuthController extends Controller |
| 14 | +{ |
| 15 | + /* |
| 16 | + |-------------------------------------------------------------------------- |
| 17 | + | Registration & Login Controller |
| 18 | + |-------------------------------------------------------------------------- |
| 19 | + | |
| 20 | + | This controller handles the registration of new users, as well as the |
| 21 | + | authentication of existing users. By default, this controller uses |
| 22 | + | a simple trait to add these behaviors. Why don't you explore it? |
| 23 | + | |
| 24 | + */ |
| 25 | + |
| 26 | + use AuthenticatesAndRegistersUsers, ThrottlesLogins; |
| 27 | + |
| 28 | + /** |
| 29 | + * Where to redirect users after login / registration. |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $redirectTo = '/admin'; |
| 34 | + protected $guard = 'admin'; |
| 35 | + |
| 36 | + public function showLoginForm() |
| 37 | + { |
| 38 | + if(view()->exists('auth.authenticate')) { |
| 39 | + return view('auth.authenticate'); |
| 40 | + } |
| 41 | + return view('admin.auth.login'); |
| 42 | + } |
| 43 | + public function showRegistrationForm() |
| 44 | + { |
| 45 | + return view('admin.auth.register'); |
| 46 | + } |
| 47 | + /** |
| 48 | + * Create a new authentication controller instance. |
| 49 | + * |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function __construct() |
| 53 | + { |
| 54 | + $this->middleware($this->guestMiddleware(), ['except' => 'logout']); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get a validator for an incoming registration request. |
| 59 | + * |
| 60 | + * @param array $data |
| 61 | + * @return \Illuminate\Contracts\Validation\Validator |
| 62 | + */ |
| 63 | + protected function validator(array $data) |
| 64 | + { |
| 65 | + return Validator::make($data, [ |
| 66 | + 'name' => 'required|max:255', |
| 67 | + 'email' => 'required|email|max:255|unique:users', |
| 68 | + 'password' => 'required|min:6|confirmed', |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Create a new user instance after a valid registration. |
| 74 | + * |
| 75 | + * @param array $data |
| 76 | + * @return User |
| 77 | + */ |
| 78 | + protected function create(array $data) |
| 79 | + { |
| 80 | + return Admin::create([ |
| 81 | + 'name' => $data['name'], |
| 82 | + 'email' => $data['email'], |
| 83 | + 'password' => bcrypt($data['password']), |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Handle a registration request for the application. |
| 89 | + * |
| 90 | + * @param \Illuminate\Http\Request $request |
| 91 | + * @return \Illuminate\Http\Response |
| 92 | + */ |
| 93 | + public function postRegister(Request $request) |
| 94 | + { |
| 95 | + return $this->register($request); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Handle a registration request for the application. |
| 100 | + * |
| 101 | + * @param \Illuminate\Http\Request $request |
| 102 | + * @return \Illuminate\Http\Response |
| 103 | + */ |
| 104 | + public function register(Request $request) |
| 105 | + { |
| 106 | + $validator = $this->validator($request->all()); |
| 107 | + |
| 108 | + if ($validator->fails()) { |
| 109 | + $this->throwValidationException( |
| 110 | + $request, $validator |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + Auth::guard($this->getGuard())->login($this->create($request->all())); |
| 115 | + |
| 116 | + return redirect($this->redirectPath()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get the guard to be used during registration. |
| 121 | + * |
| 122 | + * @return string|null |
| 123 | + */ |
| 124 | + protected function getGuard() |
| 125 | + { |
| 126 | + return property_exists($this, 'guard') ? $this->guard : null; |
| 127 | + } |
| 128 | +} |
0 commit comments