LoginController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers\AdminAuth;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  6. use Illuminate\Support\Facades\Auth;
  7. use Hesto\MultiAuth\Traits\LogsoutGuard;
  8. use App\Models\Admin;
  9. use App\Models\LoginRecords;
  10. use Session;
  11. use Browser;
  12. use Carbon\Carbon;
  13. class LoginController extends Controller
  14. {
  15. /*
  16. |--------------------------------------------------------------------------
  17. | Login Controller
  18. |--------------------------------------------------------------------------
  19. |
  20. | This controller handles authenticating users for the application and
  21. | redirecting them to your home screen. The controller uses a trait
  22. | to conveniently provide its functionality to your applications.
  23. |
  24. */
  25. use AuthenticatesUsers, LogsoutGuard {
  26. LogsoutGuard::logout insteadof AuthenticatesUsers;
  27. }
  28. /**
  29. * Where to redirect users after login / registration.
  30. *
  31. * @var string
  32. */
  33. public $redirectTo = '/admin/dashboard';
  34. /**
  35. * Create a new controller instance.
  36. *
  37. * @return void
  38. */
  39. public function __construct()
  40. {
  41. $this->middleware('admin.guest', ['except' => 'logout']);
  42. }
  43. /**
  44. * Show the application's login form.
  45. *
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function showLoginForm()
  49. {
  50. return view('admin.auth.login');
  51. }
  52. public function login(Request $request)
  53. {
  54. $dateTime = date('y-m-d H:i:s',time());
  55. $admin = Admin::where('email',$request->email)->first();
  56. if(!$admin){
  57. return redirect('/admin')->with('error','Wrong Email/Password combination');
  58. }else{
  59. if($admin->status == '0'){
  60. return redirect('/admin')->with('error','Account is not Activated!');
  61. }else{
  62. $credentials = [
  63. 'email' => $request->email,
  64. 'password' => $request->password,
  65. 'status' => '1'
  66. ];
  67. if (Auth::guard('admin')->attempt($credentials)) {
  68. $ip = $request->ip();
  69. $browser = Browser::browserName();
  70. $platform = Browser::platformName();
  71. $device = Browser::deviceFamily();
  72. $records = new LoginRecords;
  73. $records->auth_type = 'Admin';
  74. $records->auth_id = $admin->id;
  75. $records->ip_address = $ip;
  76. $records->browser = $browser;
  77. $records->platform = $platform;
  78. $records->device = $device;
  79. $records->start_time = $dateTime;
  80. $records->save();
  81. Session::put('session_start', $dateTime);
  82. Session::put('login_record', $records->id);
  83. Session::put('admin_type', 'Admin');
  84. return redirect('/admin/dashboard');
  85. }else{
  86. return redirect('/admin')->with('error','Wrong Email/Password combination');
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Get the guard to be used during authentication.
  93. *
  94. * @return \Illuminate\Contracts\Auth\StatefulGuard
  95. */
  96. protected function guard()
  97. {
  98. return Auth::guard('admin');
  99. }
  100. public function logout(){
  101. if(Session::get('login_record')){
  102. $record_id = Session::get('login_record');
  103. $time = date('y-m-d H:i:s',time());
  104. $records = LoginRecords::find($record_id);
  105. if($records){
  106. $records->end_time = $time;
  107. $records->update();
  108. }
  109. }
  110. Auth::guard('admin')->logout();
  111. return redirect('/admin');
  112. }
  113. }