2019年5月27日月曜日

laravel Authentication

ログイン後のredirect先
LoginController, RegisterController, ResetPasswordController, and  VerificationController:

protected $redirectTo = '/';

modify the RedirectIfAuthenticated middleware's handle method to use your new URI when redirecting the user.

you may access the authenticated user via the Auth facade:

// Get the currently authenticated user...
$user = Auth::user(); // auth()->user()

// Get the currently authenticated user's ID...
$id = Auth::id(); // auth()->id()

public function update(Request $request)
    {
        // $request->user() returns an instance of the authenticated user...
    }

if (Auth::check()) { // auth()->check()  auth()->guest()
    // The user is logged in...
}


Route::get('profile', function () {
    // Only authenticated users may enter...
})->middleware('auth');

If you are using controllers, you may call the middleware method from the controller's constructor instead of attaching it in the route definition directly:

public function __construct()
{
    $this->middleware('auth');
}

Manually Authenticating Users

public function authenticate(Request $request)
    {
        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
The intended method on the redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware. A fallback URI may be given to this method in case the intended destination is not available.

Auth::logout();

Remembering Users

Your users table must include the string remember_token column, which will be used to store the "remember me" token.

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
    // The user is being remembered...
}
If you are "remembering" users, you may use the viaRemember method to determine if the user was authenticated using the "remember me" cookie:

if (Auth::viaRemember()) {
    //
}

0 件のコメント:

コメントを投稿