Added authentication

This commit is contained in:
Exynox 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']);
});
}
}

View File

@ -61,14 +61,10 @@ return [
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
// 'driver' => 'eloquent',
'driver' => 'legacy',
'model' => App\Models\Account::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*

View File

@ -15,7 +15,7 @@ return [
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
'default' => env('DB_CONNECTION', 'website'),
/*
|--------------------------------------------------------------------------
@ -35,62 +35,26 @@ return [
'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',
],
],

View File

@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Factories\Factory;
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
{

View File

@ -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 @@
</div>
@else
<div id="userBox">
<div class='welcome-text welcome-text-left'>Bun venit, " . $_SESSION['id'] . "</div>
<div class='welcome-text welcome-text-right'>Ai " . $_SESSION['coins'] . " Monede</div>
<div class="welcome-text welcome-text-left">Bun venit, {{ Auth::user()->login }}</div>
<div class="welcome-text welcome-text-right">Ai {{ Auth::user()->cash }} Monede Dragon</div>
<br class="clearfloat" />
<div class="header-box-nav-container">
<ul class="header-box-nav-login" style="position:absolute; margin-left:-0px;">
<li class="stepdown"><a href="?s=donate" class="nav-box-btn nav-box-btn-1">Doneaza</a></li>
<li class="stepdown"><a href="?s=administration" class="nav-box-btn nav-box-btn-2">Datele utilizatorului</a></li>
<li class="stepdown"><a href="?s=home&logout" class="nav-box-btn nav-box-btn-4">Delogare</a></li>
<li class="stepdown"><a href="#" class="nav-box-btn nav-box-btn-1">Încarcă MD</a></li>
<li class="stepdown"><a href="{{ url('user/administration') }}" class="nav-box-btn nav-box-btn-2">Datele utilizatorului</a></li>
<li class="stepdown"><a href="{{ url('user/logout') }}" class="nav-box-btn nav-box-btn-4">Delogare</a></li>
</ul>
</div>
</div>
@ -180,18 +180,20 @@
<div class="modul-box-bg-bottom">
<h3>Logare</h3>
<form action="{{ url('user/login') }}" method="post">
@csrf
<div class="form-login">
<label>Nume de utilizator</label>
<div class="input">
<input type="text" name="user"><br>
<input type="text" name="login"><br>
</div>
<label>Parola</label>
<div class="input">
<input type="password" name="pw"><br>
<input type="password" name="password"><br>
</div>
<div>
<input type="submit" class="button btn-login" name="login" value="Login">
<input type="submit" class="button btn-login" value="Login">
<p class="agbok">
Intrând aici, accept <a href="{{ url('legal/terms') }}"><strong>Termenii și condițiile</strong></a>.
<a href="{{ url('user/passwordlostrequest') }}" rel="nofollow" class="password">Ai uitat parola?</a>
@ -209,7 +211,7 @@
<div class="modul-box-bg">
<div class="modul-box-bg-bottom">
<h3>Descărcare</h3>
<a href="?s=download" class="btn download-btn"></a>
<a href="{{ url('main/download') }}" class="btn download-btn"></a>
</div>
</div>
</div>
@ -219,16 +221,6 @@
<div class="modul-box-bg">
<div class="modul-box-bg-bottom">
<h3>Magazinul de item-uri</h3>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
dataType: 'jsonp', data: '', jsonp: '',
url: "/is/",
success: function(returndata) {;},
error: function(XMLHttpRequest, textStatus, errorThrown) {;}
});
});
</script>
<a href="/is/" class="itemshop btn itemshop-btn iframe" title="Magazinul de item-uri"></a>
</div>
</div>

View File

@ -12,21 +12,25 @@
<h4>Datele utilizatorului</h4>
<ul>
<li>Nume de utilizator: gigelfrone</li>
<li>Email: <span id="yourEmail">gigelfrone@gmail.com</span></li>
<li>Monede Dragon: 123 <a href="#" class="load-link">(top up)</a></li>
<li>Semnele Dragonului: 20</li>
<li class="long-li-code">Cod de ștergere caracter: <span class="delete-code">încă nespecificat</span></li>
<li>Nume de utilizator: {{ Auth::user()->login }}</li>
<li>Email: <span id="yourEmail">{{ Auth::user()->email }}</span></li>
<li>Monede Dragon: {{ Auth::user()->cash }} <a href="#" class="load-link">(top up)</a></li>
<li>Semnele Dragonului: {{ Auth::user()->mileage }}</li>
@if (Auth::user()->social_id)
<li class="long-li-code">Cod de ștergere caracter: <span class="delete-code">{{ Auth::user()->social_id }}</span></li>
@else
<li class="long-li-code">Cod de ștergere caracter: <span class="delete-code">încă nespecificat</span></li>
@endif
</ul>
<div class="administration-box"><a href="#" class="btn">Încarcă MD</a><p>Îmbunătățește-ți contul folosind Monede Dragon</p></div>
<div class="administration-box"><a href="#" class="btn">Caracter</a><p>Lista caracterelor</p></div>
<div class="administration-box"><a href="{{ url('user/characters') }}" class="btn">Caracter</a><p>Lista caracterelor</p></div>
{{-- <div class="administration-box"><a href="#" class="btn">Prieteni</a><p>Recruteaza prieteni si vezi statusul noilor prieteni recrutati</p></div> --}}
<div class="administration-box"><a href="?s=emailchange" class="btn">Email</a><p>Schimbă adresa de email</p></div>
<div class="administration-box"><a href="#passwordchangerequest" class="btn">Parola</a><p>Schimbă parola</p></div>
<div class="administration-box"><a href="?s=pwchange" class="btn">Schimba Datele</a><p>Schimba datele contului</p></div>
<div class="administration-box"><a href="?s=storagepasswordlost" class="btn">Parola Depozit</a><p>Cere parola de la Depozit</p></div>
<div class="administration-box"><a href="?s=displaycode" class="btn">Parola Stergere</a><p>Codul pentru ştergerea personajelor</p></div>
<div class="administration-box"><a href="{{ url('user/emailchange') }}" class="btn">Email</a><p>Schimbă adresa de email</p></div>
<div class="administration-box"><a href="{{ url('user/passwordchangerequest') }}" class="btn">Parola</a><p>Schimbă parola</p></div>
<div class="administration-box"><a href="{{ url('user/storagepasswordlost') }}" class="btn">Parola Depozit</a><p>Cere parola de la Depozit</p></div>
<div class="administration-box"><a href="{{ url('user/displaycode') }}" class="btn">Parola Stergere</a><p>Codul pentru ştergerea personajelor</p></div>
<div class="administration-box"><a href="#" class="btn">Anularea contului</a><p>Anularea contului</p></div>
</div>

View File

@ -12,19 +12,27 @@
<h3><a id="topwLost" href="{{ url('user/passwordlostrequest') }}" title="Ai uitat parola?">Ai uitat parola?</a>Logare</h3>
<div class="trenner"></div>
<form name="loginForm" id="loginForm" action="{{ url('user/login') }}" method="post">
@csrf
<div>
<label for="username">Nume de utilizator: *</label>
<input type="text" class="validate[required,custom[noSpecialCharacters]]" id="username" name="username" maxlength="16" value=""/>
<input type="text" class="validate[required,custom[noSpecialCharacters]]" id="username" name="login" maxlength="16" value="{{ old('login') }}"/>
</div>
<div>
<label for="password">Parola: *</label>
<input type="password" class="validate[required,length[5,16]]" id="password" name="password" maxlength="16" value=""/>
<input type="password" class="validate[required,length[5,16]]" id="password" name="password" maxlength="16" value="{{ old('password') }}" />
</div>
<div id="checkerror">
<p>Intrând aici, accept <a href="{{ url('legal/terms') }}" target="_blank"><strong>Termenii şi condiţiile</strong></a>.</p>
</div>
<input id="submitBtn" class="btn-big" type="submit" name="SubmitLoginForm" value="Trimite"/>
<script type="text/javascript">
@error('login')
$.validationEngine.buildPrompt("#username", "{{ $message }}", "error");
@enderror
@error('password')
$.validationEngine.buildPrompt("#password", "{{ $message }}", "error");
@enderror
</script>
</form>
<p id="regLegend">* este necesar</p>

View File

@ -1,5 +1,6 @@
<?php
use App\Http\Controllers\LoginController;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
@ -49,27 +50,30 @@ Route::prefix('main')->group(function() {
Route::prefix('user')->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::prefix('legal')->group(function() {
Route::get('/privacy', fn () => view('legal/privacy'));
Route::get('/imprint', fn () => view('legal/imprint'));
});
Route::fallback(function() {
return view('errors.404');
});