douqie1816 2017-12-27 20:36
浏览 43
已采纳

根据值Laravel登录用户

I am verifying user's account via email and I want to redirect the user directly to home page after account is verified.

The issue I am having is that I am not sure how to actually log in the user using login function.

class VerificationController extends Controller {

    public function verify($token){ 

        User::where('email_token',$token)->firstOrFail()->verified();
        // auth()->login($user); works if $user exists
        return redirect('/home');
    }
}   

Can I log in the user based on the email_token? I tried but it doesn't seem to work as expected.

  • 写回答

2条回答 默认 最新

  • dsk920417 2017-12-27 23:03
    关注

    You are on the right way. You just need to get the User instance and pass it to the login Method of the Auth class. I've made an example controller for you to show how this could be done.

    class VerificationController extends Controller 
    {
        public function verify($token)
        {
            // Fetch the user by the email token from the database.
            // #firstOrFail returns the first matching user or aborts 
            // the request with a 404 error.
            $user = User::where('email_token', $token)->firstOrFail();
    
            // Activate your user or whatever this method does.
            $user->verified();
    
            // Logs the Client who did this web request into the
            // User account fetched above in.
            Auth::login($user);
    
            // Redirect to wherever you want.
            return redirect('/home');
        }
    }
    

    Read more about authenticating users in the official documentation:
    https://laravel.com/docs/authentication#other-authentication-methods

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭