Any Idea how to do Session based authentication in laravel 5.3 using vue js and vue router -
my biggest problem authentication
my head aches because im using api fetch , send data server using vue
so ive tried doing login thing auth middleware kept saying unauthorized logged in
so whats problem authentication , how can implement session based authentication?
if you're using laravel api, you'll want token-based authentication. main idea use 'api' guard (which uses tokenguard class), instead of default 'web' guard (you can change in congif/auth.php
). way laravel bearer token in authorization header in each request.
this decent walk through started, though i'm sure there many other blog posts , tutorials out there.
edit:
not sure if helpful, here's example using automated test. in modelfactory, have user model api_token
attribute set random (and unique) string of 60 characters. simple test make sure can retrieve list of users:
/** @test */ public function user_index_endpoint_returns_list_of_users() { $user = factory(user::class)->create(); $this->get('/users', array( 'accept' => 'application/json', 'authorization' => 'bearer ' . $user->api_token )) ->assertresponsestatus(200) ->seejsonstructure([ 'users' ]); }
at top of config/auth.php
file, i've set default guard 'api':
'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ],
finally, in routes file i've set users route use 'auth' middleware.
route::group(['middleware' => 'auth'], function() { route::get('/users', 'userscontroller@index'); });
because i've defined 'api' middleware default, behind scenes laravel use tokenguard class try authenticate request. so, bearer token in request , compare against users table.
Comments
Post a Comment