The parameter app_id is required Socialite - php

Routes.php
Route::get('login/facebook', 'Auth\LoginController#redirectToProvider');
Route::get('login/facebook/callback', 'Auth\LoginController#handleProviderCallback');
Services.php
'facebook' => [
'client_id' => env('FB_KEY','188636635048329'), // Your facbook Client ID
'client_secret' => env('FB_SECRET','d16af66a2e168d05d87e76d618b48225'), // Your facbook Client Secret
'redirect' => 'FB_CALLBACK','http://localhost:8000/login/facebook/callback',
],
LoginController.php
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
return $user->token;
}
I followed up with laravel Socialite Documentation but it didn't work it just showed The parameter app_id is required i trued to clear Config cache but it didn't work neither
and i tried to put in .env file
FB_KEY = 188636635048329
FB_SECRET=d16af66a2e168d05d87e76d618b48225
FB_CALLBACK=http://localhost:8000/login/facebook/callback
but it didnt work so what else i can do to make it work

solution
The error was in the .env file i left some space in the FB_KEY so it should be like that
FB_KEY=188636635048329

Related

Laravel request url with parameter problem with GET Method

I have a route in api.php which look like this:
Route::get('auth/logout/{token}','UserController.php';
I tested this API endpoint using Postman with these configurations:
Method: GET
Params: key = token; value = $2y$10$Xji0VW1Qq9rtF04QlXDu1ePKNKHpRA2ppjDYWNFX.37C30sd3WSIu
Header: none
URL: localhost:8000/api/v1/logout?token=$2y$10$Xji0VW1Qq9rtF04QlXDu1ePKNKHpRA2ppjDYWNFX.37C30sd3WSIu
Here is my UserController#logout:
public function logout($token){
return response()->json([
'message' => 'Logout Success',
'token' => $token
], 200);
}
As you can see there, I just want to show a message and the $token parameter in Postman. But my problem is, Postman shows me a blank response. I can't access the URL with ? as the parameter separator. But I can access the URL with /, just like host/api/v1/auth/logout/{token_value}. But it is not what I desired. Anyone can help me?
You can remove the token route parameter:
Route::get('auth/logout', 'UserController.php');
And retrieve the token from the request in the controller:
public function logout(Request $request) {
return response()->json([
'message' => 'Logout Success',
'token' => $request->token
], 200);
}

Laravel: Registration and Login using Passport Oauth Token (ReflectionException: Controller does not exist)

So I'm trying to use the Laravel Api Passport and I'm getting this error.
ReflectionException:
Class App\Http\Controllers\Api\AuthController does not exist
Whenever I try to enter this command: "php artisan route:list"
My route codes are placed in my api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/register','Api\AuthController#register');
I guess the error is located in api.php but I dont know where is it.
Here's the AuthController.php codes
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
class AuthController extends Controller
{
public function register(Request $request){
$validatedData = $request->validate([
'email'=>'required',
'name'=>'required',
'password'=>'required'
]);
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
$http = new Client;
$response = $http->post(url('oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => '2',
'client_secret' => 'C0aJVstKoiTxx1HrCQEwBOYNKYHhMCGN9w6NUz57',
'username' => $request->email,
'password' => $request->password,
'scope' => '',
],
]);
return response(['data'=>json_decode((string) $response->getBody(), true)]);
}
public function login(){
}
}
I'm following this tutorial on youtube : https://www.youtube.com/watch?v=fiVA2Oko23o
It's my part two already I did everything explained in the tutorial but seems like there's something wrong with my codes I guess.
I'm so newbie I found that the problem is located in api.php
Auth should be place before Api
Route::post('/register','Auth\Api\AuthController#register');
I guess you forget to put '\'.
you should try this.
Route::post('/register','\Api\AuthController#register');
Laravel route always locates the controller class on \Controller namespace.
You should call the action by
http://{your_domain}/api/register

Laravel Socialite InvalidStateException in AbstractProvider.php when reloading my page after facebook login

My expected result is to have the user redirected to my homepage after facebook login. I am using Socialite, Laravel 5.4, and Xampp. After being able to login through facebook, my url is now in callback in which the callback is calling the logincontroller that redirects to the homepage. So problem is after redirected to homepage, my url has now some hashed value. localhost/sampleProject/public/login/facebook/callback?code=AQBZpjdW... and when I reload page it shows error invalid state exception in abstractprovider.php. Am I missing something in my functions?
Inside my login controller
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Obtain the user information from facebook.
*
* #return Response
*/
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
return view('user-profile', compact('user',$user));
}
Inside my services.php file
'facebook' => [
'client_id' => 'insert_app_id_here',
'client_secret' => 'enter_app_secret_here',
'redirect' => 'http://localhost/sampleProject/public/login/facebook/callback',
],
Inside my routes/web.php
Route::get('login/facebook', 'Auth\LoginController#redirectToProvider');
Route::get('login/facebook/callback', 'Auth\LoginController#handleProviderCallback');
I also have included these in my config/app.php
Laravel\Socialite\SocialiteServiceProvider::class,
and
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
You could try this thing)
public function handleProviderCallback(Request $request)
{
session()->put('state', $request->input('state'));
$user = Socialite::driver('facebook')->user();
return view('user-profile', compact('user',$user));
}
First of all, run the following command:
composer require laravel/socialite
After that In app/config.php add the following line in providers.
Laravel\Socialite\SocialiteServiceProvider::class,
After that In app/config.php add the following line in aliases
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
In config/services.php add:
//Socialite
'facebook' => [
'client_id' => '1234567890444',
'client_secret' => '1aa2af333336fffvvvffffvff',
'redirect' => 'http://laravel.dev/login/callback/facebook',
],
Now create two routes, mine are like these:
//Social Login
Route::get('/login/{provider?}',[
'uses' => 'AuthController#getSocialAuth',
'as' => 'auth.getSocialAuth'
]);
Route::get('/login/callback/{provider?}',[
'uses' => 'AuthController#getSocialAuthCallback',
'as' => 'auth.getSocialAuthCallback'
]);
You also need to create controller for the routes above like so:
<?php namespace App\Http\Controllers;
use Laravel\Socialite\Contracts\Factory as Socialite;
class AuthController extends Controller
{
public function __construct(Socialite $socialite){
$this->socialite = $socialite;
}
public function getSocialAuth($provider=null)
{
if(!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist
return $this->socialite->with($provider)->redirect();
}
public function getSocialAuthCallback($provider=null)
{
if($user = $this->socialite->with($provider)->user()){
dd($user);
}else{
return 'something went wrong';
}
}
}
and finally, add Site URL to your Facebook App. I followed this article for the proposed solution above. https://www.cloudways.com/blog/social-login-in-laravel-using-socialite/

Laravel 5 authenticate users through external API

I'd like to know is it possible to extend the built-in authentication to use an external API to authenticate a user? I'm a Laravel newbie, so I'd appreciate your help.
I'm making a custom app in Laravel 5.2 for my client, but I don't a direct access to their database server and I can only call their API to get users' details.
Thanks.
If I understood correctly you want to log users from APIs like facebook, twitter or github for example ? If that's so you need to use a laravel package named Socialite, here is the link to download and use it :
https://github.com/laravel/socialite
run on your command this :
composer require laravel/socialite
Next you need to tell laravel you want to use this package, so you need to add this in config/app.php :
'providers' => [
// Other service providers...
Laravel\Socialite\SocialiteServiceProvider::class,
],
and this is the aliases :
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Basically, you'll need to create an app on the developers site, i'll take facebook for this example.You need to go to this site :
https://developers.facebook.com/, create an account and you'll get your app url and secret key. You'll use it on your .env and config/services files.
In your config/services file add this after stripe :
'facebook' => [
'client_id' => env('FACEBOOK_ID'),
'client_secret' => env('FACEBOOK_SECRET'),
'redirect' => env('FACEBOOK_URL'),
],
And in your .env file :
FACEBOOK_ID=*your facebook id*
FACEBOOK_SECRET=*your facebook secret*
FACEBOOK_URL=http://yourwebsite.com/callback
Next you'll need a controller to handle the auth process, create something like SocialAuthController and put this in :
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
public function callback() {
$user = $this->findOrCreateFbUser(Socialite::driver('facebook')->user());
session([
'user' => $user
]);
return redirect()->route('/');
}
public function logout() {
session()->forget('user');
return redirect()->route('home');
}
protected function findOrCreateFbUser($fbUser) {
// the data you want to get from facebook
$fbData = [
'facebook_id' => $fbUser->id,
'avatar' => $fbUser->avatar,
'username' => $fbUser->name,
'email' => $fbUser->email,
];
$user = \App\User::where('facebook_id', $fbData['facebook_id'])->first();
if(!$user) $user = \App\User::create($fbData);
$user->update([
'avatar' => $fbUser->avatar,
'username' => $fbUser->name,
'email' => $fbUser->email
]);
return $user;
}
Of course you need to add a facebook_id field in your user database and model.
In User.php :
protected $fillable = [
'facebook_id',
'username',
'email',
'avatar'
];
I know this solution isn't really dynamic as it is for only one api, i'm still pretty new at Laravel too and this is my first answer to a stackoverflowquestion, but this did the trick for me :) If I forgot something don't hesitate to tell me so i can update this answer..
I also suggest you follow Jeffrey Way's tutorial on social auth on the Laracasts website, it's very instructive and clear, i could manage it thanks to him !

Laravel Refreshing JWT tokens

I have a small Lumen / Laravel app that is just used as an API. I am able to sign in and set JWT tokens but after a period of time they timeout, I was expecting them to refresh each time an Endpoint was hit.
I've been looking at the docs for Tymon's JWT-AUTH but I cannot seem to get it to work.
Below is an example of one of my end points which return an array of all the users in the db. But when the token timesout the endpoint returns the error You don't have previleges to view all users
I'd be very grateful if someone was able to advise me or show me how to make my code refresh a token when someone is hitting an endpoint.
Inside Controller
public function index(Request $request)
{
$user = JWTAuth::parseToken()->authenticate();
if (!$user->isAdmin()) {
return $this->error_respond(['error' => "You don't have previleges to view all users"]);
}
$users = $this->repository->findAllWithPlan();
return $this->respond(['users' => $users]);
}
Inside Routes.php
$app->group(['middleware' => 'jwt.auth'], function ($app) {
/**
* Show All users
*/
$app->get(
'users',
[
'as' => 'user.all',
'middleware' => 'cors',
'uses' => 'App\Http\Controllers\UserController#index'
]
);
});

Categories