使用 Laravel 5 登录/注册 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40357637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:41:14  来源:igfitidea点击:

Login / Register API With Laravel 5

apilaravel

提问by Khirad Banu

How do I can create an API with laravel 5, That will be used for login /register for mobile app? Is there any useful tutorial for this type of API.

如何使用 Laravel 5 创建一个 API,它将用于登录/注册移动应用程序?是否有针对此类 API 的任何有用教程。

Like when someone sends a request from a mobile application, I need to authenticate and then send the proper response against that request.

就像有人从移动应用程序发送请求一样,我需要进行身份验证,然后针对该请求发送正确的响应。

回答by spicydog

I've made a very simple version in jQuery format as for example and hosted on GitHub.

例如,我以 jQuery 格式制作了一个非常简单的版本,并托管在 GitHub 上。

Here are all the changes from a fresh Laravel: See all changed files

以下是全新 Laravel 的所有更改查看所有更改的文件

Feel free to adapt to your code.

随意适应您的代码。

You can check the step in each commit.

您可以检查每个提交中的步骤。

  1. Create the project(Don't forget to run php artisan make:auth)

  2. Make login support AJAX request(instead of auto redirect) 1a47de4

  3. Login with AJAX(submit login data via AJAX, Please CSRF tokenheader) 80be34d

  4. Make register support AJAX(same as step 2) 89f6dd7

  5. Register via AJAX(Same as step 3) ab8d2d7

  6. Use redirect path from Laravel(Bonus) 27b9a1c4

  1. 创建项目(不要忘记运行php artisan make:auth

  2. 使登录支持 AJAX 请求(而不是自动重定向)1a47de4

  3. 使用 AJAX登录(通过 AJAX 提交登录数据,请CSRF token标头)80be34d

  4. 使寄存器支持 AJAX(同步骤 2)89f6dd7

  5. 通过 AJAX 注册(同步骤 3)ab8d2d7

  6. 使用来自 Laravel 的重定向路径(奖励)27b9a1c4

By the way, for the mobile app, you may need to use access tokeninstead. My example is for web app session. Laravel already have the particular method already just twist a bit.

顺便说一句,对于移动应用程序,您可能需要access token改用。我的例子是网络应用程序会话。Laravel 已经有了特定的方法,只是稍微扭曲了一下。

回答by Gayan Kavirathne

If you need to create a login register with laravel you can simply use the Heto Multi auth.. All the required details are there.. Heto Multi Auth

如果您需要使用 laravel 创建登录注册,您可以简单地使用 Heto Multi auth .. 所有必需的详细信息都在那里.. Heto Multi Auth

Hope This will be helpful

希望这会有所帮助

回答by Dawlatzai Ghousi

Creating a REST Api on Laravel isn't very difficult. All we need to keep in mind is that where to start and how to prepare RestFull resources. As usual, you can follow this link: Build Rest Resources With Laravel

在 Laravel 上创建 REST Api 并不是很困难。我们需要记住的是从哪里开始以及如何准备 RestFull 资源。像往常一样,您可以点击此链接:使用 Laravel 构建休息资源

And if you want that your APIworks with Mobile Applicationsyou should use tymon/jwt-authpackage. you can find the tutorial to work with laraveland tymon/jwt-authin this link: Create an API Server for Mobile Apps using Laravel

如果你想要你的API作品,Mobile Applications你应该使用tymon/jwt-authpackage.json 。你可以找到的教程工作,laraveltymon/jwt-auth在此链接:创建使用Laravel针对移动应用的API服务器

The tymon/jwt-authcontains:

其中tymon/jwt-auth包含:

  1. Installation of the package.
  2. Configuration
  3. Creating Tokens
  4. Authentication
  1. 安装包。
  2. 配置
  3. 创建令牌
  4. 验证

And the link for tymon/jwt-authpackage is: tymon/jwt-auth package

tymon/jwt-auth包的链接是: tymon/jwt-auth package

回答by Saumya Rastogi

There is a popular tool named - Dingo APIwhich is specially created for Laravel / Lumen Frameworks and is meant to provide you, a set of tools to help you easily and quickly build your own APIs.

有一个名为Dingo API的流行工具,它是专门为 Laravel / Lumen 框架创建的,旨在为您提供一组工具来帮助您轻松快速地构建自己的 API。

The package provide you with these set of features:

该软件包为您提供了以下功能集:

  • Content Negotaition
  • Authentication
  • API Versioning
  • Throttling
  • Formated / Transformed Responses
  • API Blueprint Documentation
  • and much more.
  • 内容协商
  • 验证
  • API 版本控制
  • 节流
  • 格式化/转换响应
  • API 蓝图文档
  • 以及更多。

You can find the full documentation of Dingo API here >>

您可以在此处找到Dingo API完整文档 >>

回答by Noitidart

Here is a solution that shows how to set up register/login/logout endpoints but he changes it slighlty so we don't get the nice effects of too many login attempts etc. https://www.toptal.com/laravel/restful-laravel-api-tutorial

这是一个解决方案,展示了如何设置注册/登录/注销端点,但他对其进行了轻微更改,因此我们不会获得太多登录尝试等的良好效果。https://www.toptal.com/laravel/restful -laravel-api-tutorial

I am still looking for a tutorial on how to do this, will share when I find.

我仍在寻找有关如何执行此操作的教程,当我找到时将分享。