Reputation: 644
I am writing API login register system for my app.
I have created a controller for login and register, so I can create my user from postmen and log in from there but when I try to call my get details
method it's returning
as a method not allowed an exception
here is my Controller
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Validator;
class PassportController extends Controller
{
//
public $successStatus = 200;
public function register(Request $request){
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
return response()->json(['success'=>$success], $this->successStatus);
}
public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
public function getDetails()
{
$user = Auth::user();
return response()->json(['success' => $user], $this->successStatus);
}
}
api.php
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('register', 'API\PassportController@register')->name('register');
Route::post('login', 'API\PassportController@login')->name('login');
Route::group(['middleware' => 'auth:api'], function(){
Route::post('details', 'API\PassportController@getDetails');
});
here is my postmen screenshot
you can share me your thoughts or any ideas
Upvotes: 0
Views: 4462
Reputation: 1140
Please add your function url in app->http->Middleware->VerifyCsrfToken in $except .
laravel need csrf . if you pass data from outside it will not pass csrf so at that time you have to define for this function no csrf required .
Upvotes: 2
Reputation: 702
Are you using passport with laravel? If yes there might be multiple issues causing this.
I would like to see what you get when you click on application frames, but here are some of the more common issues i found myself dealing with
General issues I know of (regardless of technology)
when using the Authorization it need to be like this: Headers tab
key: Authorization Value: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiI
where this gibberish is your access token without any commas
check you server and laravel logs. Sometimes postman messes up and send get instead of post requests and the other way around
If you get Unauthenticated error
4.1 follow these steps here: https://laravel.com/docs/5.6/passport
4.2 copy the client secret from the db/command line
4.3 use this endpoint in the picture
{{url_key}} id the address to my site ex: demo.dev or htttp://localhost. I'm not sure about using locahost (I only developed laravel on vagrant so i don't know what issues may arise from that)
Headers should be empty in this case
4.4 use the token to authenticate
class VerifyCsrfToken extends BaseVerifier { /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ 'api/*' ]; }
Upvotes: 3
Reputation: 4365
You method is getDetails()
while your route is post
Route::post('details', 'API\PassportController@getDetails');
Try changing getDetails()
to postDetails()
and change route as
Route::post('details', 'API\PassportController@postDetails');
Upvotes: 2