/**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $session_token = Input::get('session_token');
     $session = MobileSession::where('session_id', $session_token)->first();
     if (!$session) {
         return Response::json(['status' => 3, 'message' => 'Invalid user. Please login again.']);
     }
     return $next($request);
 }
Ejemplo n.º 2
0
 public static function getCustomer($session_token)
 {
     $session = MobileSession::where('session_id', $session_token)->first();
     if ($session) {
         $customer_id = $session->user_id;
         $customer = Customer::find($customer_id);
         return $customer;
     }
     throw new Exception("Invalid Session Token", 1);
 }
 public function login(Request $request)
 {
     try {
         $email = Input::get('email');
         $password = Input::get('password');
         $device_token = Input::get('device_token', 'null');
         $platform = Input::get('platform', 'A');
         $customer = Customer::where('email', $email)->first();
         if ($customer) {
             if ($customer->status == 0) {
                 return Response::json(['status' => 2, 'message' => 'Account is inactive.']);
             }
             if (Hash::check($password, $customer->password)) {
                 //Login Success
                 //Update platform and device token.
                 $customer->update(['platform' => $platform, 'device_token' => $device_token]);
                 //Delete old sessions
                 $old_session = MobileSession::where('user_id', $customer->id)->first();
                 //Save new session
                 Session::regenerate();
                 $session_token = Session::getId();
                 $current_datetime = date('Y-m-d H:i:s');
                 $session_data = ['user_id' => $customer->id, 'session_id' => $session_token, 'created_at' => $current_datetime, 'updated_at' => $current_datetime];
                 if ($old_session) {
                     MobileSession::find($old_session->id)->update($session_data);
                 } else {
                     MobileSession::create($session_data);
                 }
                 //Respond Success
                 $data = ['status' => 1, 'session_token' => $session_token];
                 return Response::json($data);
             }
             return Response::json(['status' => 2, 'message' => 'Incorrect Password.'], 401);
         }
         return Response::json(['status' => 2, 'message' => 'Incorrect Email.'], 401);
     } catch (Exception $e) {
         return Response::json(['status' => 0, 'message' => 'Something went wrong.'], 500);
     }
 }