lundi 11 mars 2019

Set authenticate stuff manually in laravel 5.7

I'm working on a laravel 5.7 project and I want to have my own authentication scenario.

Well, I'll give a mobile from my user and send her a one time pass to her phone and then check if she is giving me the correct code.

So, I do not use laravel authentication system at this point at all.

My Controller is something like this :

    /*
     * Show Login Form
     */
    public function showLoginForm()
    {
        return view('auth.custom.login');
    }


    /*
     * Login
     */
    public function login(Request $request)
    {
        $mobile = $request->mobile;


        $this->validate($request, [
            'mobile' => 'iran_mobile|required'
        ]);

        $check = User::where('mobile', $mobile)->first();

        if( $check === null )
        {
            Session::flash('toasterr', 'is not registered yet');
            Session::put('mobile', $mobile);
            return redirect(route('register'));
        }
        else
        {
            $singleTimePass = Str::random(4);
            sendSms($mobile, 'your code:' . PHP_EOL . $singleTimePass . PHP_EOL . 'Insert that bla bla');
            Session::put('singleTimePass', $singleTimePass);
            Session::put('mobile', $mobile);

            return redirect(route('check_pass'));
        }

        dd($check);

    }



    /*
     * Show Check Pass page
     */
    public function showCheckPass()
    {
        return view('auth.custom.pass');
    }



    /*
     * Check Pass For Login
     */
    public function checkPassForLogin(Request $request)
    {
        $this->validate($request, [
            'pass' => 'required|regex:/^[\w-]*$/'
        ]);

        if( $request->pass == Session::get('singleTimePass'))
        {
            $user = User::where('mobile', Session::get('mobile'))->first();
//            dd($user->id);
            Auth::login($user->id);
            return redirect(route('game'));
        }
        else
        {
            Session::flash('toasterr', 'pass is wrong');
            return redirect(route('check_pass'));
        }
    }


    /*
     * Show Register Form
     */
    public function showRegisterForm()
    {
        return view('auth.custom.register');
    }

    /*
     * Register
     */
    public function register(Request $request)
    {
        $this->validate($request, [
            'name' => 'persian_alpha|required',
            'family' => 'persian_alpha|required',
            'username' => 'required|min:4|max:255|string',
            'mobile' => 'iran_mobile|required',
        ]);

        return $request->all();
    }

Ok! Every thing seems to be good but now, I expect laravel that give me abilities like Auth::check() or Auth::user() and...

So I know that I have an error at this line: Auth::login($user->id); and I want to know how can I do something like this manually for mentioned goal.

May be it is because of my poor knowledge about laravel authentication architecture but it would be appreciate if you let me know how do that because googled this for a while and there's not direct answer to this question-or I did not searched enough-.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire