feat: register & login
This commit is contained in:
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,35 +29,25 @@ public function login(Request $request)
|
|||||||
'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'])) {
|
|
||||||
$authType = 'mobile';
|
|
||||||
$data['auth'] = MobileNumberHelper::formatMobile($data['auth']);
|
$data['auth'] = MobileNumberHelper::formatMobile($data['auth']);
|
||||||
} else {
|
|
||||||
return response()->json([
|
|
||||||
'message' => 'wrong credentials'
|
|
||||||
], 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$user = User::where($authType, $data['auth'])->first()) {
|
$user = User::where($authType, $data['auth'])->first();
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'wrong credentials'
|
'user' => $user,
|
||||||
], 404);
|
// 'token' => $token,
|
||||||
|
'hash'=> trim($data['password']),
|
||||||
|
'password' => $user ? $user->password : null,
|
||||||
|
'check' => $user ? Hash::check(trim($data['password']), $user->password) : null,
|
||||||
|
|
||||||
|
]);
|
||||||
|
if (!$user || !Hash::check(trim($data['password']), $user->password)) {
|
||||||
|
return response()->json(['message' => 'wrong credentials'], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Hash::check($data['password'], $user->password)) {
|
$token = $user->createToken('token', $user->is_admin ? ['admin'] : ['user'])->plainTextToken;
|
||||||
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([
|
return response()->json([
|
||||||
'user' => $user,
|
'user' => $user,
|
||||||
@@ -65,32 +55,34 @@ public function login(Request $request)
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function register(Request $request)
|
public function register(Request $request)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
$data = $request->validate([
|
||||||
'first_name' => 'string',
|
'first_name' => 'string|nullable',
|
||||||
'last_name' => 'string',
|
'last_name' => 'string|nullable',
|
||||||
'email' => 'email|unique:users|required_without:mobile',
|
'email' => 'email|required_without:mobile',
|
||||||
'password' => 'string|nullable|required_with:email',
|
'password' => 'string|nullable|required_with:email',
|
||||||
'mobile' => ['string', new MobileNumber, 'nullable', 'unique:users', 'required_without:email'],
|
'mobile' => ['string', new MobileNumber, 'nullable', 'required_without:email'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (isset($data['mobile'])) {
|
if (isset($data['mobile'])) {
|
||||||
$data['mobile'] = MobileNumberHelper::formatMobile($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([
|
return response()->json([
|
||||||
'message' => 'User registered successfully. Verification code sent to email.',
|
'message' => 'User registered successfully.',
|
||||||
'user' => $user,
|
'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([
|
||||||
|
|||||||
@@ -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 () {
|
||||||
|
|||||||
Reference in New Issue
Block a user