Search
 
SCRIPT & CODE EXAMPLE
 

PHP

AuthController

namespace AppHttpControllers;

use IlluminateSupportFacadesAuth;
use IlluminateHttpRequest;
use AppModelsUser;
use IlluminateSupportFacadesHash;

class AuthController extends Controller
{
   //-----------------------------register users ---------------------------------
   public function register(Request $request)
   {
       $password = password_hash($request->password, PASSWORD_DEFAULT);
       $users_count= User::where('username',$request->username)->get();
       $email_count= User::where('email',$request->email)->get();

       $credentials = request(['username', 'password']);
       dd(auth()->attempt($credentials));
       
       if(count($users_count)>0){
           $data = [
               'status' => 'username found',
           ];
       }
       else {
           if (count($email_count)>0){
               $data = [
                   'status' => 'email is found',
               ];
           }
           else {
               $user = User::create(
                   [
                       'username'=>$request->username,
                       'password'=>$password,
                       'email'=>$request->email,
                       'fullname'=>$request->fullname,
                       'natId'=>$request->nat_id,
                       'role'=>'user'
                   ]);

               if($user){
                   $credentials = request(['username', 'password']);

                   if (! $token = auth()->attempt($credentials)) {
                       return response()->json(['error' => 'Unauthorized'], 401);
                   }
                   else{
                   $data = [
                       'username'=>$request->username,
                       'password'=>$password,
                       'email'=>$request->email,
                       'fullname'=>$request->fullname,
                       'natId'=>$request->nat_id,
                       'status' => 'success',
                       'token' => $token
                   ];}
               }
               else{
                   $data = [
                       'status' => 'failed',
                   ];
               } 
           }
       }
       return $data;  
   }
} 
Comment

PREVIOUS NEXT
Code Example
Php :: woocommerce unset custom checkout field 
Php :: guarded and fillable in laravel 
Php :: php remove non printable characters 
Php :: php get function from different file 
Php :: codeigniter 4 radio button isset 
Php :: php capture include 
Php :: php edit link 
Php :: php strip url of all invalid characters 
Php :: php filter_var name 
Php :: audio validation in jquery validation 
Php :: laravel get url parameter value in controller 
Php :: entrust laravel 
Php :: yii2 activeform adding field css class 
Php :: static function php 
Php :: limit wordpress search to title 
Php :: check if config exist laravel 
Php :: 20 usd to php 
Php :: laravel 7 upload file s3 
Php :: laravel return from db reorder 
Php :: laravel query when 
Php :: laravel create model for existing table 
Php :: laravel validation on update 
Php :: blade check if variable exists 
Php :: How to make a simple mail system in Laravel without view or notification 
Php :: laravel zoom integration 
Php :: connexion à la base de donnée microsoftsqlserver avec php 
Php :: dompdf php 8 
Php :: many to many relationship laravel 
Php :: wp post view 
Php :: netchain media 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =