|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Auth; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use Laravel\Socialite\Facades\Socialite; |
| 7 | +use Litepie\Http\Response\AuthResponse; |
| 8 | +use Litepie\User\Traits\RoutesAndGuards; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use Illuminate\Foundation\Auth\AuthenticatesUsers; |
| 11 | +class SocialAuthController extends Controller |
| 12 | +{ |
| 13 | + /* |
| 14 | + |-------------------------------------------------------------------------- |
| 15 | + | Login Controller |
| 16 | + |-------------------------------------------------------------------------- |
| 17 | + | |
| 18 | + | This controller handles authenticating users for the application and |
| 19 | + | redirecting them to your home screen. The controller uses a trait |
| 20 | + | to conveniently provide its functionality to your applications. |
| 21 | + | |
| 22 | + */ |
| 23 | + use RoutesAndGuards; |
| 24 | + use AuthenticatesUsers; |
| 25 | + |
| 26 | + /** |
| 27 | + * Create a new controller instance. |
| 28 | + * |
| 29 | + * @return void |
| 30 | + */ |
| 31 | + public function __construct() |
| 32 | + { |
| 33 | + $this->response = resolve(AuthResponse::class); |
| 34 | + $this->middleware('guest', ['except' => 'logout']); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Redirect the user to the provider authentication page. |
| 39 | + * |
| 40 | + * @return Response |
| 41 | + */ |
| 42 | + public function redirectToProvider($provider) |
| 43 | + { |
| 44 | + if (!config("services.{$provider}.client_id")) { |
| 45 | + abort(404, "Please configure the [{$provider}] parameters."); |
| 46 | + } |
| 47 | + |
| 48 | + $this->setCallbackUrl($provider); |
| 49 | + |
| 50 | + return Socialite::driver($provider)->redirect(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Obtain the user information from provider. |
| 55 | + * |
| 56 | + * @return Response |
| 57 | + */ |
| 58 | + public function handleProviderCallback($provider) |
| 59 | + { |
| 60 | + $this->setCallbackUrl($provider); |
| 61 | + $guard = $this->getGuard(); |
| 62 | + $user = Socialite::driver($provider)->user(); |
| 63 | + $model = $this->getAuthModel(); |
| 64 | + $data = [ |
| 65 | + 'name' => $user->getName(), |
| 66 | + 'email' => $user->getEmail(), |
| 67 | + 'status' => 'Active', |
| 68 | + 'password' => bcrypt(Str::random(8)), |
| 69 | + 'api_token' => Str::random(60), |
| 70 | + ]; |
| 71 | + $user = $model::whereEmail($data['email'])->first(); |
| 72 | + |
| 73 | + if (!is_null($user)) { |
| 74 | + app('auth')->login($user, false, $guard); |
| 75 | + } else { |
| 76 | + $user = $model::create($data); |
| 77 | + app('auth')->login($user, false, $guard); |
| 78 | + } |
| 79 | + |
| 80 | + return redirect()->intented($this->redirectTo); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * undocumented function. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + * |
| 88 | + * @author |
| 89 | + **/ |
| 90 | + public function setCallbackUrl($provider) |
| 91 | + { |
| 92 | + $guard = $this->getGuardRoute(); |
| 93 | + $currentUrl = config("services.{$provider}.redirect"); |
| 94 | + $newUrl = str_replace('/user/', "/$guard/", $currentUrl); |
| 95 | + config(["services.{$provider}.redirect" => $newUrl]); |
| 96 | + } |
| 97 | +} |
0 commit comments