简体   繁体   English

Laravel 7,仅使用身份验证保护

[英]Laravel 7, use auth guard only

I am using PHP Laravel 7, I'm working on a project for which i have to use authentication from outer source, API's are provided for signup and login.我正在使用 PHP Laravel 7,我正在处理一个我必须使用来自外部源的身份验证的项目,为注册和登录提供了 API。 I just want to know is it possible that i can use the authentication of Laravel and use my custom login and signup and can i use authentication guard for stopping the URL routes.我只是想知道我是否可以使用 Laravel 的身份验证并使用我的自定义登录和注册,并且我可以使用身份验证防护来停止 URL 路由。

I had already created pages for login and register so far but haven't added authentication yet.到目前为止,我已经创建了登录和注册页面,但还没有添加身份验证。

    if (Auth::check()) {
    // The user is logged in...
}

I want something like this for custom login in.我想要这样的自定义登录。

You can receive all the data from your API and then manually create a user and login them.您可以从 API 接收所有数据,然后手动创建用户并登录。

A piece of example from a real application真实应用中的一个例子

$user = User::firstOrCreate(['vk_id' => $data['user_id']]);
auth()->login($user, true); //true is to remember them

After that you can add any data to any field you want:之后,您可以将任何数据添加到您想要的任何字段:

$user->position = 'ceo';
$user->save();

Or any other logic you want.或者你想要的任何其他逻辑。

You can add this logic to your auth register or login controller, or create a separate one.您可以将此逻辑添加到您的身份验证寄存器或登录 controller,或创建一个单独的。 Virtually, built-in Auth controllers are the same controller with their own logic to work with a database and sessions.实际上,内置的 Auth 控制器是相同的 controller,具有自己的逻辑来处理数据库和会话。

But login the user manually using ->login() function.但是使用->login() function 手动登录用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM