Search code examples
phplaravelauthenticationlaravel-5crypt

Laravel - problem with Crypt::decrypt, wrong syntax in code


I know that my solution is wrong, but can someone help me with correct syntax how decrypt my string and log in ?

Laravel API resived code and store encrypted in Database

api route

Route::post('data','DataImport@Insert');

controller

use Illuminate\Support\Facades\Crypt;

function Insert(Request $request)
{
    $user=new User();
     $user->code=Crypt::encrypt($request->input('code'));
    return $user->save();
}

Auth\Controller

use Illuminate\Support\Facades\Crypt;

public function postLogin(Request $request)
{
    request()->validate([
        'id_message' => 'required',
        'code' => 'required',
    ]);
   
    $credentials = $request->only('id_message', 'code');
    $decrypted = $request->input('code'); 
    $request->session()->flash('success');

    if ($user=User::where($credentials)->first()) {
        if (\Crypt::decrypt($user->code) == $decrypted) {
            Auth::login($user);
            
        return redirect()->intended('dashboard')->with('success');
        }
    }
    //dd($decrypted);

    return Redirect::back()->with('error');
}

dd($decrypted);

 array:1 [▼
 "code" => "123456"
]

decrypted code is in correct value. No error in laravel.log only message code does not match.

thanks for any help.

(solution with hash, dont help me with this problem)


Solution

  • If someone has same problem. @lagbox help me with incorrect syntax with input, and reference link below did final solution:

    replace this:

    $credentials = $request->only('id_message', 'sms_code');
    

    to:

    $user = User::where('id_message', $request->input('id_message'))->first();
    

    and if statment to:

    if ($user) {
        if (\Crypt::decrypt($user->code) == $decrypted) {
            Auth::login($user);
            return redirect()->intended('dashboard')->with('success');
            }
        }
        return Redirect::back()->with('error');
    }    
    

    reference link: https://stackoverflow.com/a/50891573/3681759