|
@@ -0,0 +1,177 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\AdminAuth;
|
|
|
+
|
|
|
+use App\Http\Controllers\Controller;
|
|
|
+use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
+use Hesto\MultiAuth\Traits\LogsoutGuard;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+
|
|
|
+use App\Models\Admin;
|
|
|
+use App\Models\LoginRecords;
|
|
|
+use Session;
|
|
|
+
|
|
|
+class LoginController extends Controller
|
|
|
+{
|
|
|
+ /*
|
|
|
+ |--------------------------------------------------------------------------
|
|
|
+ | Login Controller
|
|
|
+ |--------------------------------------------------------------------------
|
|
|
+ |
|
|
|
+ | This controller handles authenticating users for the application and
|
|
|
+ | redirecting them to your home screen. The controller uses a trait
|
|
|
+ | to conveniently provide its functionality to your applications.
|
|
|
+ |
|
|
|
+ */
|
|
|
+
|
|
|
+ use AuthenticatesUsers, LogsoutGuard {
|
|
|
+ LogsoutGuard::logout insteadof AuthenticatesUsers;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Where to redirect users after login / registration.
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ public $redirectTo = '/admin/dashboard';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create a new controller instance.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->middleware('admin.guest', ['except' => 'logout']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Show the application's login form.
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\Response
|
|
|
+ */
|
|
|
+ public function showLoginForm()
|
|
|
+ {
|
|
|
+ return view('admin.auth.login');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function login(Request $request)
|
|
|
+ {
|
|
|
+ $admin = Admin::where('email',$request->email)->first();
|
|
|
+ if(!$admin){
|
|
|
+ return redirect('/')->with('error','Unknown Email address!');
|
|
|
+ }else{
|
|
|
+ if($admin->status == '0'){
|
|
|
+ return redirect('/')->with('error','Account Status is not Activated!');
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+
|
|
|
+ if($admin->type==5)
|
|
|
+ return redirect('https://sales.samscrm.co.uk')->with('success','Please login here!');
|
|
|
+
|
|
|
+
|
|
|
+ $credentials = [
|
|
|
+ 'email' => $request->email,
|
|
|
+ 'password' => $request->password,
|
|
|
+ 'status' => '1'
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (Auth::guard('admin')->attempt($credentials)) {
|
|
|
+ $time = date('y-m-d h:i:s',time());
|
|
|
+ $ip = $request->ip();
|
|
|
+
|
|
|
+ $records = new LoginRecords;
|
|
|
+ $records->auth_type = 'admins';
|
|
|
+ $records->auth_id = $admin->id;
|
|
|
+ $records->ip_address = $ip;
|
|
|
+ $records->start_time = $time;
|
|
|
+ $records->save();
|
|
|
+
|
|
|
+ $admin->active_status=1;
|
|
|
+ $admin->last_logged_timezone=$request->last_logged_timezone;
|
|
|
+ $admin->api_token=make_api_token('admins');
|
|
|
+ $admin->update();
|
|
|
+
|
|
|
+
|
|
|
+ $previous_session = $admin->session_id;
|
|
|
+ if($previous_session) {
|
|
|
+ Session::getHandler()->destroy($previous_session);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Session::put('session_start', $time);
|
|
|
+ Session::put('login_record', $records->id);
|
|
|
+ Session::put('admin_type', 'admins');
|
|
|
+
|
|
|
+ $admin->session_id = Session::getId();
|
|
|
+ $admin->update();
|
|
|
+
|
|
|
+ return redirect('admin/dashboard');
|
|
|
+ }else{
|
|
|
+ return redirect('/')->with('error','Wrong Email/Password combination');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get the guard to be used during authentication.
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Contracts\Auth\StatefulGuard
|
|
|
+ */
|
|
|
+ protected function guard()
|
|
|
+ {
|
|
|
+ return Auth::guard('admin');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function logout(){
|
|
|
+
|
|
|
+ $auth_user=Auth::guard('admin')->user();
|
|
|
+
|
|
|
+ if($auth_user){
|
|
|
+
|
|
|
+ $auth_user->update([
|
|
|
+ 'active_status'=>0,
|
|
|
+ 'api_token'=>NULL
|
|
|
+ ]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(Session::get('login_record')){
|
|
|
+ $record_id = Session::get('login_record');
|
|
|
+ $time = date('y-m-d h:i:s',time());
|
|
|
+ $records = LoginRecords::find($record_id);
|
|
|
+ if($records){
|
|
|
+ $records->end_time = $time;
|
|
|
+ $records->update();
|
|
|
+
|
|
|
+ if($records->login_source){
|
|
|
+ $source = $records->loginSource->source;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $previous_session = Auth::guard('admin')->session_id;
|
|
|
+ if($previous_session) {
|
|
|
+ Session::getHandler()->destroy($previous_session);
|
|
|
+ }
|
|
|
+ Auth::guard('admin')->logout();
|
|
|
+
|
|
|
+ if(!isset($source)){
|
|
|
+ return redirect('/');
|
|
|
+ }else{
|
|
|
+ return redirect($source);
|
|
|
+ }
|
|
|
+ //return redirect('/admin');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function showRegisterForm()
|
|
|
+ {
|
|
|
+ return view('admin.auth.register');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|