|
| 1 | +<?php namespace App\Http\Controllers\Auth; |
| 2 | + |
| 3 | +use App\Http\Controllers\Controller; |
| 4 | +use Illuminate\Contracts\Auth\Guard; |
| 5 | +use Illuminate\Contracts\Auth\Registrar; |
| 6 | +use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use App\User; |
| 9 | + |
| 10 | +class AuthController extends Controller { |
| 11 | + |
| 12 | + /* |
| 13 | + |-------------------------------------------------------------------------- |
| 14 | + | Registration & Login Controller |
| 15 | + |-------------------------------------------------------------------------- |
| 16 | + | |
| 17 | + | This controller handles the registration of new users, as well as the |
| 18 | + | authentication of existing users. By default, this controller uses |
| 19 | + | a simple trait to add these behaviors. Why don't you explore it? |
| 20 | + | |
| 21 | + */ |
| 22 | + |
| 23 | + use AuthenticatesAndRegistersUsers; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new authentication controller instance. |
| 27 | + * |
| 28 | + * @param \Illuminate\Contracts\Auth\Guard $auth |
| 29 | + * @param \Illuminate\Contracts\Auth\Registrar $registrar |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function __construct(Guard $auth, Registrar $registrar) |
| 33 | + { |
| 34 | + $this->auth = $auth; |
| 35 | + $this->registrar = $registrar; |
| 36 | + |
| 37 | + $this->middleware('guest', ['except' => 'getLogout']); |
| 38 | + } |
| 39 | + |
| 40 | + public function postLogin(Request $request) |
| 41 | + { |
| 42 | + $this->validate($request, [ |
| 43 | + 'email' => 'required|email', 'password' => 'required', |
| 44 | + ]); |
| 45 | + |
| 46 | + $credentials = $request->only('email', 'password'); |
| 47 | + |
| 48 | + return $this->loginUser($credentials); |
| 49 | + } |
| 50 | + |
| 51 | + private function loginUser($credentials){ |
| 52 | + $model = User::where('email', $credentials['email'])->first(); |
| 53 | + if(isset($model)){ |
| 54 | + if($model['password'] == base64_encode($credentials['password'])){ |
| 55 | + $this->auth->login($model); |
| 56 | + return redirect($this->redirectPath()); |
| 57 | + }else{ |
| 58 | + return redirect($this->loginPath()) |
| 59 | + ->withInput($credentials) |
| 60 | + ->withErrors([ |
| 61 | + 'email' => $this->getFailedLoginMessage(), |
| 62 | + ]); |
| 63 | + } |
| 64 | + }else{ |
| 65 | + return redirect($this->loginPath()) |
| 66 | + ->withInput($credentials) |
| 67 | + ->withErrors([ |
| 68 | + 'email' => $this->getFailedLoginMessage(), |
| 69 | + ]); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function redirectPath() |
| 74 | + { |
| 75 | + if (property_exists($this, 'redirectPath')) |
| 76 | + { |
| 77 | + return $this->redirectPath; |
| 78 | + } |
| 79 | + |
| 80 | + return property_exists($this, 'redirectTo') ? $this->redirectTo : '/'; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +} |
0 commit comments