New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Password Less Auth #1

Open
wants to merge 7 commits into
base: master
from
Copy path View file
@@ -4,6 +4,7 @@
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Routing\Exceptions\InvalidSignatureException;
class Handler extends ExceptionHandler
{
@@ -46,6 +47,10 @@ public function report(Exception $exception)
*/
public function render($request, Exception $exception)
{
if (is_a($exception, InvalidSignatureException::class)) {
return response()->view('_signature-expired');
}
return parent::render($request, $exception);
}
}

This file was deleted.

Oops, something went wrong.
@@ -2,23 +2,12 @@
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Auth\Traits;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
use Traits\PasswordLessAuth;
/**
* Where to redirect users after login.
@@ -2,11 +2,12 @@
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
@@ -19,7 +20,7 @@ class RegisterController extends Controller
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
*/
use RegistersUsers;
@@ -51,7 +52,6 @@ protected function validator(array $data)
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
@@ -66,7 +66,22 @@ protected function create(array $data)
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
return redirect()->route('login')
->with(['success' => 'Success! your account is registered.']);
}
}

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,122 @@
<?php
namespace App\Http\Controllers\Auth\Traits;
use App\LoginAttempt;
use App\Notifications\NewLoginAttempt;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
trait PasswordLessAuth
{
use AuthenticatesUsers;
/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$messages = ['exists' => trans('auth.exists')];
$this->validate($request, [
$this->username() => 'required|email|exists:users',
], $messages);
}
/**
* Handle a login attempt request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function attempt(Request $request)
{
$this->incrementLoginAttempts($request);
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$this->validateLogin($request);
if ($this->createLoginAttempt($request)) {
return $this->sendAttemptResponse($request);
}
return $this->sendFailedLoginResponse($request);
}
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function login($token, Request $request)
{
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($token, $request)) {
return $this->sendLoginResponse($request);
}
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
/**
* Attempt to log the user into the application.
*
* @param string $token
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function attemptLogin($token, Request $request)
{
$user = LoginAttempt::userFromToken($token);
if (is_object($user)) {
return $this->guard()->login($user);
}
}
/**
* Attempt to log the user into the application.
*
* @param \Illuminate\Http\Request $request
* @return \App\LoginAttempt
*/
protected function createLoginAttempt(Request $request)
{
$authorize = LoginAttempt::create([
'email' => $request->input($this->username()),
'token' => str_random(40) . time(),
]);
$authorize->notify(new NewLoginAttempt($authorize));
return $authorize;
}
/**
* @param $request
*/
public function sendAttemptResponse($request)
{
return \View::make('auth._link-sent');
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
Copy path View file
@@ -59,5 +59,6 @@ class Kernel extends HttpKernel
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];
}
Copy path View file
@@ -0,0 +1,46 @@
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class LoginAttempt extends Model
{
use Notifiable;
/**
* @var string
*/
protected $table = 'login_attempts';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email', 'token',
];
/**
* @return mixed
*/
public function user()
{
return $this->hasOne(User::class, 'email', 'email');
}
/**
* @param $token
*/
public static function userFromToken($token)
{
$query = self::where('token', $token)
->where('created_at', '>', Carbon::parse('-15 minutes'))
->first();
return $query->user ?? null;
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.