Laravel external Authentication

2019-09-14 19:00发布

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
  • Email
  • First_Name
  • Last_Name
  • BA
  • Facebook_ID
  • Google_ID

1条回答
甜甜的少女心
2楼-- · 2019-09-14 19:36

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.

查看更多
登录 后发表回答