From 4c4c3c5f7b83f2ef3f6b016a9fc94427ae6ce4b2 Mon Sep 17 00:00:00 2001 From: Amirmahdi Nourkazemi Date: Mon, 1 Sep 2025 20:21:00 +0330 Subject: [PATCH] feat: register & login --- app/Console/Commands/TestHash.php | 27 +++++ app/Http/Controllers/UserController.php | 130 ++++++++++++------------ routes/api.php | 13 +++ 3 files changed, 103 insertions(+), 67 deletions(-) create mode 100644 app/Console/Commands/TestHash.php diff --git a/app/Console/Commands/TestHash.php b/app/Console/Commands/TestHash.php new file mode 100644 index 0000000..2a40c02 --- /dev/null +++ b/app/Console/Commands/TestHash.php @@ -0,0 +1,27 @@ +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"); + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 181de5c..2bb7d36 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -22,75 +22,67 @@ use App\Models\UserBreathingSession; class UserController extends Controller { - public function login(Request $request) - { - $data = $request->validate([ - 'auth' => 'string|required', - 'password' => 'string|required' - ]); + public function login(Request $request) +{ + $data = $request->validate([ + 'auth' => 'string|required', + 'password' => 'string|required' + ]); - if (filter_var($data['auth'], FILTER_VALIDATE_EMAIL)) { - $authType = 'email'; - } elseif (MobileNumberHelper::checkMobileNumber($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, - ]); + $authType = filter_var($data['auth'], FILTER_VALIDATE_EMAIL) ? 'email' : 'mobile'; + if ($authType == 'mobile') { + $data['auth'] = MobileNumberHelper::formatMobile($data['auth']); } - public function register(Request $request) - { - $data = $request->validate([ - 'first_name' => 'string', - 'last_name' => 'string', - 'email' => 'email|unique:users|required_without:mobile', - 'password' => 'string|nullable|required_with:email', - 'mobile' => ['string', new MobileNumber, 'nullable', 'unique:users', 'required_without:email'], - ]); + $user = User::where($authType, $data['auth'])->first(); + return response()->json([ + 'user' => $user, + // 'token' => $token, + 'hash'=> trim($data['password']), + 'password' => $user ? $user->password : null, + 'check' => $user ? Hash::check(trim($data['password']), $user->password) : null, - 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'])) { - $data['password'] = Hash::make(config('app.default_password')); - } else { - $data['password'] = Hash::make($data['password']); - } + if (isset($data['email']) && User::where('email', $data['email'])->exists()) { + return response()->json(['message' => 'Email already exists'], 400); + } + $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); - - // Save verification record \DB::table('email_verifications')->insert([ 'user_id' => $user->id, 'code' => $code, @@ -98,16 +90,20 @@ public function register(Request $request) 'created_at' => now(), 'updated_at' => now(), ]); - - // Send verification email \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) { $request->validate([ diff --git a/routes/api.php b/routes/api.php index fcfe42b..6fb8533 100644 --- a/routes/api.php +++ b/routes/api.php @@ -9,6 +9,19 @@ use App\Http\Controllers\BreathingTemplate; use App\Http\Controllers\MoodController; 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::prefix('auth')->controller(UserController::class)->group(function () {