vendredi 7 septembre 2018

Laravel send web routes to index AngularJS

I have a new Laravel AngularJS app, which is working by using Lavavel to act as an API resource and handling all the interface in Angular. When I navigate away from first page to a page such as '/users' it works, however if I refresh the URL it runs through Laravel and outputs this message to the browser:

{"errors":{"status":401,"message":"Unauthenticated"}}

I know that this message is the result of the exceptions handler, but I following advice online I've not found a solution to get the web route to successfully load the URL into Angular :/

Here are the relevant files content...

Exceptions/Handler.php:

...    
public function render($request, Exception $exception)
{
    //return parent::render($request, $exception);
    return response()->json(
        [
            'errors' => [
                'status' => 401,
                'message' => 'Unauthenticated',
            ]
        ], 401
    );
}
...

routes/web.php:

Route::get('/', function () {
    return view('index');
});

routes/api.php

Route::resource('authenticate', 'AuthenticateController');
Route::post('authenticate', 'AuthenticateController@authenticate');
Route::post('logout', 'AuthenticateController@logout');
Route::post('refresh', 'AuthenticateController@refresh');
Route::post('me', 'AuthenticateController@me');

app.js:

require('./bootstrap');
require('angular');
require('angular-ui-router');
require('satellizer');

var app = angular
   .module('APP', ['ui.router', 'satellizer'])
   .config(function($stateProvider, $urlRouterProvider, $authProvider, $locationProvider) {

        // Satellizer configuration that specifies which API
        // route the JWT should be retrieved from
        $authProvider.loginUrl = '/app/api/authenticate';

        // Redirect to the auth state if any other states
        // are requested other than users
        $urlRouterProvider.otherwise('/');

        $stateProvider
            .state('auth', {
                url: '/',
                templateUrl: '/app/views/auth/login.php',
                controller: 'AuthController'
            })
            .state('users', {
                url: '/users',
                views:{
                    '' :{
                        templateUrl: '/app/views/users.php',
                        controller: 'UserController'
                    },
                    'nav@':{
                        templateUrl: '/app/views/components/nav.php'
                    }
                },
                children: [
                    {
                        url: '/list',
                        templateUrl: '/app/views/users.list.php'
                    }
                ]
            });

        $locationProvider.html5Mode(true);

    });

I have initiated html5Mode to give pretty urls and I have set

<base href="/app/">

in the HTML head (not that that should affect the handling of URLs in Laravel).



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2oOqqHJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire