Laravel external Authentication

牧云@^-^@ 提交于 2019-12-11 16:44:10

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!