Laravel外部认证 [英] Laravel external Authentication

查看:50
本文介绍了Laravel外部认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个可验证的宁静API进行身份验证,如果验证成功,它将为我提供用户名,然后我使用该用户名并尝试登录到我的Laravel应用.当我完成登录后,它似乎可以工作了.但是,当我浏览仍通过访客身份验证的网站时,似乎并没有保持会话状态.

I do my authentication via a restful API which I validate, if its successful it gives me the username and i take that and try to login to my Laravel app. When I finish logging in, it seems to work. However, It doesn't seem to be keeping session as I navigate around the site I am still authenticated as a guest.

public function login(){
    // I do a guzzle request that gives me the value of $response['username']
    $Username = $response['username'];
    $user = User::where('username','thomc')->first();
    Auth::login($user);
    return view('home');
}

这是我的用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    protected $table = 'UserDetails';
}

我的桌子:

  • 用户名
  • 电子邮件
  • 名字
  • 姓氏
  • BA
  • Facebook_ID
  • Google_ID

推荐答案

不确定这是否是测试更改,但是您没有使用$ Username,而是搜索用户thomc.要注意的另一件事是检查数据库中是否存在具有用户名的用户,否则返回的$ user将为null.尝试使用ID登录.

Not sure if it's a test change, but you're not using the $Username, instead searching for the user thomc. Another thing to note would be check if the user with username exists in the database, if not the $user returned would be null. Try login with id as well.

$username = $response['username'];
$user = User::where('username', $username)->firstOrFail();
Auth::loginUsingId($user->id);

还要确保所有路由都用web中间件包装,并根据需要在路由上使用适当的authguest中间件.

Also make sure all the routes are wrapped with the web middleware, and appropriate auth and guest middleware on routes as needed.

这篇关于Laravel外部认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆