r/angularjs • • Oct 13 '14

[General] AngularJS 1.3 has been released

https://github.com/angular/angular.js/blob/v1.3.0/CHANGELOG.md
127 Upvotes

45 comments sorted by

View all comments

Show parent comments

3

u/8Bytes Oct 14 '14 edited Oct 14 '14

It works out really well, I use angular front laravel back for all my projects these days (until I find time to learn django). Laravel does a lot of obnoxious things for you out of the box, like user auth, password reset, input sanitation, bcrypt pass hashing, ect. And it has a very nice SQL orm and route controller that makes designing rest APIs extremely easy.

Then using $resource I link angular to the rest api, and the project is live. I highly recommend it.

2

u/YodaLoL Oct 17 '14

I can recommend restangular over $resource.

Also, do you dev REST APIs with Laravel, do you have any tips/recommendations regarding that?

2

u/8Bytes Oct 17 '14

I've heard great thing about restangular, but I haven't run into a situation where in need it just yet, resource allows nested objects like /user/id/posts/id, which is most of my use cases.

Rest APIs are exceedingly simply in laravel. You have a routes file called routes.php. In that you define a route:

Route::resource ('user', 'UserController');

This sets up around 7 routes for you, read he doc for details on what they all are. They will be routed to the code in the UserController.

Then make the UserController. There is already a User model for you to use, but for any noun other then user, you'll have to make your own.

In the UserController, use the User model to implement the SQL calls.

function index() 
{
    $users = User::all();
    return Response:: json([
        'error': false,
        'users': $users]);
}

Now you have an api call that handles an angular query request at /user.

1

u/YodaLoL Oct 17 '14

I'm familiar with Laravel, just never made a REST API with it. I've done it in Rails, which functions in almost the same way as you describe. Thanks for the answer :).

How do you auth users? Do you use HTTP Headers, cookies, something else? Is it easy to deal with actual auth in the code?

2

u/8Bytes Oct 17 '14 edited Oct 17 '14

I use http headers. The client stores their auth string as a cookie, and places it in the header of each request.

Then in laravel I simply check the header in the filter with:

Route::filter('auth.token', function($route, $request)
{
$payload = $request->header('X-Auth-Token');

$user =  DB::table('users')->where('remember_token',$payload)->first();

if(!$payload || !$user) {

    $response = Response::json([
        'error' => true,
        'message' => 'Not authenticated',
        'code' => 401],
        401
    );

    $response->header('Content-Type', 'application/json');
return $response;
}

});

Then i group the routes that need auth behind:

//public routes
...
Route::group(array('before' => 'auth.token'), function() {
    //protected routes
    ...
});

** How do you auth users in rails?