Question

I am trying to follow David Mosher's tutorial on going end-to-end with angular JS on youtube: http://www.youtube.com/watch?v=hqAyiqUs93c.

Everything was going fine until I attempted to route the /auth/login and auth/logout urls to the Authentication Service, as seen in the video at about 14:30. When I attempt to login, I receive a 404 Not Found Error. I have tried messing around with the route's URL, but to no avail. I am running locally on MAMP with MySQL and Laravel.

Here's my code for routes.php

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

Route::post('/auth/login/', 'AuthController@login');
Route::get('/auth/logout/', 'AuthController@logout');

and my code for AuthController

<?php

class AuthController extends BaseController {
  public function login()
  {
    if(Auth::attempt(array('email' => Input::json('email'), 'password' => Input::json('password'))))
    {
      return Response::json(Auth::user());
    } else {
      return Response::json(array('flash' => 'Invalid username or password'), 500);
    }
  }

  public function logout()
  {
    Auth::logout();
    return Response::json(array('flash' => 'Logged Out!'));
  }

}

lastly, code for authentication service

angular.module("epicApp")
    .factory('AuthenticationService', function($http, $location){
        return{
            login: function(credentials){
                return $http.post("/auth/login/", credentials);
            },
            logout: function(){
                return $http.get("/auth/logout/");
            }
        }
    })
Was it helpful?

Solution

The only one problem is you have to replace all /auth/login/ to auth/login and /auth/logout/ to auth/logout in your route and angular script.

So your route should be,

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

Route::post('auth/login', 'AuthController@login');
Route::get('auth/logout', 'AuthController@logout');

And js,

angular.module("epicApp")
    .factory('AuthenticationService', function($http, $location){
        return{
            login: function(credentials){
                return $http.post("auth/login", credentials);
            },
            logout: function(){
                return $http.get("auth/logout");
            }
        }
    })

OTHER TIPS

I don't know if this question still relevant, but you will get a 404 with any HTTP URL if inside the public folder the index file is not a .php. You could just rename the index.html of your angular app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top