Laravel Auth: versuch () wird die Anmeldung nicht fortsetzen
Habe ich gefunden, dass viele online-Ressourcen mit ähnlichen Problemen, aber keine Lösungen angezeigt, um mein problem zu lösen.
Wenn ich das log ein Benutzer mit dem folgenden code, scheint alles in Ordnung zu sein:
$email = Input::get('email');
$password = Input::get('password');
if (Auth::attempt(array('email' => $email, 'password' => $password))) {
return Auth::user();
} else {
return Response::make("Invalid login credentials, please try again.", 401);
}
Den Auth::attempt()
Funktion gibt true
und der angemeldete Benutzer an den client zurückgegeben wird mit Auth::user()
.
Aber wenn der client macht eine Anfrage an den server direkt nach Auth::user()
zurück NULL
.
Habe ich bestätigt, dass Laravel Sitzungen sind ordnungsgemäß funktioniert, indem Sie die Session::put()
und Session::get()
erfolgreich.
Update
Auf die weitere Untersuchung scheint es, dass Sitzungen nicht beibehalten! Könnte dies etwas damit zu tun, dass die AngularJS web-app-server über app.mydomain.com und das Laravel-API bedient, über api.mydomain.com?
Meine Benutzer-Modell ist wie folgt:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Mein auth config ist wie folgt:
<?php
return array(
/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/
'driver' => 'eloquent',
/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => 'User',
/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'table' => 'users',
/*
|--------------------------------------------------------------------------
| Password Reminder Settings
|--------------------------------------------------------------------------
|
| Here you may set the settings for password reminders, including a view
| that should be used as your password reminder e-mail. You will also
| be able to set the name of the table that holds the reset tokens.
|
| The "expire" time is the number of minutes that the reminder should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
Die migration zum erstellen der users
Tabelle ist wie folgt:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->string('first_name');
$table->string('last_name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function(Blueprint $table)
{
//
});
}
}
Sowie die session-config:
<?php
return array(
/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------
|
| This option controls the default session "driver" that will be used on
| requests. By default, we will use the lightweight native driver but
| you may specify any of the other wonderful drivers provided here.
|
| Supported: "file", "cookie", "database", "apc",
| "memcached", "redis", "array"
|
*/
'driver' => 'database',
/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/
'lifetime' => 120,
'expire_on_close' => false,
/*
|--------------------------------------------------------------------------
| Session File Location
|--------------------------------------------------------------------------
|
| When using the native session driver, we need a location where session
| files may be stored. A default has been set for you but a different
| location may be specified. This is only needed for file sessions.
|
*/
'files' => storage_path().'/sessions',
/*
|--------------------------------------------------------------------------
| Session Database Connection
|--------------------------------------------------------------------------
|
| When using the "database" or "redis" session drivers, you may specify a
| connection that should be used to manage these sessions. This should
| correspond to a connection in your database configuration options.
|
*/
'connection' => null,
/*
|--------------------------------------------------------------------------
| Session Database Table
|--------------------------------------------------------------------------
|
| When using the "database" session driver, you may specify the table we
| should use to manage the sessions. Of course, a sensible default is
| provided for you; however, you are free to change this as needed.
|
*/
'table' => 'sessions',
/*
|--------------------------------------------------------------------------
| Session Sweeping Lottery
|--------------------------------------------------------------------------
|
| Some session drivers must manually sweep their storage location to get
| rid of old sessions from storage. Here are the chances that it will
| happen on a given request. By default, the odds are 2 out of 100.
|
*/
'lottery' => array(2, 100),
/*
|--------------------------------------------------------------------------
| Session Cookie Name
|--------------------------------------------------------------------------
|
| Here you may change the name of the cookie used to identify a session
| instance by ID. The name specified here will get used every time a
| new session cookie is created by the framework for every driver.
|
*/
'cookie' => 'laravel_session',
/*
|--------------------------------------------------------------------------
| Session Cookie Path
|--------------------------------------------------------------------------
|
| The session cookie path determines the path for which the cookie will
| be regarded as available. Typically, this will be the root path of
| your application but you are free to change this when necessary.
|
*/
'path' => '/',
/*
|--------------------------------------------------------------------------
| Session Cookie Domain
|--------------------------------------------------------------------------
|
| Here you may change the domain of the cookie used to identify a session
| in your application. This will determine which domains the cookie is
| available to in your application. A sensible default has been set.
|
*/
'domain' => null,
/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------
|
| By setting this option to true, session cookies will only be sent back
| to the server if the browser has a HTTPS connection. This will keep
| the cookie from being sent to you if it can not be done securely.
|
*/
'secure' => false,
);
Irgendwelche Ideen?
InformationsquelleAutor der Frage Leon Revill | 2014-02-06
Du musst angemeldet sein, um einen Kommentar abzugeben.
Hatte ich dieses problem. Ändern der primären Schlüssel für die Benutzer-Modell half bei mir.
Versuchen, fügen Sie so etwas wie
in class User{} (app/models/User.php)
(Feld
user_id
ist auto-increment Schlüssel in meinem Schema für Tabelle 'users')Siehe auch dieses ticket: https://github.com/laravel/framework/issues/161
InformationsquelleAutor der Antwort Juljan
Ich hatte dieses problem heute morgen, und ich erkannte, dass, wenn Sie eine Ausgabe der Daten vor dem Aufruf
Dann können Sie SICHER SEIN, DASS IHRE SITZUNG WIRD NICHT FESTGELEGT.
So zum Beispiel, wenn Sie etwas tun, wie
knapp oberhalb der Linie, die sagt
Dann seien Sie versichert, dass Sie verbringen den ganzen Vormittag versucht zu finden, warum laravel ist nicht die Persistenz des authentifizierten Benutzers und ruft
geben Sie eine null, und auch den Aufruf
wird Ihnen immer die falschen.
Dies war mein problem und das ist, wie ich es behoben, durch das entfernen der echo-Anweisung.
InformationsquelleAutor der Antwort Moses Ndeda
Versuchen vorbei
true
Auth:Versuch() für dieremember
parameter:Ich bin mir nicht sicher, warum Sie zurückgeben Auth::user() an alle obwohl.
InformationsquelleAutor der Antwort TonyArra
Ich hatte ein ähnliches Problem, und am Ende war ich so konzentriert auf die back-end -, dass ich nicht der Meinung das problem sein könnte, auf dem front-end.
Würde ich Klinge-Ausgang
Auth:logout()
direkt an das front-end zu erstellen, die einen logout-button, etwa so:Was falsch ist. Jedes mal, wenn ich bei der Anwendung angemeldet, wäre ich zu einer Seite geleitet mit dieser Taste auf, die ich irrtümlich dachte, Sie nennen würde
Auth::logout()
wenn Sie gedrückt werden. Natürlich, das PHP gerendert wird auf pageload undAuth::logout()
heißt sofort. Dann navigiert ein Benutzer zu einer anderen Seite, da haben Sie ausgeloggt sind Sie weitergeleitet auf die login-Seite starten Sie den Prozess erneut.FYI - Der richtige Weg, um erstellen Sie einen logout-button, wenn Sie mit der default-Auth-route-controller, wäre die direkte route '/auth/logout', etwa so:
<a href="{{url('/auth/logout')}}">Log Out</a>
InformationsquelleAutor der Antwort TimothyBuktu
Das Problem möglicherweise mit Ihrer session-Konfiguration. Überprüfen Sie, wenn Sie eingerichtet haben, ist die session-Tabelle Laravel verwenden muss die "Datenbank" - Treiber.
Sehen Sie die config hier: http://laravel.com/docs/session#database-sessions
Hoffe, das hilft!
InformationsquelleAutor der Antwort Bill Riley
Wie lange ist das id-Feld in der sessions-Tabelle? Laravel nutzt ein sha1-hash, als die session-id, das ergibt einen string der Länge 40. Ich hatte ein ähnliches problem und es war, weil mein id-Feld festgelegt wurde auf Länge 32.
Siehe diese Frage: Laravel 4.1-Authentifizierung, session-Daten, die nicht beibehalten und diese über Anfragen hinweg
InformationsquelleAutor der Antwort ralbatross
Okay, ich habe nicht gegraben, tief hinein, aber ich habe herausgefunden, dass laravel gibt nur cookies über eine sichere Verbindung.
Wie Sie müssen bemerkt haben, laravel setzt ein cookie, aber es reagiert nicht auf die Lebensdauer Einstellung in session.php
Erhalten Sie diese auf Ihrem lokalen server, müssen Sie nachahmen https Verbindung, um sicherzustellen, dass die Anmeldung bestehen bleibt.
Sie können dies durch die Erstellung eines gefälschten ssl-Zertifikat/Schlüssel für Ihre lokale Domäne.
Gibt es mehrere tutorials online verfügbar, die Ihnen helfen können, aktivieren Sie ssl.
Dieser hilfreich sein kann :
Zum aktivieren von SSL in MAMP Pro
MAMP mit SSL (https)
So Erstellen Sie ein SSL Zertifikat auf Apache Ubuntu 12.04
InformationsquelleAutor der Antwort Varun Nath
Versuchen,
bevor die Rücksendung oder das echo-Anweisung;
ex:
InformationsquelleAutor der Antwort Jean Dobler