Laravel :How to authenticated user login with mobile number only and create jwt token for auth data

2k Views Asked by At

I am creating an API for my application using JWT. It is for different table not for users table. How can I authenticate login user with another table for exapmle students table and create jwt token.

For the User table user authentication function which is working, & its is as below:

$credentials = $request->only(['email', 'password']);
    try {
        $token = Auth::guard()->attempt($credentials);
        if(!$token) {
            return response()->json([
                'message'       => "Email and password do not match",
                'status_code'   => 204,
            ]);
        }
        $user = Auth::user();
        if($user->status == "Inactive") {
            return response()->json([
                'message'       => "User ID is disabled",
                'status_code'   => 403,
            ]);
        }
        $user->last_login = Carbon::now();
        $user->save();
        $user = Auth::user();
        $user->UserDeviceData()->firstOrCreate([
            'device_id'     => $request->device_id,
            'device_type'   => $request->device_type ? $request->device_type : "ios",
        ]);

        return (new UserTransformer)->transform($user,[
            'request_type'  => 'login',
            'token'         =>  $token
        ]);

    } catch (JWTException $e) {
        return response()->json([
            'message'       => "Internal server error",
            'status_code'   => 500,
        ]);
    } 

And here is the OTP verification function and its code look like :

public function verify(Request $request)
{
    

    try
    {
        $token              = config('app.TWILIO_AUTH_TOKEN');
        $twilio_sid         = config('app.TWILIO_SID');
        $twilio_verify_sid  = config('app.TWILIO_VERIFY_SID');
        $twilio             = new Client($twilio_sid, $token);
        $phoneNumber        = $request->get('phone_number');
        $verification       = $twilio->verify->v2->services($twilio_verify_sid)->verificationChecks->create($request->get('verification_code'), array('to' => $request->get('phone_number')));
        if ($verification->valid) {
            // Updating table student table that the number is verified & want to return JWT token for authenticate user
            
            return response()->json([
                "status_code"   => 200, 
                "message"       => "Phone number verified."
            ]);
        }
        return response()->json([
            "status_code"   => 200, 
            "message"       => "Verification failed."
        ]);

    }catch (Exception $e){
        return response()->json([
            'response' => [
                'code'          => 401,
                'message'       => "Unable to verify OTP.Please try again.",
            ],
        ]);
    }   

    
}

How can I authenticate the verified user which is on another table.

0

There are 0 best solutions below