I've been looking all over the internet for 2 days for a solution for my problem to no avail. I'm new to Laravel and API's as a whole, so bear with me.
I've managed to create a working REST API with the use of passport
via composer
:
The routes in routes/api.php
:
Route::post('login', 'PassportController@login');
Route::post('register', 'PassportController@register');
And the controller code for login
and register
:
public function login(Request $request){
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required'
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')->accessToken;
return response()->json(['success' => $success], 200);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required'
]);
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], 200);
}
This API works on localhost
but not when i open to the internet with:
php artisan serve --host 192.168.x.x --port 80
connected to a bought domain via port forwarding.
only the GET
methods works, but the POST
methods gives the error:
405 Method Not Allowed
As far as i could figure out, it has something to do with redirecting from http to https:
I have a same problem on a half of month ago. The reason is when I post on valid route, it redirects from http to https (configured by .htaccess), so "POST" method becomes "GET" and you see a MethodNotAllowedException.
Check your browser and follow the request, you may see the accurate problem.
from Laravel: POST method returns MethodNotAllowedHttpException
If this is the case for me as well, how do I fix the problem? The .htaccess
file has nothing with https
in it. Sorry if the answer is in the quoted answer but it don't know how to use that information.
UPDATE:
I found something about a csrf token problem https://laracasts.com/discuss/channels/laravel/405-method-not-allowed-laravel-55-api-passport-help. But apperently it could be fixed via entering the URIs for the POST
methods in the VerifyCsrfToken
file in the Middleware
folder, like so:
protected $except = [
'api/register',
'api/login'
];
I still get the MethodNotAllowed error..... This is starting to really annoy as there seem to be no answers online. HELP!
Apperently i was missing the "www" when i was doing requests.
I had the following in postman:
mysite.com/api/login
but i needed:
www.mysite.com/api/login
Embarrassingly small rookie mistake that costed me 5 days to figure out... I hope this will help any new webdevelopers that run into the same issue so they don't waste time like i did.