mercredi 12 octobre 2016

Laravel Authentication not saving session

I have a fresh installed Laravel 5.3 project.

I chose not to use the default authentication controllers.

I made this controller to handle user authentication:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use App\Http\Requests\CreateSessionRequest;

class SessionController extends Controller
{

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        $this->middleware('guest');

        var_dump( Auth::user() );

        return view('session.create');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateSessionRequest $request)
    {

        $this->middleware('guest');

        if ($this->guard()->attempt($request->only(['email', 'password']))) {

            dd(Auth::user());

        }
        else return redirect()->back()->withInput()->withErrors(['email' => 'Login invalid']);

    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard();
    }

}

The issue is that the sessions are not getting saved. The authentication is happening, and the dd(Auth::user()) in the store function is showing the correct information of the user.

But when i navigate back to the create function, Auth::user() is returning null.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire