LoginController无法登录

时间:2019-03-23 19:52:43

标签: php laravel

我正在尝试根据API响应登录用户。如何设置控制器以登录用户?

我已经阅读了文档,但是没有解释控制器中的登录功能是如何被实际调用或经过身份验证的功能如何工作的,只是在控制器中找到了使用这些功能的文章。

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Library\API;
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    public function username()
    {
        return 'name';
    }
    public function login(Request $request)
    {
        $APIresponse = API::login($request->all());
        if ($APIresponse->status !== 'logged_in')  return $this->sendFailedLoginResponse($request);
        return $this->authenticated($request, auth()->user());
    }

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');

    }
}

某人登录后,应将其重定向到主页,其名称应显示在右上角。不幸的是,即使我返回“主页”视图,也不会显示其名称,并且用户实际上也没有登录(返回到/ home将会再次显示登录页面)。

edit:视图是php artisan make:auth附带的默认视图

1 个答案:

答案 0 :(得分:0)

public function login(Request $request)
{
    $APIresponse = API::login($request->all());
    if ($APIresponse->status !== 'logged_in')  return $this->sendFailedLoginResponse($request);
    $this->attemptLogin($request);
    return redirect()->intended($this->redirectPath());
}

这实际上将登录用户。