LoginController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Providers\RouteServiceProvider;
  5. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  6. Use Redirect;
  7. use Auth;
  8. use App\Models\User;
  9. use Hash;
  10. use Illuminate\Http\Request;
  11. use Validator;
  12. class LoginController extends Controller
  13. {
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Login Controller
  17. |--------------------------------------------------------------------------
  18. |
  19. | This controller handles authenticating users for the application and
  20. | redirecting them to your home screen. The controller uses a trait
  21. | to conveniently provide its functionality to your applications.
  22. |
  23. */
  24. use AuthenticatesUsers;
  25. /**
  26. * Where to redirect users after login.
  27. *
  28. * @var string
  29. */
  30. protected $redirectTo = RouteServiceProvider::HOME;
  31. /**
  32. * Create a new controller instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. $this->middleware('guest')->except('logout');
  39. }
  40. public function postLogin(Request $request)
  41. {
  42. // dd($request->all());
  43. $validator = Validator::make($request->all(), [
  44. 'username' => 'required',
  45. 'password' => 'required|min:8',
  46. ]);
  47. if ($validator->fails()) {
  48. Redirect::to('/')
  49. ->withErrors($validator)
  50. ->withInput()->send();
  51. } else {
  52. $credentials = $request->only('username', 'password');
  53. //dd($credentials);
  54. $password = Hash::make($credentials['password']);
  55. $user = User::where('username', $credentials['username'])
  56. // ->where('password', $password)
  57. // ->where('role','restaurant')
  58. ->where('is_active', 1)
  59. ->first();
  60. if ($user && Hash::check($credentials['password'], $user->password)) {
  61. Auth::loginUsingId($user->user_id);
  62. // -- OR -- //
  63. Auth::login($user, true);
  64. if($user->role=="admin_user")
  65. return redirect()->intended('/blogs');
  66. if($user->role=="resto_user"){
  67. if($user->resto_users->role=="staff")
  68. return redirect()->intended('/orders');
  69. }
  70. return redirect()->intended('/dashboard');
  71. } else {
  72. return redirect()->intended('/')->withErrors([$this->username() => 'You have entered an invalid username or password']);
  73. }
  74. //
  75. }
  76. }
  77. }