问题
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');
}
This is my User model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'UserDetails';
}
My table:
- Username
- First_Name
- Last_Name
- BA
- Facebook_ID
- Google_ID
回答1:
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);
Also make sure all the routes are wrapped with the web
middleware, and appropriate auth
and guest
middleware on routes as needed.
来源:https://stackoverflow.com/questions/44021077/laravel-external-authentication