Compare commits
2 Commits
d2c3564c26
...
48a7703436
| Author | SHA1 | Date | |
|---|---|---|---|
| 48a7703436 | |||
| 39fae18ef6 |
@ -21,15 +21,42 @@ public function showLogin()
|
|||||||
*/
|
*/
|
||||||
public function login(Request $request)
|
public function login(Request $request)
|
||||||
{
|
{
|
||||||
$credentials = $request->validate([
|
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
|
||||||
'email' => ['required', 'email'],
|
'email' => ['required', 'email'],
|
||||||
'password' => ['required'],
|
'password' => ['required'],
|
||||||
|
], [
|
||||||
|
'email.required' => 'Please enter your email address.',
|
||||||
|
'email.email' => 'Please enter a valid email address.',
|
||||||
|
'password.required' => 'Please enter your password.',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if ($validator->fails()) {
|
||||||
|
return back()->withErrors($validator)->withInput($request->only('email'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$credentials = $validator->validated();
|
||||||
|
|
||||||
// Database-backed authentication check
|
// Database-backed authentication check
|
||||||
$user = \App\Models\User::where('email', $credentials['email'])->first();
|
$user = \App\Models\User::where('email', $credentials['email'])->first();
|
||||||
|
|
||||||
if ($user && \Illuminate\Support\Facades\Hash::check($credentials['password'], $user->password) && $user->role === 'admin') {
|
if (!$user) {
|
||||||
|
return back()->withErrors([
|
||||||
|
'email' => 'This email is not registered.',
|
||||||
|
])->withInput($request->only('email'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user->role !== 'admin') {
|
||||||
|
return back()->withErrors([
|
||||||
|
'email' => 'This email is not registered as an admin.',
|
||||||
|
])->withInput($request->only('email'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!\Illuminate\Support\Facades\Hash::check($credentials['password'], $user->password)) {
|
||||||
|
return back()->withErrors([
|
||||||
|
'password' => 'The password you entered is incorrect.',
|
||||||
|
])->withInput($request->only('email'));
|
||||||
|
}
|
||||||
|
|
||||||
$request->session()->regenerate();
|
$request->session()->regenerate();
|
||||||
|
|
||||||
auth()->login($user);
|
auth()->login($user);
|
||||||
@ -44,11 +71,6 @@ public function login(Request $request)
|
|||||||
return redirect()->intended('/admin/dashboard');
|
return redirect()->intended('/admin/dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
return back()->withErrors([
|
|
||||||
'email' => 'The provided credentials do not match our records. (Use admin@example.com / password)',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log the admin out of the application.
|
* Log the admin out of the application.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -623,10 +623,238 @@ public function nationalities(Request $request)
|
|||||||
|
|
||||||
$rawNationalities = \App\Models\Nationality::all();
|
$rawNationalities = \App\Models\Nationality::all();
|
||||||
|
|
||||||
|
$callingCodes = [
|
||||||
|
'AF' => '+93',
|
||||||
|
'AL' => '+355',
|
||||||
|
'DZ' => '+213',
|
||||||
|
'AD' => '+376',
|
||||||
|
'AO' => '+244',
|
||||||
|
'AG' => '+1-268',
|
||||||
|
'AR' => '+54',
|
||||||
|
'AM' => '+374',
|
||||||
|
'AU' => '+61',
|
||||||
|
'AT' => '+43',
|
||||||
|
'AZ' => '+994',
|
||||||
|
'BS' => '+1-242',
|
||||||
|
'BH' => '+973',
|
||||||
|
'BD' => '+880',
|
||||||
|
'BB' => '+1-246',
|
||||||
|
'BY' => '+375',
|
||||||
|
'BE' => '+32',
|
||||||
|
'BZ' => '+501',
|
||||||
|
'BJ' => '+229',
|
||||||
|
'BT' => '+975',
|
||||||
|
'BO' => '+591',
|
||||||
|
'BA' => '+387',
|
||||||
|
'BW' => '+267',
|
||||||
|
'BR' => '+55',
|
||||||
|
'BN' => '+673',
|
||||||
|
'BG' => '+359',
|
||||||
|
'BF' => '+226',
|
||||||
|
'BI' => '+257',
|
||||||
|
'KH' => '+855',
|
||||||
|
'CM' => '+237',
|
||||||
|
'CA' => '+1',
|
||||||
|
'CV' => '+238',
|
||||||
|
'CF' => '+236',
|
||||||
|
'TD' => '+235',
|
||||||
|
'CL' => '+56',
|
||||||
|
'CN' => '+86',
|
||||||
|
'CO' => '+57',
|
||||||
|
'KM' => '+269',
|
||||||
|
'CG' => '+242',
|
||||||
|
'CD' => '+243',
|
||||||
|
'CR' => '+506',
|
||||||
|
'HR' => '+385',
|
||||||
|
'CU' => '+53',
|
||||||
|
'CY' => '+357',
|
||||||
|
'CZ' => '+420',
|
||||||
|
'DK' => '+45',
|
||||||
|
'DJ' => '+253',
|
||||||
|
'DM' => '+1-767',
|
||||||
|
'DO' => '+1-809',
|
||||||
|
'TL' => '+670',
|
||||||
|
'EC' => '+593',
|
||||||
|
'EG' => '+20',
|
||||||
|
'SV' => '+503',
|
||||||
|
'GQ' => '+240',
|
||||||
|
'ER' => '+291',
|
||||||
|
'EE' => '+372',
|
||||||
|
'ET' => '+251',
|
||||||
|
'FJ' => '+679',
|
||||||
|
'FI' => '+358',
|
||||||
|
'FR' => '+33',
|
||||||
|
'GA' => '+241',
|
||||||
|
'GM' => '+220',
|
||||||
|
'GE' => '+995',
|
||||||
|
'DE' => '+49',
|
||||||
|
'GH' => '+233',
|
||||||
|
'GR' => '+30',
|
||||||
|
'GD' => '+1-473',
|
||||||
|
'GT' => '+502',
|
||||||
|
'GN' => '+224',
|
||||||
|
'GW' => '+245',
|
||||||
|
'GY' => '+592',
|
||||||
|
'HT' => '+509',
|
||||||
|
'HN' => '+504',
|
||||||
|
'HU' => '+36',
|
||||||
|
'IS' => '+354',
|
||||||
|
'IN' => '+91',
|
||||||
|
'ID' => '+62',
|
||||||
|
'IR' => '+98',
|
||||||
|
'IQ' => '+964',
|
||||||
|
'IE' => '+353',
|
||||||
|
'IL' => '+972',
|
||||||
|
'IT' => '+39',
|
||||||
|
'JM' => '+1-876',
|
||||||
|
'JP' => '+81',
|
||||||
|
'JO' => '+962',
|
||||||
|
'KZ' => '+7',
|
||||||
|
'KE' => '+254',
|
||||||
|
'KI' => '+686',
|
||||||
|
'KP' => '+850',
|
||||||
|
'KR' => '+82',
|
||||||
|
'KW' => '+965',
|
||||||
|
'KG' => '+996',
|
||||||
|
'LA' => '+856',
|
||||||
|
'LV' => '+371',
|
||||||
|
'LB' => '+961',
|
||||||
|
'LS' => '+266',
|
||||||
|
'LR' => '+231',
|
||||||
|
'LY' => '+218',
|
||||||
|
'LI' => '+423',
|
||||||
|
'LT' => '+370',
|
||||||
|
'LU' => '+352',
|
||||||
|
'MK' => '+389',
|
||||||
|
'MG' => '+261',
|
||||||
|
'MW' => '+265',
|
||||||
|
'MY' => '+60',
|
||||||
|
'MV' => '+960',
|
||||||
|
'ML' => '+223',
|
||||||
|
'MT' => '+356',
|
||||||
|
'MH' => '+692',
|
||||||
|
'MR' => '+222',
|
||||||
|
'MU' => '+230',
|
||||||
|
'MX' => '+52',
|
||||||
|
'FM' => '+691',
|
||||||
|
'MD' => '+373',
|
||||||
|
'MC' => '+377',
|
||||||
|
'MN' => '+976',
|
||||||
|
'ME' => '+382',
|
||||||
|
'MA' => '+212',
|
||||||
|
'MZ' => '+258',
|
||||||
|
'MM' => '+95',
|
||||||
|
'NA' => '+264',
|
||||||
|
'NR' => '+674',
|
||||||
|
'NP' => '+977',
|
||||||
|
'NL' => '+31',
|
||||||
|
'NZ' => '+64',
|
||||||
|
'NI' => '+505',
|
||||||
|
'NE' => '+227',
|
||||||
|
'NG' => '+234',
|
||||||
|
'NO' => '+47',
|
||||||
|
'OM' => '+968',
|
||||||
|
'PK' => '+92',
|
||||||
|
'PW' => '+680',
|
||||||
|
'PA' => '+507',
|
||||||
|
'PG' => '+675',
|
||||||
|
'PY' => '+595',
|
||||||
|
'PE' => '+51',
|
||||||
|
'PH' => '+63',
|
||||||
|
'PL' => '+48',
|
||||||
|
'PT' => '+351',
|
||||||
|
'QA' => '+974',
|
||||||
|
'RO' => '+40',
|
||||||
|
'RU' => '+7',
|
||||||
|
'RW' => '+250',
|
||||||
|
'KN' => '+1-869',
|
||||||
|
'LC' => '+1-758',
|
||||||
|
'VC' => '+1-784',
|
||||||
|
'WS' => '+685',
|
||||||
|
'SM' => '+378',
|
||||||
|
'ST' => '+239',
|
||||||
|
'SA' => '+966',
|
||||||
|
'SN' => '+221',
|
||||||
|
'RS' => '+381',
|
||||||
|
'SC' => '+248',
|
||||||
|
'SL' => '+232',
|
||||||
|
'SG' => '+65',
|
||||||
|
'SK' => '+421',
|
||||||
|
'SI' => '+386',
|
||||||
|
'SB' => '+677',
|
||||||
|
'SO' => '+252',
|
||||||
|
'ZA' => '+27',
|
||||||
|
'SS' => '+211',
|
||||||
|
'ES' => '+34',
|
||||||
|
'LK' => '+94',
|
||||||
|
'SD' => '+249',
|
||||||
|
'SR' => '+597',
|
||||||
|
'SZ' => '+268',
|
||||||
|
'SE' => '+46',
|
||||||
|
'CH' => '+41',
|
||||||
|
'SY' => '+963',
|
||||||
|
'TJ' => '+992',
|
||||||
|
'TZ' => '+255',
|
||||||
|
'TH' => '+66',
|
||||||
|
'TG' => '+228',
|
||||||
|
'TO' => '+676',
|
||||||
|
'TT' => '+1-868',
|
||||||
|
'TN' => '+216',
|
||||||
|
'TR' => '+90',
|
||||||
|
'TM' => '+993',
|
||||||
|
'TV' => '+688',
|
||||||
|
'UG' => '+256',
|
||||||
|
'UA' => '+380',
|
||||||
|
'AE' => '+971',
|
||||||
|
'GB' => '+44',
|
||||||
|
'US' => '+1',
|
||||||
|
'UY' => '+598',
|
||||||
|
'UZ' => '+998',
|
||||||
|
'VU' => '+678',
|
||||||
|
'VE' => '+58',
|
||||||
|
'VN' => '+84',
|
||||||
|
'YE' => '+967',
|
||||||
|
'ZM' => '+260',
|
||||||
|
'ZW' => '+263',
|
||||||
|
'AI' => '+1-264',
|
||||||
|
'VG' => '+1-284',
|
||||||
|
'BM' => '+1-441',
|
||||||
|
'KY' => '+1-345',
|
||||||
|
'CK' => '+682',
|
||||||
|
'CY-W' => '+44',
|
||||||
|
'CY-M' => '+44',
|
||||||
|
'FO' => '+298',
|
||||||
|
'GI' => '+350',
|
||||||
|
'GL' => '+299',
|
||||||
|
'GU' => '+1-671',
|
||||||
|
'HK' => '+852',
|
||||||
|
'XK' => '+383',
|
||||||
|
'MO' => '+853',
|
||||||
|
'MQ' => '+596',
|
||||||
|
'MS' => '+1-664',
|
||||||
|
'NU' => '+683',
|
||||||
|
'GB-NIR' => '+44',
|
||||||
|
'PS' => '+970',
|
||||||
|
'PN' => '+64',
|
||||||
|
'PR' => '+1-787',
|
||||||
|
'GB-SCT' => '+44',
|
||||||
|
'SH' => '+290',
|
||||||
|
'ST-L' => '',
|
||||||
|
'TA-T' => '+290',
|
||||||
|
'TC' => '+1-649',
|
||||||
|
'VA' => '+379',
|
||||||
|
'WF' => '+681',
|
||||||
|
'GB-WLS' => '+44',
|
||||||
|
];
|
||||||
|
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($rawNationalities as $item) {
|
foreach ($rawNationalities as $item) {
|
||||||
$list[] = [
|
$list[] = [
|
||||||
'code' => $item->code,
|
'code' => $item->code,
|
||||||
|
'country_code' => $item->code,
|
||||||
|
'phone_code' => $callingCodes[$item->code] ?? '',
|
||||||
|
'dial_code' => $callingCodes[$item->code] ?? '',
|
||||||
|
'calling_code' => $callingCodes[$item->code] ?? '',
|
||||||
'name' => $item->{$lang} ?? $item->name,
|
'name' => $item->{$lang} ?? $item->name,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,7 +54,7 @@ export default function Login() {
|
|||||||
<hr className="border-slate-100 mb-6" />
|
<hr className="border-slate-100 mb-6" />
|
||||||
|
|
||||||
{/* Form */}
|
{/* Form */}
|
||||||
<form onSubmit={handleSubmit} className="space-y-5">
|
<form onSubmit={handleSubmit} noValidate className="space-y-5">
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1.5">
|
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1.5">
|
||||||
Email Address
|
Email Address
|
||||||
|
|||||||
124
tests/Feature/AdminLoginTest.php
Normal file
124
tests/Feature/AdminLoginTest.php
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AdminLoginTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test admin login page can be rendered.
|
||||||
|
*/
|
||||||
|
public function test_admin_can_view_login_page()
|
||||||
|
{
|
||||||
|
$response = $this->get('/admin/login');
|
||||||
|
$response->assertStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test admin login validation fails if email is invalid.
|
||||||
|
*/
|
||||||
|
public function test_admin_login_fails_if_email_is_invalid()
|
||||||
|
{
|
||||||
|
$response = $this->post('/admin/login', [
|
||||||
|
'email' => 'invalid-email',
|
||||||
|
'password' => 'password',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertSessionHasErrors(['email']);
|
||||||
|
$this->assertEquals(
|
||||||
|
'Please enter a valid email address.',
|
||||||
|
session('errors')->get('email')[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test admin login validation fails if email is not registered.
|
||||||
|
*/
|
||||||
|
public function test_admin_login_fails_if_email_is_not_registered()
|
||||||
|
{
|
||||||
|
$response = $this->post('/admin/login', [
|
||||||
|
'email' => 'unregistered@example.com',
|
||||||
|
'password' => 'password',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertSessionHasErrors(['email']);
|
||||||
|
$this->assertEquals(
|
||||||
|
'This email is not registered.',
|
||||||
|
session('errors')->get('email')[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test admin login validation fails if email is registered but not as an admin.
|
||||||
|
*/
|
||||||
|
public function test_admin_login_fails_if_user_is_not_an_admin()
|
||||||
|
{
|
||||||
|
User::create([
|
||||||
|
'name' => 'Regular User',
|
||||||
|
'email' => 'user@example.com',
|
||||||
|
'password' => bcrypt('password'),
|
||||||
|
'role' => 'employer', // Not admin
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->post('/admin/login', [
|
||||||
|
'email' => 'user@example.com',
|
||||||
|
'password' => 'password',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertSessionHasErrors(['email']);
|
||||||
|
$this->assertEquals(
|
||||||
|
'This email is not registered as an admin.',
|
||||||
|
session('errors')->get('email')[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test admin login validation fails if password is wrong.
|
||||||
|
*/
|
||||||
|
public function test_admin_login_fails_if_password_is_incorrect()
|
||||||
|
{
|
||||||
|
User::create([
|
||||||
|
'name' => 'Admin User',
|
||||||
|
'email' => 'admin_test@example.com',
|
||||||
|
'password' => bcrypt('correct_password'),
|
||||||
|
'role' => 'admin',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->post('/admin/login', [
|
||||||
|
'email' => 'admin_test@example.com',
|
||||||
|
'password' => 'wrong_password',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertSessionHasErrors(['password']);
|
||||||
|
$this->assertEquals(
|
||||||
|
'The password you entered is incorrect.',
|
||||||
|
session('errors')->get('password')[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test admin login succeeds with correct credentials.
|
||||||
|
*/
|
||||||
|
public function test_admin_login_succeeds_with_correct_credentials()
|
||||||
|
{
|
||||||
|
$admin = User::create([
|
||||||
|
'name' => 'Admin User',
|
||||||
|
'email' => 'admin_test@example.com',
|
||||||
|
'password' => bcrypt('correct_password'),
|
||||||
|
'role' => 'admin',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->post('/admin/login', [
|
||||||
|
'email' => 'admin_test@example.com',
|
||||||
|
'password' => 'correct_password',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertRedirect('/admin/dashboard');
|
||||||
|
$this->assertAuthenticatedAs($admin);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -660,10 +660,18 @@ public function test_nationalities_list_api()
|
|||||||
])
|
])
|
||||||
->assertJsonFragment([
|
->assertJsonFragment([
|
||||||
'code' => 'IN',
|
'code' => 'IN',
|
||||||
|
'country_code' => 'IN',
|
||||||
|
'phone_code' => '+91',
|
||||||
|
'dial_code' => '+91',
|
||||||
|
'calling_code' => '+91',
|
||||||
'name' => 'Indian',
|
'name' => 'Indian',
|
||||||
])
|
])
|
||||||
->assertJsonFragment([
|
->assertJsonFragment([
|
||||||
'code' => 'PH',
|
'code' => 'PH',
|
||||||
|
'country_code' => 'PH',
|
||||||
|
'phone_code' => '+63',
|
||||||
|
'dial_code' => '+63',
|
||||||
|
'calling_code' => '+63',
|
||||||
'name' => 'Filipino',
|
'name' => 'Filipino',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -728,6 +736,10 @@ public function test_nationalities_list_api()
|
|||||||
$this->assertGreaterThanOrEqual(1, count($responseSearch->json('data.nationalities')));
|
$this->assertGreaterThanOrEqual(1, count($responseSearch->json('data.nationalities')));
|
||||||
$responseSearch->assertJsonFragment([
|
$responseSearch->assertJsonFragment([
|
||||||
'code' => 'IN',
|
'code' => 'IN',
|
||||||
|
'country_code' => 'IN',
|
||||||
|
'phone_code' => '+91',
|
||||||
|
'dial_code' => '+91',
|
||||||
|
'calling_code' => '+91',
|
||||||
'name' => 'Indian'
|
'name' => 'Indian'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user