Support the ongoing development of Laravel.io →
Authentication Security Session
Last updated 2 years ago.
0

you could look into oauth2. dingo api can integrate with oauth2 server.

Last updated 2 years ago.
0

The solution I use is to POST to my REST auth controller, and return a api key (token), that is saved in the user's profile. The mobile app saves this token in storage. Then in any call in the mobile app send the token in the request as a GET parameter or header parameter. All other methods in the app have a Route filter using something that looks like this:

Route::filter('api', function() {
	// Fetch a user record based on api key
	$user = User::where('api_key', '=', Input::get('api_key'))
				->take(1)
				->get();
	if ($user->count() > 0) {
		Auth::onceUsingId($user[0]->id); // Authorize the user for this one request
	} else {
		return Response::view('errors.404', array(), 404)->header('Content-Type', 'application/json');
	}
});

This will authorize the user for the request to the API, and allows you to use the syntax in the Auth library for whatever purpose you need it for. You can usually set a session length type key in the app, and it you like, you can compare the current time and the session length to force them to login again.

Last updated 2 years ago.
0

The solution I use is to POST to my REST auth controller, and return a api key (token), that is saved in the user's profile. The mobile app saves this token in storage. Then in any call in the mobile app send the token in the request as a GET parameter or header parameter. All other methods in the app have a Route filter using something that looks like this:

Route::filter('api', function() {
	// Fetch a user record based on api key
	$user = User::where('api_key', '=', Input::get('api_key'))
				->take(1)
				->get();
	if ($user->count() > 0) {
		Auth::onceUsingId($user[0]->id); // Authorize the user for this one request
	} else {
		return Response::view('errors.404', array(), 404)->header('Content-Type', 'application/json');
	}
});

This will authorize the user for the request to the API, and allows you to use the syntax in the Auth library for whatever purpose you need it for. You can usually set a session length type key in the app, and it you like, you can compare the current time and the session length to force them to login again.

Last updated 2 years ago.
0

Thanks pickupman and labox.

How do you generate the token and what's the time to live of this token? It change?

pickupman said:

The solution I use is to POST to my REST auth controller, and return a api key (token), that is saved in the user's profile. The mobile app saves this token in storage. Then in any call in the mobile app send the token in the request as a GET parameter or header parameter. All other methods in the app have a Route filter using something that looks like this:

Route::filter('api', function() {
  // Fetch a user record based on api key
  $user = User::where('api_key', '=', Input::get('api_key'))
  			->take(1)
  			->get();
  if ($user->count() > 0) {
  	Auth::onceUsingId($user[0]->id); // Authorize the user for this one request
  } else {
  	return Response::view('errors.404', array(), 404)->header('Content-Type', 'application/json');
  }
});

This will authorize the user for the request to the API, and allows you to use the syntax in the Auth library for whatever purpose you need it for. You can usually set a session length type key in the app, and it you like, you can compare the current time and the session length to force them to login again.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

oscarvady oscarvady Joined 29 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.