public function handleCallback($driver, Request $req)
 {
     $socialuser = \Socialize::with($driver)->user();
     $user = User::where('email', $socialuser->email)->first();
     if (count($user) < 1) {
         $user = new User();
         $user->id = (string) Uuid::generate();
         $user->email = $socialuser->email;
         $user->name = $socialuser->name;
         $user->avatar = $socialuser->avatar;
         $user->save();
     }
     $user = \Auth::loginUsingId($user->id);
     $authcookie = \Cookie::queue('authenticated', true, 20, null, "www.thinkmerit.in", false, false);
     $username = \Cookie::queue('name', $user->name, 20, null, "www.thinkmerit.in", false, false);
     $avatar = \Cookie::queue('avatar', $user->avatar, 20, null, "www.thinkmerit.in", false, false);
     $email = \Cookie::queue('email', $user->email, 20, null, "www.thinkmerit.in", false, false);
     if (app()->environment() == "production") {
         return \Redirect::to("http://www.thinkmerit.in");
     } else {
         return \Redirect::to("http://www.dev.thinkmerit.in");
     }
     /* ->withCookie($authcookie)
        ->withCookie($username)
        ->withCookie($avatar)
        ->withCookie($email);*/
     // ;
 }
 public function handleFacebookCallback()
 {
     //Since its the same callback function for registration & login. Check prev url and handle appropriately
     $prevUrl = session()->previousUrl();
     $facebookUser = Socialize::with('facebook')->user();
     $appUser = User::whereEmail($facebookUser->getEmail())->get();
     if (strpos($prevUrl, "register")) {
         //If user is not already registered, register and login
         if ($appUser->count() == 0) {
             $user = User::create(['name' => $facebookUser->getName(), 'email' => $facebookUser->getEmail(), 'password' => str_random(8)]);
             $user->verified = true;
             $user->token = null;
             $user->save();
             session()->flash('message', 'You are now registered. Please update your account details.');
             \Auth::loginUsingId($user->id);
             return redirect('/myAccount');
         } else {
             return redirect()->back()->withErrors('User already exists');
         }
     } else {
         if ($appUser->count() == 0) {
             return redirect()->back()->withErrors('User not found');
         }
         $user = $appUser[0];
     }
     \Auth::loginUsingId($user->id);
     return redirect()->intended('/dashboard');
 }
 public function manualregister(Request $request)
 {
     $validator = $this->validator($request->all());
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $arr = $request->all();
     $arr['id'] = (string) Uuid::generate();
     $arr['password'] = bcrypt($arr['password']);
     $user = new User($arr);
     $user->save();
     \Auth::loginUsingId($user->id);
     return Response::json(['msg' => 'Succsfully registered!'], 200);
 }
 public function authajax(Request $request)
 {
     $rules = array('email' => 'required|email', 'password' => 'required');
     $validator = \Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return Response::json(array('result' => false, 'data' => $validator->errors()->all()));
     }
     $email = $request->input('email');
     $password = $request->input('password');
     $member = Member::whereRaw('email = ? and password = ? ', [$email, md5($password)])->first();
     if ($member == null) {
         return Response::json(array('result' => false, 'data' => 'กรุณาลองใหม่อีกครั้ง.'));
     }
     $result = \Auth::loginUsingId($member->id);
     return Response::json(array('result' => true, 'data' => 'login success.'));
 }
 public function oauthCallback(Request $request)
 {
     if ($request->get('state') == session('state')) {
         $auth = new Auth(config('wechat.appid'), config('wechat.secret'));
         $ouser = $auth->user();
         if (!$ouser) {
             abort('微信授权错误 #13101');
         }
         $sa = SocialAccount::where('openid', $ouser->get('openid'))->where('platform', 'wechat')->first();
         if ($sa) {
             \Auth::loginUsingId($ouser->user_id);
         } else {
             \DB::transaction(function () use($ouser, $auth) {
                 $user = User::create(['name' => $ouser->get('nickname', ''), 'username' => $ouser->get('platform') . '_' . $ouser->get('openid', ''), 'avatar' => $ouser->get('headimgurl')]);
                 SocialAccount::create(['user_id' => $user->id, 'access_token' => $auth->access_token, 'refresh_token' => $auth->refresh_token, 'platform' => 'wechat', 'openid' => $ouser->get('openid'), 'user_info' => json_encode($ouser), 'union_id' => $ouser->get('union_id', '')]);
                 \Auth::loginUsingId($user->id);
             });
         }
         return redirect(route('home'));
     } else {
         abort('403', '服务器授权state不正确');
     }
 }