Temporary Auth Updates L5

Posted: 2014-10-12 14:20:28

Auth updates

First make sure your composer update has the latest Laravel and check that there are not relevant fixes already via laravel/docs master laravel/framework master or laravel/laravel dev

Moved evn files to the new format and only 1 is needed now having the

APP_ENV='local' 

setting for the environment.

Then update the path in the AuthController to go to auth/login

#app/Http/Controllers/Auth/AuthController

    public function login(LoginRequest $request)
    {
        if ($this->auth->attempt($request->only('email', 'password')))
        {
            return redirect('/');
        }

        return redirect('/auth/login')->withErrors([
            'email' => 'The credentials you entered did not match our records. Try again?',
        ]);
    }

Here is a good article on using annotations which are needed now in the controller.

http://mattstauffer.co/blog/laravel-5.0-route-annotations

For example I remove api/v1/projects from routes.php (which is key it can not be in both)

Then in the ProjectsController.php I added this to the top of the class

/**
 * @Middleware("BehatEditor\Http\Middleware\AuthMiddleware")
 * @Before("auth")
 */
class ProjectsController extends BaseController {

and in the index function


/** * @Get("api/v1/projects", as="api.v1.projects") * @return mixed */ public function index() { try { $results = $this->service->getAllProjectsForThisUser(); /** * @TODO move Response to a class to not have it tightly coupled here */ return Response::json($this->responseServices->respond($results, "Loaded Projects"), 200); } catch(\Exception $e) { return Response::json($this->responseServices->respond($e->getMessage(), "Error Getting Project Failed"), 422); } }

So the route is now api/v1/projects using Get and the route name for ease of use is api.v1.projects

Then I ran

php artisan route:scan 

to get this file made for me

storage/framework/routes.scanned.php

Inside we see

<?php 

$router->get('auth/login', ['uses' => 'App\Http\Controllers\Auth\AuthController@showLoginForm', 'domain' => NULL, 'as' => NULL, 'middleware' => array (
  0 => 'csrf',
  1 => 'guest',
), 'where' => array (
)]);
$router->post('auth/login', ['uses' => 'App\Http\Controllers\Auth\AuthController@login', 'domain' => NULL, 'as' => NULL, 'middleware' => array (
  0 => 'csrf',
  1 => 'guest',
), 'where' => array (
)]);
$router->get('auth/logout', ['uses' => 'App\Http\Controllers\Auth\AuthController@logout', 'domain' => NULL, 'as' => NULL, 'middleware' => array (
  0 => 'csrf',
  1 => 'guest',
), 'where' => array (
)]);
$router->get('api/v1/sites/reports', ['uses' => 'App\Http\Controllers\DashController@getDash', 'domain' => NULL, 'as' => 'sites.reports', 'middleware' => array (
  0 => 'App\\Http\\Middleware\\AuthMiddleware',
), 'where' => array (
)]);
$router->get('api/v1/projects', ['uses' => 'App\Http\Controllers\ProjectsController@index', 'domain' => NULL, 'as' => 'api.v1.projects', 'middleware' => array (
  0 => 'App\\Http\\Middleware\\AuthMiddleware',
), 'where' => array (
)]);
$router->get('sites', ['uses' => 'App\Http\Controllers\SitesController@index', 'domain' => NULL, 'as' => 'sites.index', 'middleware' => array (
  0 => 'App\\Http\\Middleware\\AuthMiddleware',
), 'where' => array (
)]);

All the App namespaces can really be your App or the default Laravel app files.

This file is auto generated from that command so do not edit it.

The one we just made is

$router->get('api/v1/projects', ['uses' => 'App\Http\Controllers\ProjectsController@index', 'domain' => NULL, 'as' => 'api.v1.projects', 'middleware' => array (
  0 => 'App\\Http\\Middleware\\AuthMiddleware',
), 'where' => array (
)]);