Laravel route Error: Route [login] is not definded - laravel

I'm developing an Laravel API, and finding a bit difficult to find the right answer for following problem. The problem is I'm having that when I query my group/join endpoint I'm getting the following error.
InvalidArgumentException: Route [login] not defined. in file\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php on line 372
When I query my GroupController#getAll I'm getting results from the database. So now I'm a bit flustered about what to try next.
My Group Controller code (only the requests that are implemented)
/**
* #param CreateGroupRequest $request
*/
public function createGroup(CreateGroupRequest $request){
$data = $request->get('group', []);
}
/*
* #param JoinGroupUserRequest $request
*/
public function joinGroup(JoinGroupUserRequest $request){
$group_id = $request->group_id;
$user_id = $request->user_id;
$data = $this->groupService->joinGroup($group_id, $user_id);
return response()->json([], 201);
}
My Group repository
class GroupRepository{
/*
Get a new instance of empty Group Model
*/
public function GetModel(){
return new Group();
}
/**
* #param int $private
* #return mixed
*/
public function GetAllGroups($private = 0){
$groups = Group::where('group_private', $private)->get();
return $groups;
}
/**
* #param array $data
* #return Group
*/
public function CreateGroup(array $data){
$group = $this->GetModel();
$group->fill($data);
$group->save();
return $group;
}
public function AddUserToGroup(int $group_id, int $user_id){
$group_user = new GroupUsers();
$group_user->group_id = $group_id;
$group_user->user_id = $user_id;
$group_user->save();
return $group_user;
}
}
My GroupService
class GroupService
{
private $groupRepository;
/**
* GroupService constructor.
* #param php $groupRepository
*/
public function __construct(GroupRepository $groupRepository){
$this->groupRepository = $groupRepository;
}
/*
* Gets all Groups
*/
public function getAll()
{
return $this->groupRepository->GetAllGroups();
}
public function joinGroup($group_id, $user_id){
return $this->groupRepository->AddUserToGroup($group_id, $user_id);
}
}
My api routes
Route::group(['middleware' => 'web', 'prefix' => 'auth'], function () {
Route::post('login', 'AuthController#login');
Route::post('signup', 'AuthController#signup');
Route::get('social/{provider}', 'AuthController#signupSocial');
Route::get('callback/{service}', 'AuthController#callback');
Route::group(['middleware' => 'auth:api'], function() {
Route::get('logout', 'AuthController#logout');
Route::get('user', 'AuthController#user');
});
});
Route::group(['middleware' => 'auth:api', 'prefix' => 'group'], function () {
Route::get('/', 'GroupController#getAll');
Route::post('join', 'GroupController#joinGroup');
});
Postman request
POST /api/group/join HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjBhNThiOWY4NTBmZTNmNDZmMTQzYmM2NmY1NzVkZThkMTRiOGFhZjZjMWU5ZThiMjJjNDU3N2QzNmYxYTgyM2JkMjJiMTMzNzBhODcyODAxIn0.eyJhdWQiOiIzIiwianRpIjoiMGE1OGI5Zjg1MGZlM2Y0NmYxNDNiYzY2ZjU3NWRlOGQxNGI4YWFmNmMxZTllOGIyMmM0NTc3ZDM2ZjFhODIzYmQyMmIxMzM3MGE4NzI4MDEiLCJpYXQiOjE1MzM2NjY3NjIsIm5iZiI6MTUzMzY2Njc2MiwiZXhwIjoxNTY1MjAyNzYyLCJzdWIiOiI2Iiwic2NvcGVzIjpbXX0.s96tp1nxYJoXV8j1JNsmPKz0yw0qF1G13v2581HU6uVt5WJkOdXF4ysOQdccIaBDO05CPwqtzjtjgGDV41EuCWgXeT0qYJwPtZzx6OhYmeZiSlsYvC69ttxWRMFIefpX1tEZH0CaFVTV0ZaMpuwBdY7ElDxjM_XWuApFIyouqvNudKrMT0DztY1HrzOeqzzLBZgJbsrrTEnndq37TpXaFBjMfy0GCEt1RFNuGEkws1cQo4SBVt4Zbqdevmyo6kJ2rFMjOn6YdDVg-eYE08X1Qn-51fuHabgKy33_UnwvBATNpF0DgzjmaD7s9C0u8B1T9VIEdRnL6Fr9nVDaIV9aTcSozA-xdLQ7CLNgGLxkilw5Pm4tjo75-UcD-xMdvJ4APWMzk1R4VHa11JjPUzs_4aVLegwE3apExYxjMXO4wC0pyxUoY-1QvVloUEbckx2iJI91P16aKgvKl8IgxjZZdeYVjLwc6IRAHtF4Rv0PXSD6t_1IDSZydiu7s_mT0p3rRxF59bNC23O1QOtdKsYB6Bk1T9mdxG5ndTX_v2HqPZyhjuZQzmOJUH6GotkRPvcWldN-g0kKwA4dF2cYVA7el4RXge_bAAUbbas3l0pWuMNBJEfW78Kh7mmG9oJjj5Qipqzd7clWRhtkUyOikHPvIrJyLdNVdFNyfOeHesjWeaU
Cache-Control: no-cache
Postman-Token: c4b9178e-f64c-4146-ab7f-7453961e11f7
{
"group_id": "1",
"user_id": "1"
}
Now what I have tried is
Cleared my route cache
Named my login route which gave a MethodNotAllowedException
Can someone push me in the right direction ?

The solution was to add the Accept header with application/json

Related

Laravel JWT makes new request insted of sending status

I'm making an API in laravel with tymon/jwt-auth package. based on https://jwt-auth.readthedocs.io/en/develop/
Login works fine.
Sending GET request with right token works fine.
BUT when I send a GET request with incorrect/no token it redirects me to POST login (wants me to login) rather than send a 401
GET /api/reviews with no Auth:
The GET method is not supported for this route. Supported methods: POST.
api.php
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::group([
'prefix' => 'auth',
], function ($router) {
Route::post('login', [AuthController::class, 'login'])->name('login');
});
Route::get('reviews',[ReviewController::class, 'index']);
AuthController.php
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Get a JWT via given credentials.
*
* #return \Illuminate\Http\JsonResponse
*/
public function login()
{
$credentials = request(['email', 'password']);
if (!$token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return response()->json([
"user" => auth()->user(),
"access_token" => $token,
]);
}
/**
* Get the authenticated User.
*
* #return \Illuminate\Http\JsonResponse
*/
public function me()
{
return response()->json(auth()->user());
}
/**
* Log the user out (Invalidate the token).
*
* #return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth()->logout();
return response()->json(['message' => 'Successfully logged out']);
}
/**
* Refresh a token.
*
* #return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth()->refresh());
}
}
ReviewController.php
class ReviewController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
public function index()
{
$reviews = Review::all();
return response()->json([
'status' => 200,
'reviews' => $reviews
]);
}
}
Should I remove the auth middleware and do it manually?

How to test logout while using tymon/jwt-auth in Laravel?

I'm trying to make a logout test for my api with tymon/jwt-auth package. Here I have defined the api routes, controller, and a unit test.
In api.php:
Route::group(['middleware' => 'api', 'prefix' => 'auth'], function ($router) {
Route::post('login', 'AuthController#login');
Route::post('logout', 'AuthController#logout');
Route::post('refresh', 'AuthController#refresh');
Route::post('me', 'AuthController#me');
Route::post('me/profile', 'AuthController#profile');
});
In AuthController.php:
/**
* Log the user out (Invalidate the token).
*
* #return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth()->logout();
return response()->json(['message' => 'Successfully logged out']);
}
In tests/Unit/AuthenticationTest.php:
/**
* Test if user can login trough internal api.
*
* #return void
*/
public function testLogin()
{
$response = $this->post('api/auth/login', [
'email' => 'admin#xscriptconnect.com',
'password' => 'password'
]);
$response->assertStatus(200)
->assertJsonStructure(['access_token', 'token_type', 'expires_in']);
$this->assertAuthenticated('api');
}
/**
* Test if user can logout trough internal api.
*
* #return void
*/
public function testLogout()
{
$user = User::first();
$user = $this->actingAs($user, 'api');
$user->post('api/auth/logout')
->assertStatus(200)
->assertJsonStructure(['message']);
$this->assertUnauthenticatedAs($user, 'api');
}
The login test works fine but when it starts the logout test, the assertion fails. It shows me this error:
There was 1 failure:
1) Tests\Unit\AuthenticationTest::testLogout
Expected status code 200 but received 500.
Failed asserting that false is true.
And when I tested it using this method:
public function testLogout()
{
$user = User::first();
$this->actingAs($user, 'api');
$response = auth()->logout();
$response->assertStatus(200);
$response->assertJsonStructure(['message']);
}
I got this error:
There was 1 error:
1) Tests\Unit\AuthenticationTest::testLogout
Tymon\JWTAuth\Exceptions\JWTException: Token could not be parsed from the request
What is the proper way to test a logout trough this package? Please help.
According to the this comment in it's github page, I have found the solution for this problem. I changed my script like this and it works.
/**
* Test if user can logout trough internal api.
*
* #return void
*/
public function testLogout()
{
$user = User::first();
$token = \JWTAuth::fromUser($user);
$this->post('api/auth/logout?token=' . $token)
->assertStatus(200)
->assertJsonStructure(['message']);
$this->assertGuest('api');
}
Please feel free to post another answer regarding to this question if any. Thank you very much.
Override the method be() in your TestCase to set the authorization header when use actingAs() aka be() method
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
abstract class TestCase extends BaseTestCase
{
public function be(UserContract $user, $driver = null)
{
$token = auth()->fromUser($user);
return parent::be($user, $driver)->withHeader('Authorization', "Bearer {$token}");
}
}

Trying to get property of non-object. Error in laravel

I'm kinda new at laravel and need your help.
I have this IR model :
class IR extends Model
{
//
protected $fillable=['irnum','date','subject','cause','facts','as','at','rec','user_id'];
protected $casts=['user_id'=>'int'];
public function user()
{
return $this->belongsTo(user::class);
}
public static $rules =array (
'date'=>'required',
'status'=>'required|min:10',
'cause' => 'required|min:10',
'facts' => 'required|min:10',
'ir-as' => 'required|min:10',
'rec' => 'required|min:10',
'ir-at' => 'required|min:10',
);
}
and route:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
})->middleware('guest');
Route::resource('tasks','TaskController');
Route::get('ir',function ()
{
return View::make('tasks/ir');
});
Route::resource('irs','IRController');
Route::auth();
});
and this is my controller :
class IRController extends Controller
{
/**
* The task repository instance.
*
* #var TaskRepository
*/
protected $irs;
/**
* Create a new controller instance.
*
* #param TaskRepository $tasks
* #return void
*/
public function __construct(IRRepository $irs)
{
$this->middleware('auth');
$this->irs = $irs;
}
/**
* Display a list of all of the user's task.
*
* #param Request $request
* #return Response
*/
public function index(Request $request)
{
return view('tasks.ir',[
'irs' => $this->irs->forUser($request->user()),
]);
}
/**
* Create a new task.
*
* #param Request $request
* #return Response
*/
public function create()
{
return View::make('irs.create');
}
public function store(Request $request)
{
$request->user_id=Auth::user()->id;
$input =$request->all();
$validation=Validator::make($input, IR::$rules);
if($validation->passes())
{
IR::create($input);
return Redirect::route('tasks.ir');
}
return Redirect::route('tasks.ir')
->withInput()
->withErrors($validation)
->with('message','There were validation errors.');
}
/**
* Destroy the given task.
*
* #param Request $request
* #param Task $task
* #return Response
*/
public function destroy(Request $request, IR $irs)
{
}
}
I really dont know what causes to throw this error.
Error throws when i add Incident report.
Pls help.
New at laravel
You're saying you get an error when you're trying to add an incident report or IR, so I assume problem is in a store() action.
I can see only one potential candidate for this error in a store() action:
Auth::user()->id;
Add dd(Auth::user()); before this clause and if it will output null, use check() method, which checks if any user is authenticated:
if (Auth::check()) {
Auth::user->id;
}

Laravel Form Request issue with validation

I am trying to use form request in my REST API built using laravel 5.2. My controller is
public function save(SbcEntityFormRequest $request)
{
$requestData = Input::all();
try {
list($success, $message) = $this->sbcService->saveSbcEntity($requestData);
if ($success) {
return $this->successJsonResponse($request, ['id' => $message]);
}
return $this->errorJsonResponse($request, Response::HTTP_BAD_REQUEST, [$message]);
} catch (Exception $e) {
AppLog::write($e);
$message = [config('messages.save_failed')];
return $this->errorJsonResponse($request, Response::HTTP_BAD_REQUEST, $message);
}
}
My form request is
namespace App\Http\Requests;
use Illuminate\Http\Request;
class SbcEntityFormRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'logo' => 'Required',
'bio' => 'Required|Max:150'
];
}
}
My validation rules are never called. I put a die statement in authorize() function and it is neither called. When I printed $request->all() in the controller it shows empty array. Any Idea on what is wrong here?

Laravel 5 middleware auth always fails and redirects to login

I am authenticating my user and redirecting him to dashboard if credentials are correct. I want to secure the dashboard route and added middleware auth, but now it always redirects to login page.
Routes.php
Route::get('login', array('uses' => 'HomeController#showLogin'));
Route::post('login', array('uses' => 'HomeController#doLogin'));
Route::get('logout', array('uses' => 'HomeController#doLogout'));
Route::group(['middleware' => 'auth'], function() {
Route::get('/', function () {
return view('dashboard');
});
Route::get('dashboard', function () {
return view('dashboard');
});
});
HomeController.php
public function showLogin(){
return View::make('login');
}
public function doLogin(Request $request){
$rules = array(
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make($request::all(), $rules);
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator)
->withRequest($request::except('password'));
}
else {
$userdata = array(
'email' => $request::get('email'),
'password' => $request::get('password')
/*'password' => Hash::make($request::get('password'))*/
);
if (Auth::attempt($userdata)) {
$userid = Auth::id();
return redirect()->intended('/');
} else {
return Redirect::to('login');
}
}
}
public function doLogout()
{
Auth::logout();
return Redirect::to('login');
}
Middleware Authenticate.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
Middleware RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/');
}
return $next($request);
}
I am not sure why your code is not working, but you can try replace:
if (Auth::attempt($userdata)) {
$userid = Auth::id();
return redirect()->intended('/');
}
with:
if (Auth::attempt($userdata)) {
$userid = Auth::id();
return redirect('dashboard');
}
From the API Docs of intended method:
Create a new redirect response to the previously intended location.
is giving some error to you as it is going back to the previous location and not to the next location.
UPDATE 1:
I would have gone with the following approach.
Make your own middleware called UserAlreadyLoggedIn
php artisan make:middleware UserAlreadyLoggedIn
Open UserAlreadyLoggedIn.php and update handle method with the below code:
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if(auth()->check()) {
return $next($request);
}
return redirect('login');
}
Register it in app/Http/Kernel.php file inside $routeMiddleware array:
$routeMiddleware = [
'user_already_logged_in' => \App\Http\Middleware\UserAlreadyLoggedIn::class,
];
Separate the already logged in user in controller by making UserSessionsController
php artisan make:controller UserSessionsController --plain
Inside UserSessionsController place the __constructor method:
/**
* Check if the user is already logged in.
*
* #return void
*/
public function __construct()
{
$this->middleware('user_already_logged_in');
}
routes.php
Route::get('login', 'HomeController#showLogin');
Route::post('login', 'HomeController#doLogin');
Route::get('logout', 'HomeController#doLogout');
// Replace the dashboard code inside the dashboard method..
Route::get('dashboard', 'UserSessionsController#dashboard');
Again I would have created a middleware called UserIsAGuest and would have replaced the if block inside the handle method:
if(auth()->guest()) {
return $next($request);
}
return redirect('dashboard');
And then inside the HomeController's __construct method:
/**
* Check if the user is already logged in.
*
* #return void
*/
public function __construct()
{
// Register the middleware in Kernel.php file
$this->middleware('user_is_guest');
}
Hope this helps you out. Happy Coding. Cheers.
Sorry for being late to the party, but after a long search for me, I had to change my middleware from ['middleware' => 'auth] to ['middleware' => 'auth:web']
I hope this helps out

Resources