From c26d8e464235ac70e4c45c834fe5c56cdc1fe8ab Mon Sep 17 00:00:00 2001 From: Exynox Date: Fri, 28 Jul 2023 19:57:16 +0300 Subject: [PATCH] Added authentication --- app/Hashing/MySQLHasher.php | 24 ++++++++ app/Http/Controllers/LoginController.php | 57 +++++++++++++++++++ app/Models/{User.php => Account.php} | 28 +++++++-- app/Providers/AuthServiceProvider.php | 9 ++- config/auth.php | 10 +--- config/database.php | 54 +++--------------- database/factories/UserFactory.php | 2 +- resources/views/layouts/app.blade.php | 32 ++++------- resources/views/user/administration.blade.php | 26 +++++---- resources/views/user/login.blade.php | 12 +++- routes/web.php | 30 ++++++---- 11 files changed, 181 insertions(+), 103 deletions(-) create mode 100644 app/Hashing/MySQLHasher.php create mode 100644 app/Http/Controllers/LoginController.php rename app/Models/{User.php => Account.php} (64%) diff --git a/app/Hashing/MySQLHasher.php b/app/Hashing/MySQLHasher.php new file mode 100644 index 0000000..69c5d48 --- /dev/null +++ b/app/Hashing/MySQLHasher.php @@ -0,0 +1,24 @@ +make($value) === $hashedValue; + } + + public function needsRehash($hashedValue, array $options = []): bool + { + return false; + } +} diff --git a/app/Http/Controllers/LoginController.php b/app/Http/Controllers/LoginController.php new file mode 100644 index 0000000..398adf5 --- /dev/null +++ b/app/Http/Controllers/LoginController.php @@ -0,0 +1,57 @@ +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'); + } +} diff --git a/app/Models/User.php b/app/Models/Account.php similarity index 64% rename from app/Models/User.php rename to app/Models/Account.php index 4d7f70f..c46b997 100644 --- a/app/Models/User.php +++ b/app/Models/Account.php @@ -4,23 +4,40 @@ // 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 */ protected $fillable = [ - 'name', - 'email', + 'login', 'password', + 'email', ]; /** @@ -30,7 +47,8 @@ class User extends Authenticatable */ protected $hidden = [ 'password', - 'remember_token', + 'social_id', + 'securitycode' ]; /** diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 54756cd..b35d8a6 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -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']); + }); } } diff --git a/config/auth.php b/config/auth.php index 9548c15..66120a0 100644 --- a/config/auth.php +++ b/config/auth.php @@ -61,14 +61,10 @@ 'providers' => [ 'users' => [ - 'driver' => 'eloquent', - 'model' => App\Models\User::class, + // 'driver' => 'eloquent', + 'driver' => 'legacy', + 'model' => App\Models\Account::class, ], - - // 'users' => [ - // 'driver' => 'database', - // 'table' => 'users', - // ], ], /* diff --git a/config/database.php b/config/database.php index 137ad18..6336ccf 100644 --- a/config/database.php +++ b/config/database.php @@ -15,7 +15,7 @@ | */ - 'default' => env('DB_CONNECTION', 'mysql'), + 'default' => env('DB_CONNECTION', 'website'), /* |-------------------------------------------------------------------------- @@ -35,62 +35,26 @@ 'connections' => [ - 'sqlite' => [ - 'driver' => 'sqlite', - 'url' => env('DATABASE_URL'), - 'database' => env('DB_DATABASE', database_path('database.sqlite')), - 'prefix' => '', - 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), - ], - - 'mysql' => [ + 'website' => [ 'driver' => 'mysql', - 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), - 'database' => env('DB_DATABASE', 'forge'), + 'database' => 'website', 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), - 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', - 'prefix' => '', - 'prefix_indexes' => true, - 'strict' => true, - 'engine' => null, - 'options' => extension_loaded('pdo_mysql') ? array_filter([ - PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), - ]) : [], ], - 'pgsql' => [ - 'driver' => 'pgsql', - 'url' => env('DATABASE_URL'), + 'account' => [ + 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), - 'port' => env('DB_PORT', '5432'), - 'database' => env('DB_DATABASE', 'forge'), + 'port' => env('DB_PORT', '3306'), + 'database' => 'account', 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), - 'charset' => 'utf8', - 'prefix' => '', - 'prefix_indexes' => true, - 'search_path' => 'public', - 'sslmode' => 'prefer', - ], - - 'sqlsrv' => [ - 'driver' => 'sqlsrv', - 'url' => env('DATABASE_URL'), - 'host' => env('DB_HOST', 'localhost'), - 'port' => env('DB_PORT', '1433'), - 'database' => env('DB_DATABASE', 'forge'), - 'username' => env('DB_USERNAME', 'forge'), - 'password' => env('DB_PASSWORD', ''), - 'charset' => 'utf8', - 'prefix' => '', - 'prefix_indexes' => true, - // 'encrypt' => env('DB_ENCRYPT', 'yes'), - // 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'), + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', ], ], diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index a6ecc0a..287426f 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -6,7 +6,7 @@ use Illuminate\Support\Str; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Account> */ class UserFactory extends Factory { diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 00be3e1..9689d21 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -59,7 +59,7 @@ fancybox_css.close.right = $('#fancybox-close').css('right'); fancybox_css.close.top = $('#fancybox-close').css('top'); fancybox_css.close.width = $('#fancybox-close').css('width'); - $('#fancybox-outer').css({'background': 'transparent url("img/isbg.png") center center no-repeat'}); + $('#fancybox-outer').css({'background': 'transparent url("/img/isbg.png") center center no-repeat'}); $('#fancybox-close').css({'background-image': 'none', 'height': '16px', 'right': '3px', 'top': '7px', 'width': '16px'}); }, 'onComplete': function() { @@ -129,14 +129,14 @@ function () { @else
-
Bun venit, " . $_SESSION['id'] . "
-
Ai " . $_SESSION['coins'] . " Monede
+
Bun venit, {{ Auth::user()->login }}
+
Ai {{ Auth::user()->cash }} Monede Dragon

@@ -180,18 +180,20 @@ function () {

Logare

+ @csrf + diff --git a/resources/views/user/login.blade.php b/resources/views/user/login.blade.php index 6f6f66c..95e9708 100644 --- a/resources/views/user/login.blade.php +++ b/resources/views/user/login.blade.php @@ -12,19 +12,27 @@

Ai uitat parola?Logare

+ @csrf +
- +
- +

Intrând aici, accept Termenii şi condiţiile.

* este necesar

diff --git a/routes/web.php b/routes/web.php index 0c35489..4b2b55f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ group(function() { Route::get('/register', fn () => view('user/register')); - Route::get('/login', fn () => view('user/login')); + Route::get('/login', fn () => view('user/login'))->name('login'); + Route::post('/login', [LoginController::class, 'login']); // will autocomplete the username Route::get('/login/{username}', fn ($username) => view('user/login')); - Route::get('/logout', fn () => view('user/logout')); - - Route::get('/administration', fn () => view('user/administration')); - - Route::get('/characters', fn () => view('user/characters')); + Route::get('/logout', [LoginController::class, 'logout']); Route::get('/passwordlostrequest', fn () => view('user/passwordlostrequest')); Route::get('/passwordlost/{username}/{hash}', fn ($username, $hash) => view('user/passwordlost-expired')); Route::get('/resendack', fn () => view('user/resendack')); - Route::get('/emailchangeaccept/{username}/{hash}', fn ($username, $hash) => view('user/emailchangeaccept')); - Route::get('/emailchangecancel/{username}/{hash}', fn ($username, $hash) => view('user/emailchangecancel')); + Route::middleware('auth')->group(function() { + Route::get('/administration', fn () => view('user/administration')); - Route::get('/changeemailcode/{username}/{hash}', fn ($username, $hash) => view('user/changeemailcode')); + Route::get('/characters', fn () => view('user/characters')); - Route::get('/generatecode/{userId}/{hash}', fn ($userId, $hash) => view('user/generatecode')); + Route::get('/emailchangeaccept/{username}/{hash}', fn ($username, $hash) => view('user/emailchangeaccept')); + Route::get('/emailchangecancel/{username}/{hash}', fn ($username, $hash) => view('user/emailchangecancel')); - Route::get('/lostpasswordcode/{userId}/{hash}', fn ($userId, $hash) => view('user/lostpasswordcode')); + Route::get('/changeemailcode/{username}/{hash}', fn ($username, $hash) => view('user/changeemailcode')); + + Route::get('/generatecode/{userId}/{hash}', fn ($userId, $hash) => view('user/generatecode')); + + Route::get('/lostpasswordcode/{userId}/{hash}', fn ($userId, $hash) => view('user/lostpasswordcode')); + }); }); Route::prefix('contest')->group(function() { @@ -84,3 +88,7 @@ Route::get('/privacy', fn () => view('legal/privacy')); Route::get('/imprint', fn () => view('legal/imprint')); }); + +Route::fallback(function() { + return view('errors.404'); +});