Created
January 28, 2017 12:04
-
-
Save yooouuri/7181442dc2d554293ca803cccd4128df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers\Admin; | |
| use App\User; | |
| use Illuminate\Http\Request; | |
| use App\Http\Controllers\Controller; | |
| use Illuminate\Support\Facades\Auth; | |
| use Illuminate\Support\Facades\Validator; | |
| class UserController extends Controller | |
| { | |
| /** | |
| * @param Request $request | |
| * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector | |
| */ | |
| public function login(Request $request) | |
| { | |
| $rules = [ | |
| 'email' => 'required|email|exists:users,email', | |
| 'password' => 'required', | |
| ]; | |
| $validator = Validator::make($request->all(), $rules); | |
| /** @var User $user */ | |
| $user = Auth::attempt([ | |
| 'email' => $request->email, | |
| 'password' => $request->password | |
| ]); | |
| $validator->after(function ($validator) use ($user) { | |
| if (!$user) { | |
| $validator->errors()->add('field', 'Something is wrong with this field!'); | |
| } | |
| }); | |
| if ($validator->fails()) { | |
| return redirect('/admin') | |
| ->withErrors($validator); | |
| } else { | |
| if ($user->isAdmin()) { | |
| return redirect() | |
| ->route('admin.dashboard'); | |
| } | |
| } | |
| return redirect('/admin/login'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment