Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I make sure the user accessing a backend rendered frontend route is authenticated?

I'm using Laravel and Angular to write a web app.

In the front end Laravel is used to create the basic template, but otherwise controlled by Angular. In the back end laravel is used to create a restful API.

I have a few routes like this:

Route::group(['domain' => 'domain.com'], function() {

    Route::get('/', ['as' => 'home', function () {
        return view('homepage');
    }]);

    Route::get('/login', ['as' => 'login', function () {
        return view('login');
    }]);

    //users should be authenticated before accessing this page
    Route::get('/dashboard', ['as' => 'dashboard', function () {
        return view('dashboard');
    }]); 

});

Route::group(['domain' => 'api.domain.com', 'middleware' => ['oauth']], function() {
    Route::post('/post/create', ['uses' => 'PostController@store']);
    Route::get('/post/{id}', ['uses' => 'PostController@show']);

     //other API endpoints
     // ...
});

I want to make sure my domain.com/dashboard URL is only accessed by authenticated users.

In my backend I have OAuth implemented for my API routes which makes sure the user accessing those routes are authentic. Laravel's Auth::once() is used by the OAuth library to make sure the user credentials are correct then generates an access_token. Since Auth::once() is a "stateless" function no session or cookies are utilized and I cannot use Auth::check() to make sure a user is authenticated before the dashboard page is rendered.

How should I go about checking to see if the user trying to access domain.com/dashboard is authenticated? Should I send the access_token in the header when I forward the user from /login to /dashboard? Or should I implement Laravel's a session/cookie based authentication?


EDIT: As per this: Adding http headers to window.location.href in Angular app I cannot forward the user to the dashboard page with an Authorization header.

In order to reuse my API for my mobile apps I STRONGLY prefer to use some sort of token based authentication.

like image 287
Zaki Aziz Avatar asked Mar 10 '16 02:03

Zaki Aziz


People also ask

How do you authenticate frontend with backend?

When the frontend needs to authenticate the user, it calls an API endpoint ( /api/login ) on the backend to start the login handshake. The backend uses OpenID connect with Auth0 to authenticate the user and getting the id, access, and refresh tokens. The backend stores the user's tokens in a cache.

Is authentication done on front end or backend?

The actual beefy parts of the authentication process itself (locating the user, salting/hashing, password comparison, etc) should be done on the back-end. What is front-end development for a web page?

What is backend authentication?

Authentication in Review Board is handled by classes called Authentication Backends. They perform the tasks of looking up users from some database or server, authenticating against it given user credentials, and creating local representations of the users in Review Board's database.


2 Answers

I would advise to use JWT (JSON Web Tokens) to control for authentication.

I think there are several tutorials for their use with Lavarel and AngularJS. I'm more into Python and I use Flask, but the followings look interesting :

  • Simple AngularJS Authentication with JWT : the AngularJS configuration
  • Token-Based Authentication for AngularJS and Laravel Apps : the connection with Laravel
  • JSON Web Token Tutorial: An Example in Laravel and AngularJS
like image 122
Pierre Cordier Avatar answered Sep 28 '22 07:09

Pierre Cordier


Pierre was right in suggesting JWT for your token based auth.

When the user successfully logs in, before you finish the request, you can create a JWT and pass that back to the client. You can store it on the client (localStorage, sessionStorage) if you want. Then on subsequent requests, put the JWT inside of your Authorization header. You can then check for this header in your middleware and prevent access to your API routes if the token is valid. You can also use that token on the client and prevent Angular from switching routes if the token doesn't exists or isn't valid.

Now if you are trying to prevent the user from accessing the page entirely on initial load (Opens browser, goes straight to domain.com/dashboard), then I believe that is impossible since there is no way to get information about the client without first loading some code on the page.

like image 36
Mr_Antivius Avatar answered Sep 28 '22 08:09

Mr_Antivius