jeudi 10 septembre 2020

How to login with phone number and country code in Laravel

I have a login page with email id and password . Now in the API I received country code and phone number along with the password. My code is below.

        if(!isset($request->phone)){
        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string',
            'remember_me' => 'boolean',
        ]);
        $credentials = request(['email', 'password']);

    } else {
        $request->validate([
            'phone' => 'required|string',
            'country_code' => 'required',
            'password' => 'required|string',
            'remember_me' => 'boolean',
        ]);
        $credentials = request(['phone', 'country_code', 'password']);

    }
    if(isset($request->phone)){
        $user = User::where('phone',$credentials['phone'])->where('country_code',$credentials['country_code'])->first();
        if(!Hash::check($credentials['password'], $user->password)){
            return response()->json([
                'message' => 'Unauthorized'
            ], 401);            
        }
    } else {
        if (!Auth::attempt($credentials)) {
            return response()->json([
                'message' => 'Unauthorized'
            ], 401);
        }
        $user = $request->user();
    }
    return $user->only(['id', 'name', 'email', 'api_token']);

How do I login with phone number and country code? And also Where does the Auth:attempt resides so that I can copy it and use it in same fashion



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire