PHP code example of whyounes / laravel-passwordless-auth

1. Go to this page and download the library: Download whyounes/laravel-passwordless-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

whyounes / laravel-passwordless-auth example snippets


php artisan vandor:publish

php artisan migrate

// config/app.php

// ...
'providers' => [
    // ...
    Whyounes\Passwordless\Providers\PasswordlessProvider::class,
};

// app/User.php

class User extends Authenticatable
{
    use Whyounes\Passwordless\Traits\Passwordless;

    // ...
}

// app/User.php

class User extends Authenticatable
{
    use Whyounes\Passwordless\Traits\Passwordless;

    // ...
    
    protected function getIdentifierKey()
    {
        return 'email';
    }
}

// config/passwordless.php

return [
    'expire_in' => 15, // Minutes
    'empty_tokens_after_login' => true // Empty user tokens after login
];

// routes/web.php

Route::post('/login/direct', function() {
    return view('login.direct');
});

// 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);
    });
});

// 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");
    }
});

// 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");
    }
});