Level 104
Laravel Socialite - there are plenty of resources online describing how to integrate with Google.
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey, I want to login/signup user through their google account how can i create api for this.
Laravel Socialite - there are plenty of resources online describing how to integrate with Google.
@tykus yes I know how to implement sso with socialite package but I don't know how can I create API for this.
@amitsolanki24_ what do you mean by create an API for this? Are you unable to use the stateless authentication approach described in the docs?
@tykus Here is my code please check is this okay or Ishould I chagne something?
##Route
Route::post('auth/twitter/login', [SocialiteController::class, 'twitterLogin']);
class SocialiteController extends Controller
{
public function twitterLogin(TwitterLoginRequest $request)
{
try {
return $request->LoginOrRegister();
} catch (\Throwable $e) {
return response()->failedRequest($e);
}
}
}
##TwitterLoginRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class TwitterLoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
function LoginOrRegister()
{
$name = explode(' ', $this->name);
$user = User::firstOrCreate(['email' => $this->email], [
'first_name' => $name[0],
'last_name' => !empty($name[1]) ? $name[1] : '',
'email' => $this->email,
'twitter_id' => $this->id,
'is_submit' => 1,
'profile_type' => 1,
'is_confirm' => 1,
'is_subscribe' => 1,
'timezone' => $this->timezone,
'twitter_profile_link' => $this->profile_pic,
]);
$user->getAccessToken();
return response()->success('You are loggedIn succcessfully!', $user);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
'id' => 'required',
'name' => 'required',
'email' => 'required|email',
'timezone' => 'nullable',
'profile_pic' => 'nullable|string',
];
}
public function messages()
{
return [
'name.required' => 'Name is required!',
];
}
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
$response = response()->badRequest($validator->errors()->first(), $validator->errors());
throw new \Illuminate\Validation\ValidationException($validator, $response);
}
}
Please or to participate in this conversation.