dorv56831 2016-12-15 00:12
浏览 57
已采纳

覆盖laravel登录方法

my problem is - registered users data(username,email,password) stored on remote(other) server. when user tries to login with email and password, request goes to that remote server, and retrieve data from there(with API). the problem is - when i retrieve data, if user exists, i should login him/her in(i don't check anything in database). here's laravel login method

public function login(Request $request)
{
    $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->credentials($request);

    if ($this->guard()->attempt($credentials, $request->has('remember'))) {
        return $this->sendLoginResponse($request);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);


    return $this->sendFailedLoginResponse($request);
}

i already wrote methods i need to retrieve data, but how should i login in after i get that data? should i override login method or maybe there is a better way?

  • 写回答

1条回答 默认 最新

  • dqrb4228 2016-12-15 04:55
    关注

    Here is how to login users manually on 5.2:

    if (/* your condition */) {
        $user = User::where('email', $credentials['email'])->first();
        Auth::login($user);
        return $this->handleUserWasAuthenticated($request, $throttles);
    }
    

    Here is for 5.3:

    if (/* your condition */) {
        $user = User::where('email', $credentials['email'])->first();
        Auth::login($user);
        return $this->sendLoginResponse($request);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?