开发者

return errors from laravel exceptions Laravel

开发者 https://www.devze.com 2022-12-07 21:54 出处:网络
I\'m creating signup and login with google. Some times I get errors like email or username should 开发者_Python百科be unique as SQL exception. I want to redirect back with error for every error, knowi

I'm creating signup and login with google. Some times I get errors like email or username should 开发者_Python百科be unique as SQL exception. I want to redirect back with error for every error, knowing that there's no request bassed to the callback method.

the logic:

public function callback()
{
    $account = Socialite::driver('google')->stateless()->user();

    if(Auth::check()){
        $this->make_profile($account, Auth::id());
    }else{
        $p = Profile::where('google_id', $account->getId())->first();

        if(is_null($p)){
            if(!$this->make_user($account)){
                return redirect()->route('register')->with("error", 'حدث خطأ أثناء إنشاء حساب عبر google الرجاء إنشاء حساب عادي أو المحاولة لاحقًا!');
            }
        }else if(is_null($p->user)){
            $this->make_user($account);
        }else{
            if(Auth::login($p->user)){
                return redirect()->route('user.home');
            }else{
                return redirect()->route('login')->with("error", 'حدث خطأ أثناء تسجيل الدخول الرجاء المحاولة مجددًا!');
            }
        }
    }
}
public function make_user($account)
{
    $user = new User;
    ...
    if($user->save()){
        $this->make_profile($account, $user->id);
    }
}
public function make_profile($account, $id)
{
    $profile = Profile::firstOrNew(['user_id' => $id]);
    ...
    if($profile->save()){
        return redirect()->route('user.profile.apps')->with("success", 'تم ربط حساب Google بنجاح!');
    }else{
        return redirect()->route('user.profile.apps')->with("error", 'حدث خطأ أثناء ربط الحساب');
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消