I have recently coded my own authentication system and wondered if there was any way I could make it better? I coded it with Laravel 5.4. getView is called on Route::get
and onPost is called on Route::post
use Auth;
use App\User;
use App\Http\Controllers\Controller;
use Validator;
use Redirect;
use Illuminate\Http\Request;
use App\Database\Frontend\User\Player;
use App\Database\Frontend\WebsiteLogin;
class LoginController extends Controller
{
public function getView()
{
return view('frontend.login');
}
public function onPost(Request $request)
{
$validator = Validator::make($request->all(), [
'mail' => 'required|email|exists:users',
'password' => 'required'
]);
if ( $validator->fails()) {
return Redirect::to('/login')->withErrors($validator->messages());
}
else {
if (!Auth::attempt(['mail' => $request->input('mail'), 'password' => $request->input('password')])) {
$this->addNewWebsiteLogin($request, Player::where('mail', $request->input('mail'))->pluck('id')->first(), "0");
return Redirect::to('/login')->withMessage('Email and password do not match')->withColor('danger');
}
else {
$this->addNewWebsiteLogin($request, Auth::user()->id, "1");
$user = Auth::user();
$user->last_online = time();
$user->save();
if (config('frontend.government_only') && (Auth::Guest() || Auth::user()->roleplay->government_id == 0)) {
Auth::logout();
return Redirect::to('/login')->withMessage(config('frontend.site_name') . ' is only open to government individuals at this moment, too bad.')->withColor('danger');
}
return Redirect::to('/home')->withMessage('Welcome back!');
}
}
}
private function addNewWebsiteLogin(Request $request, $userId, $status) {
$websiteLogin = new WebsiteLogin;
$websiteLogin->user_id = $userId;
$websiteLogin->request_ip = $request->ip();
$websiteLogin->request_system = 'TODO';
$websiteLogin->request_browser = 'TODO';
$websiteLogin->login_status = $status;
$websiteLogin->save();
}
}