Package Data | |
---|---|
Maintainer Username: | Whyounes |
Maintainer Contact: | younes.rafie@gmail.com (Rafie Younes) |
Package Create Date: | 2016-11-21 |
Package Last Update: | 2016-12-11 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-18 03:03:32 |
Package Statistics | |
---|---|
Total Downloads: | 39 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 6 |
Total Watchers: | 3 |
Total Forks: | 1 |
Total Open Issues: | 1 |
Passwordless authentication for Laravel 5
Add the package to your project using Composer:
composer require whyounes/laravel-passwordless-auth
Publish package assets:
php artisan vandor:publish
Run the migration to create the tokens table:
php artisan migrate
Add it to you providers list:
// config/app.php
// ...
'providers' => [
// ...
Whyounes\Passwordless\Providers\PasswordlessProvider::class,
};
Add the Passwordless
trait to your user model:
// app/User.php
class User extends Authenticatable
{
use Whyounes\Passwordless\Traits\Passwordless;
// ...
}
If you don't want to use the user email along with the token, you can change it by overriding the following method:
// app/User.php
class User extends Authenticatable
{
use Whyounes\Passwordless\Traits\Passwordless;
// ...
protected function getIdentifierKey()
{
return 'email';
}
}
You can change the expiration time inside the config/passwordless.php
file:
// config/passwordless.php
return [
'expire_in' => 15, // Minutes
'empty_tokens_after_login' => true // Empty user tokens after login
];
You can set the empty_tokens_after_login
config to false if you don't want to delete unused tokens from DB.
Display the login form for user to type the email:
// routes/web.php
Route::post('/login/direct', function() {
return view('login.direct');
});
Catch the form submission:
// routes/web.php
Route::post('/login/direct', function(Request $request) {
// send link to user mail
$user = App\User::where('email', $request->get('email'))->first();
if (!$user) {
return redirect()->back(404)->with('error', 'User not found');
}
// generate token and save it
$token = $user->generateToken(true);
// send email to user
\Mail::send("mails.login", ['token' => $token], function($message) use($token) {
$message->to($token->user->email);
});
});
Catch the login link request:
// routes/web.php
Route::get('/login/{token}', function(Request $request, $token) {
$user = App\User::where('email', $request->get('email'))->first();
if (!$user) {
dd('User not found');
}
if($user->isValidToken($token))
{
// Login user
Auth::login($user);
} else {
dd("Invalid token");
}
});
Or, if you like working with exceptions:
// routes/web.php
Route::get('/login/{token}', function(Request $request, $token) {
try {
$user = App\User::where('email', $request->get('email'))->firstOrFail();
$user->validateToken($token);
Auth::login($user);
} catch(Illuminate\Database\Eloquent\ModelNotFoundException $ex) {
dd('User not found');
} catch(Whyounes\Passwordless\Exceptions\InvalidTokenException $ex) {
dd("Invalid token");
}
});