feat: login v2
This commit is contained in:
@@ -22,6 +22,82 @@
|
|||||||
use App\Models\UserBreathingSession;
|
use App\Models\UserBreathingSession;
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
|
public function loginV2(Request $request)
|
||||||
|
{
|
||||||
|
$data = $request->validate([
|
||||||
|
'token' => 'required|string',
|
||||||
|
'package_name' => 'required|string',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = Http::acceptJson()->withToken($data['token'])->get('http://localhost:8000/api/status', [
|
||||||
|
'package_name' => $request['package_name']
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!$response->successful()) {
|
||||||
|
return response()->json(['message' => 'invalid token'], 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = json_decode($response->body(), true);
|
||||||
|
|
||||||
|
// First, try to find user by identifier
|
||||||
|
$user = null;
|
||||||
|
if (!empty($response['uuid'])) {
|
||||||
|
$user = User::where('identifier', $response['uuid'])->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not found, try by email
|
||||||
|
if (!$user) {
|
||||||
|
$user = User::where('email', $response['email'])->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
// If user still doesn't exist, create new
|
||||||
|
if (!$user) {
|
||||||
|
$user = User::create([
|
||||||
|
'email' => $response['email'],
|
||||||
|
'identifier' => $response['uuid'] ?? null,
|
||||||
|
'password' => config('app.default_password'),
|
||||||
|
'name' => trim(($response['first_name'] ?? '') . ' ' . ($response['last_name'] ?? '')),
|
||||||
|
'mobile' => $response['mobile'] ?? null,
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
// Only update name and mobile for existing user
|
||||||
|
$user->name = trim(($response['first_name'] ?? '') . ' ' . ($response['last_name'] ?? ''));
|
||||||
|
$user->mobile = $response['mobile'] ?? $user->mobile;
|
||||||
|
$user->save(); // Do NOT update identifier!
|
||||||
|
}
|
||||||
|
|
||||||
|
$access_token = $user->createToken('user')->plainTextToken;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'email' => $user->email,
|
||||||
|
'identifier' => $user->identifier,
|
||||||
|
'token' => $access_token,
|
||||||
|
'user' => $user,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function loginForeginer(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'user_token' => ['required'],
|
||||||
|
'package_name' => ['required', new PackageNameExist],
|
||||||
|
]);
|
||||||
|
$response = $this->repository->userTokenVerify($request['package_name'], $request['user_token']);
|
||||||
|
if ($response['ok']) {
|
||||||
|
$user = User::where('identifier', $response['data']['identifier'])->first();
|
||||||
|
if (empty($user)) {
|
||||||
|
$user = new User();
|
||||||
|
$user['identifier'] = $response['data']['identifier'];
|
||||||
|
$user['name'] = $response['data']['username'];
|
||||||
|
$user->save();
|
||||||
|
}
|
||||||
|
if (!empty($request['fcm_token'])) FcmToken::firstOrCreate(['token' => $request['fcm_token'], 'user_id' => $user['id']]);
|
||||||
|
$access_token = $user->createToken('user')->plainTextToken;
|
||||||
|
return ['identifier' => $user['identifier'], 'token' => $access_token];
|
||||||
|
} else abort(500);
|
||||||
|
}
|
||||||
public function login(Request $request)
|
public function login(Request $request)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
$data = $request->validate([
|
||||||
@@ -356,8 +432,8 @@ public function profile()
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'email' => $user->email,
|
'email' => $user->email,
|
||||||
'first_name' => $user->first_name,
|
'name' => $user->name,
|
||||||
'last_name' => $user->last_name,
|
// 'last_name' => $user->last_name,
|
||||||
'mobile'=> $user->mobile,
|
'mobile'=> $user->mobile,
|
||||||
'birthday' => $user ->birthday,
|
'birthday' => $user ->birthday,
|
||||||
'xp' => $user->xp,
|
'xp' => $user->xp,
|
||||||
|
|||||||
+1
-2
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\HasUuid;
|
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use EloquentFilter\Filterable;
|
use EloquentFilter\Filterable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
@@ -13,7 +12,7 @@
|
|||||||
|
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
use HasApiTokens, HasFactory, Notifiable, HasUuid, Filterable;
|
use HasApiTokens, HasFactory, Notifiable, Filterable;
|
||||||
|
|
||||||
const GENDERS = [
|
const GENDERS = [
|
||||||
'male' => 1,
|
'male' => 1,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
trait HasUuid
|
trait HasUuid
|
||||||
{
|
{
|
||||||
public static function bootHasUUID(): void
|
public static function bootHasUuid(): void
|
||||||
{
|
{
|
||||||
static::creating(function ($model) {
|
static::creating(function ($model) {
|
||||||
if (!$model->uuid) {
|
if (!$model->uuid) {
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->string('identifier')->nullable()->unique();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
//
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
if (Schema::hasColumn('users', 'first_name')) {
|
||||||
|
$table->dropColumn('first_name');
|
||||||
|
}
|
||||||
|
if (Schema::hasColumn('users', 'last_name')) {
|
||||||
|
$table->dropColumn('last_name');
|
||||||
|
}
|
||||||
|
if (Schema::hasColumn('users', 'avatar')) {
|
||||||
|
$table->dropColumn('avatar');
|
||||||
|
}
|
||||||
|
if (Schema::hasColumn('users', 'uuid')) {
|
||||||
|
$table->dropColumn('uuid');
|
||||||
|
}
|
||||||
|
if (Schema::hasColumn('users', 'is_admin')) {
|
||||||
|
$table->dropColumn('is_admin');
|
||||||
|
}
|
||||||
|
if (Schema::hasColumn('users', 'wallet')) {
|
||||||
|
$table->dropColumn('wallet');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
if (!Schema::hasColumn('users', 'name')) {
|
||||||
|
$table->string('name')->nullable()->after('id');
|
||||||
|
}
|
||||||
|
if (!Schema::hasColumn('users', 'identifier')) {
|
||||||
|
$table->string('identifier')->unique()->after('mobile');
|
||||||
|
}
|
||||||
|
if (!Schema::hasColumn('users', 'remember_token')) {
|
||||||
|
$table->rememberToken();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
if (Schema::hasColumn('users', 'name')) {
|
||||||
|
$table->dropColumn('name');
|
||||||
|
}
|
||||||
|
if (Schema::hasColumn('users', 'identifier')) {
|
||||||
|
$table->dropColumn('identifier');
|
||||||
|
}
|
||||||
|
if (Schema::hasColumn('users', 'remember_token')) {
|
||||||
|
$table->dropColumn('remember_token');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Schema::hasColumn('users', 'first_name')) {
|
||||||
|
$table->string('first_name')->nullable();
|
||||||
|
}
|
||||||
|
if (!Schema::hasColumn('users', 'last_name')) {
|
||||||
|
$table->string('last_name')->nullable();
|
||||||
|
}
|
||||||
|
if (!Schema::hasColumn('users', 'avatar')) {
|
||||||
|
$table->string('avatar')->nullable();
|
||||||
|
}
|
||||||
|
if (!Schema::hasColumn('users', 'uuid')) {
|
||||||
|
$table->uuid('uuid')->unique();
|
||||||
|
}
|
||||||
|
if (!Schema::hasColumn('users', 'is_admin')) {
|
||||||
|
$table->boolean('is_admin')->default(false);
|
||||||
|
}
|
||||||
|
if (!Schema::hasColumn('users', 'wallet')) {
|
||||||
|
$table->unsignedBigInteger('wallet')->default(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
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 () {
|
||||||
|
Route::post('login-with-approo-v2', [UserController::class, 'loginV2']);
|
||||||
Route::middleware(['throttle:3,1'])->post('login-otp', 'loginOTP');
|
Route::middleware(['throttle:3,1'])->post('login-otp', 'loginOTP');
|
||||||
Route::post('check-otp', 'checkOTP');
|
Route::post('check-otp', 'checkOTP');
|
||||||
Route::post('verify-email-code','verifyEmailCode');
|
Route::post('verify-email-code','verifyEmailCode');
|
||||||
|
|||||||
Reference in New Issue
Block a user