Jul 30, 2017
0
Level 1
How to create a login/logout functionality in lumen
I followed these tutorials - one and two to create an authentication system for my app. However, they don't exactly explain how to create a login and a logout system for users, simply maintaining state using auth middleware.
Here is my code for routes/web.php:
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$app->get('/', function () use ($app) {
return view('welcome');
});
$app->post('login/', ['middleware' => 'auth', 'uses' => 'UsersController@authenticate']);
$app->post('todo/','TodoController@store');
$app->get('todo/', 'TodoController@index');
$app->get('todo/{id}/', 'TodoController@show');
$app->put('todo/{id}/', 'TodoController@update');
$app->delete('todo/{id}/', 'TodoController@destroy');
?>
Here I call the authenticate method within UsersController.php to login the user ie. create the api key for them. However if I call this without the 'middleware' => 'auth', part it throws this error:
[2017-07-30 16:11:39] lumen.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Parse error: syntax error, unexpected ' public' (T_STRING), expecting function (T_FUNCTION) in /home/vineet/Desktop/random/sharpnotes/app/Http/Controllers/UsersController.php:19
Stack trace:
#0 /home/vineet/Desktop/random/sharpnotes/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile('/home/vineet/De...')
#1 [internal function]: Composer\Autoload\ClassLoader->loadClass('App\\Http\\Contro...')
#2 [internal function]: spl_autoload_call('App\\Http\\Contro...')
#3 /home/vineet/Desktop/random/sharpnotes/vendor/illuminate/container/Container.php(729): ReflectionClass->__construct('App\\Http\\Contro...')
#4 /home/vineet/Desktop/random/sharpnotes/vendor/illuminate/container/Container.php(608): Illuminate\Container\Container->build('App\\Http\\Contro...')
#5 /home/vineet/Desktop/random/sharpnotes/vendor/illuminate/container/Container.php(575): Illuminate\Container\Container->resolve('App\\Http\\Contro...')
#6 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Application.php(208): Illuminate\Container\Container->make('App\\Http\\Contro...')
#7 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(676): Laravel\Lumen\Application->make('App\\Http\\Contro...')
#8 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(643): Laravel\Lumen\Application->callControllerAction(Array)
#9 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(628): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#10 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(528): Laravel\Lumen\Application->handleFoundRoute(Array)
#11 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(781): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#12 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#13 /home/vineet/Desktop/random/sharpnotes/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(475): Laravel\Lumen\Application->dispatch(NULL)
#14 /home/vineet/Desktop/random/sharpnotes/public/index.php(28): Laravel\Lumen\Application->run()
#15 {main}
And understandably, using my middleware redirects me to Authenticate.php which throws a 401 Unauthorized error.
Here is my UsersController.php for reference:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Users;
use Log;
class UsersController extends Controller
{
public function __construct()
{
// $this->middleware('auth', ['only' =>
// ['authenticate']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function authenticate(Request $request)
{
Log::info("hiiidsfoiasjdfisa");
$this->validate($request, [
'email' => 'required',
'password' => 'required'
]);
$user = Users::where('email', $request->input('email'))->first();
Log::info($user->password);
Log::info($user->email);
if(Hash::check($request->input('password'), $user->password)){
$apikey = base64_encode(str_random(40));
Users::where('email', $request->input('email'))->update(['api_key' => "$apikey"]);;
return response()->json(['status' => 'success','api_key' => $apikey]);
}else{
return response()->json(['status' => 'fail'],401);
}
}
}
?>
How do I create a login/logout with this new api_token system in lumen?
Please or to participate in this conversation.