1
0
forked from metin2/web

Added authentication

This commit is contained in:
2023-07-28 19:57:16 +03:00
parent 67af4ef427
commit c26d8e4642
11 changed files with 181 additions and 103 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace App\Hashing;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Hashing\AbstractHasher;
class MySQLHasher extends AbstractHasher implements Hasher
{
public function make($value, array $options = []): string
{
return '*' . mb_strtoupper(sha1(sha1($value, true)));
}
public function check($value, $hashedValue, array $options = []): bool
{
return $this->make($value) === $hashedValue;
}
public function needsRehash($hashedValue, array $options = []): bool
{
return false;
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\View\View;
class LoginController extends Controller
{
/**
* Handle an authentication attempt.
*/
public function login(Request $request): RedirectResponse
{
$validator = Validator::make($request->all(), [
'login' => ['required'],
'password' => ['required'],
]);
if ($validator->fails())
return redirect('user/login')->withErrors($validator)->withInput();
$credentials = $validator->validated();
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
$user = Auth::user();
$user->ip = $request->ip();
$user->saveOrFail();
return redirect()->intended('user/administration');
}
return redirect('user/login')->withErrors([
'login' => 'The provided credentials do not match our records.',
])->onlyInput('login');
}
/**
* Log the user out of the application.
*/
public function logout(Request $request): View
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return view('user/logout');
}
}

View File

@ -4,23 +4,40 @@ namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Foundation\Auth\User;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
class Account extends User
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'account';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'account';
const UPDATED_AT = null;
const CREATED_AT = 'create_time';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'login',
'password',
'email',
];
/**
@ -30,7 +47,8 @@ class User extends Authenticatable
*/
protected $hidden = [
'password',
'remember_token',
'social_id',
'securitycode'
];
/**

View File

@ -3,7 +3,11 @@
namespace App\Providers;
// use Illuminate\Support\Facades\Gate;
use App\Hashing\MySQLHasher;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
class AuthServiceProvider extends ServiceProvider
{
@ -21,6 +25,9 @@ class AuthServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
Auth::provider('legacy', function (Application $app, array $config) {
$mysqlHasher = new MySQLHasher();
return new EloquentUserProvider($mysqlHasher, $config['model']);
});
}
}