- 为了防止将来出现一些不可描述的问题, 设计数据库时, 请先确保手机号,邮箱等字段有惟一约束
Schema::connection(self::connection)->table('users', function (Blueprint $table) { $table->unique('mobile'); $table->unique('email'); });
- 在用户模型中增加以下方法
/* * 参考: * Laravel\Passport\Bridge\UserRepository@getUserEntityByUserCredentials * * https://www.jianshu.com/p/12d06fc201af * https://segmentfault.com/a/1190000010499813#articleHeader0 * https://blog.csdn.net/woqianduo/article/details/81782799 */ public function findForPassport(string $username) { if(starts_with($username, 'uid')) return $this->find(substr($username, 3)); return $this->orWhere('mobile', $username) ->orWhere('email', $username)->first(); } public function getAuthPassword() { return $this->password; }
- config目录下的auth.php文件
guards.api.driver => passport;
providers.users.model => 你的用户模型
- 如何登录
这个时候就可以使用 http(s)://{域名}:{端口}/oauth/token获取token了(注意是POST请求)