feat: register & login

This commit is contained in:
2025-09-01 20:21:00 +03:30
parent ed377842c9
commit 4c4c3c5f7b
3 changed files with 103 additions and 67 deletions
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
class TestHash extends Command
{
protected $signature = 'test:hash';
protected $description = 'Test Hash::make and Hash::check';
public function handle()
{
$plain = 'mypassword123';
$hashed = Hash::make($plain);
$this->info("Plain: $plain");
$this->info("Hashed: $hashed");
$check = Hash::check($plain, $hashed) ? 'Match' : 'Does not match';
$checkWrong = Hash::check('wrongpass', $hashed) ? 'Match' : 'Does not match';
$this->info("Check correct password: $check");
$this->info("Check wrong password: $checkWrong");
}
}
+63 -67
View File
@@ -22,75 +22,67 @@
use App\Models\UserBreathingSession; use App\Models\UserBreathingSession;
class UserController extends Controller class UserController extends Controller
{ {
public function login(Request $request) public function login(Request $request)
{ {
$data = $request->validate([ $data = $request->validate([
'auth' => 'string|required', 'auth' => 'string|required',
'password' => 'string|required' 'password' => 'string|required'
]); ]);
if (filter_var($data['auth'], FILTER_VALIDATE_EMAIL)) { $authType = filter_var($data['auth'], FILTER_VALIDATE_EMAIL) ? 'email' : 'mobile';
$authType = 'email'; if ($authType == 'mobile') {
} elseif (MobileNumberHelper::checkMobileNumber($data['auth'])) { $data['auth'] = MobileNumberHelper::formatMobile($data['auth']);
$authType = 'mobile';
$data['auth'] = MobileNumberHelper::formatMobile($data['auth']);
} else {
return response()->json([
'message' => 'wrong credentials'
], 404);
}
if (!$user = User::where($authType, $data['auth'])->first()) {
return response()->json([
'message' => 'wrong credentials'
], 404);
}
if (!Hash::check($data['password'], $user->password)) {
return response()->json([
'message' => 'wrong credentials'
], 404);
}
$token = '';
if ($user->is_admin) {
$token = $user->createToken('token', ['admin'])->plainTextToken;
} else {
$token = $user->createToken('token', ['user'])->plainTextToken;
}
return response()->json([
'user' => $user,
'token' => $token,
]);
} }
public function register(Request $request) $user = User::where($authType, $data['auth'])->first();
{ return response()->json([
$data = $request->validate([ 'user' => $user,
'first_name' => 'string', // 'token' => $token,
'last_name' => 'string', 'hash'=> trim($data['password']),
'email' => 'email|unique:users|required_without:mobile', 'password' => $user ? $user->password : null,
'password' => 'string|nullable|required_with:email', 'check' => $user ? Hash::check(trim($data['password']), $user->password) : null,
'mobile' => ['string', new MobileNumber, 'nullable', 'unique:users', 'required_without:email'],
]);
if(isset($data['mobile'])){ ]);
$data['mobile'] = MobileNumberHelper::formatMobile($data['mobile']); if (!$user || !Hash::check(trim($data['password']), $user->password)) {
return response()->json(['message' => 'wrong credentials'], 404);
}
$token = $user->createToken('token', $user->is_admin ? ['admin'] : ['user'])->plainTextToken;
return response()->json([
'user' => $user,
'token' => $token,
]);
}
public function register(Request $request)
{
$data = $request->validate([
'first_name' => 'string|nullable',
'last_name' => 'string|nullable',
'email' => 'email|required_without:mobile',
'password' => 'string|nullable|required_with:email',
'mobile' => ['string', new MobileNumber, 'nullable', 'required_without:email'],
]);
if (isset($data['mobile'])) {
$data['mobile'] = MobileNumberHelper::formatMobile($data['mobile']);
if (User::where('mobile', $data['mobile'])->exists()) {
return response()->json(['message' => 'Mobile number already exists'], 400);
} }
}
if (!isset($data['password'])) { if (isset($data['email']) && User::where('email', $data['email'])->exists()) {
$data['password'] = Hash::make(config('app.default_password')); return response()->json(['message' => 'Email already exists'], 400);
} else { }
$data['password'] = Hash::make($data['password']); $pass = $data['password'] ?? null;
} $data['password'] = isset($data['password']) ? Hash::make($data['password']) : Hash::make(config('app.default_password'));
$user = User::create($data); $user = User::create($data);
// Generate verification code if (isset($data['email'])) {
$code = rand(100000, 999999); $code = rand(100000, 999999);
// Save verification record
\DB::table('email_verifications')->insert([ \DB::table('email_verifications')->insert([
'user_id' => $user->id, 'user_id' => $user->id,
'code' => $code, 'code' => $code,
@@ -98,16 +90,20 @@ public function register(Request $request)
'created_at' => now(), 'created_at' => now(),
'updated_at' => now(), 'updated_at' => now(),
]); ]);
// Send verification email
\Mail::to($user->email)->send(new \App\Mail\VerifyEmailCodeMail($code)); \Mail::to($user->email)->send(new \App\Mail\VerifyEmailCodeMail($code));
return response()->json([
'message' => 'User registered successfully. Verification code sent to email.',
'user' => $user,
]);
} }
return response()->json([
'message' => 'User registered successfully.',
'user' => $user,
'hash' => Hash::make($data['password']),
'plain_password' => $pass,
'check' => isset($pass) ? Hash::check($pass, $data['password']) : null,
]);
}
public function verifyEmailCode(Request $request) public function verifyEmailCode(Request $request)
{ {
$request->validate([ $request->validate([
+13
View File
@@ -9,6 +9,19 @@
use App\Http\Controllers\BreathingTemplate; use App\Http\Controllers\BreathingTemplate;
use App\Http\Controllers\MoodController; use App\Http\Controllers\MoodController;
use App\Http\Controllers\WorryController; use App\Http\Controllers\WorryController;
use Illuminate\Support\Facades\Hash;
Route::get('/test-hash', function() {
$plain = 'amnk1380';
$hash = Hash::make($plain);
return [
'plain' => $plain,
'hash' => $hash,
'check' => Hash::check($plain, $hash), // should be true
];
});
Route::get('package-names/{name}/products', [ProductController::class, 'index']); Route::get('package-names/{name}/products', [ProductController::class, 'index']);
Route::prefix('auth')->controller(UserController::class)->group(function () { Route::prefix('auth')->controller(UserController::class)->group(function () {