I have to make default login in Laravel with: php artisan make:auth and I want to add 1 more authentication with API. In this auth API I don't need database for login.
Is there any solution for this case?
I want to make custom provider and guard it but I am stuck at AlumniAuthProvider.php on App\Auth:
<?php
namespace App\Auth;
use Illuminate\Contracts\Auth\User as UserContract;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use App\Auth\User;
class AlumniAuthProvider implements UserProvider {
public function alumni()
{
}
public function retrieveById($identifier)
{
}
public function retrieveByToken($identifier, $token)
{
}
public function updateRememberToken(Authenticatable $user, $token)
{
}
public function retrieveByCredentials(array $credentials)
{
}
public function validateCredentials(Authenticatable $user, array $credentials)
{
}
}
and Alumni.php on App\Auth:
<?php
namespace App\Auth;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
class Alumni implements Authenticatable
{
public function getAuthIdentifierName()
{
}
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
}
/**
* Get the token value for the "remember me" session.
*
* #return string
*/
public function getRememberToken()
{
}
/**
* Set the token value for the "remember me" session.
*
* #param string $value
* #return void
*/
public function setRememberToken($value)
{
}
/**
* Get the column name for the "remember me" token.
*
* #return string
*/
public function getRememberTokenName()
{
}
}
How could I make this custom API login?
In my opinion this code, which I made, is for second auth, or am I wrong? Maybe is there any other way to solve this problem?
I think doing api login without database is a bad idea because everytime the user will login through another system the api call will took place and thats will create more traffic when your system will be live. Better way is to make api call for the first time and store data in the database and afterwards call data from the database when user re-logins.
Related
Very simple question that I can't find the answer for. I'm using Confer in my Laravel app and wish to alter the html that is stored in the session storage for each new message. I've made a few formatting changes to conversation messages in views\conversation.blade.php as well as views\confer.blade.php, but when a user submits a new message, that message is pushed with the original formatting. I can't for the life of me find where the html formatting is passed into the session so it can be pushed to the client.
Confer's SessionController.php
namespace DJB\Confer\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
use Response;
class SessionController extends Controller {
/**
* Store the conversation list HTML in the session so that it can be retained over page loads
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
Session::put('confer_conversations', $request->input('html'));
return Response::json(['success' => true]); // required to persist the session
}
/**
* Update the requested conversations that have yet to blossom with a message
*
* This needs to persist over page loads otherwise recipient(s) will not be
* subscribed to the channel to receive any messages.
*
* #param Request $request
* #return Response
*/
public function update(Request $request)
{
if (Session::has('confer_requested_conversations'))
{
Session::push('confer_requested_conversations', (int)$request->input('conversation_id'));
} else {
Session::put('confer_requested_conversations', [(int)$request->input('conversation_id')]);
}
return Response::json(['success' => true]); // required to persist the session
}
/**
* Clear the open chat list and requested list
*
* #return Response
*/
public function destroy()
{
Session::forget('confer_conversations');
Session::forget('confer_requested_conversations');
return redirect()->back();
}
}
MessageWasSent
namespace DJB\Confer\Commands;
use Illuminate\Console\Command;
use Illuminate\Queue\InteractsWithQueue;
use DJB\Confer\Message;
use DJB\Confer\Confer;
use Push;
class MessageWasSent extends Command {
use InteractsWithQueue;
protected $message;
protected $confer;
public function __construct(Message $message)
{
$this->message = $message;
$this->confer = new Confer();
}
/**
* Handle the command.
*/
public function handle()
{
$conversation = $this->message->conversation;
$conversation->touch();
if ($conversation->isGlobal())
{
Push::trigger($this->confer->global, 'NewMessageWasPosted', $this->message->getEventData('global'));
} else {
Push::trigger($this->message->conversation->getChannel(), 'NewMessageWasPosted', $this->message->getEventData());
Push::trigger($this->message->conversation->getChannel(), 'UserStoppedTyping', ['user' => $this->message->sender->id]);
}
}
}
namespace DJB\Confer\Events;
class MessageWasSent {
public function __construct()
{
}
public function handle()
{
}
}
I am quite new to Laravel and I really need some help. I need to create a simple app for my job, and I think I will not have problem with this as the tutorials here are really excellent.
The issue I have is that for that project I need to authenticate the users against an external DB using a SOAP webservice and if the user does not exist in the local DB I create it and log the user in. I am able to manage this part as I have already written a Joomla plugin that does that.
I have tried to figure out the documentation on how to create a custom driver. http://laravel.com/docs/5.1/authentication I thought that at first I would replicate the EloquentUserProvider befor modifying it, thus I created:
ErsAuthServiceProvider and ErsUserProvider respectively placed in App\Providers and App\Extensions
But it mysteriously does not work... I get the following error:
ErrorException in ErsUserProvider.php line 33: Argument 1 passed to App\Extensions\ErsUserProvider::__construct() must be an instance of Illuminate\Contracts\Hashing\Hasher, none given, called in /home/vagrant/Code/ERSTools/app/Providers/ErsAuthServiceProvider.php on line 31 and defined
Actually I do not understand much in the documentation what they are doing with the boot() method in the example. I understand that they extend The Auth class in order to add the new driver (ers in my case) but I do not get why they pass the $app['riak.connection']
<?php
namespace App\Providers;
use Auth;
use App\Extensions\ErsUserProvider;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class ErsAuthServiceProvider extends ServiceProvider
{
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any application authentication / authorization services.
*
* #param \Illuminate\Contracts\Auth\Access\Gate $gate
* #return void
*/
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
Auth::extend('ers', function($app) {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
return new ErsUserProvider;
});
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
and
<?php
namespace App\Extensions;
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class ErsUserProvider implements UserProvider
{
/**
* The hasher implementation.
*
* #var \Illuminate\Contracts\Hashing\Hasher
*/
protected $hasher;
/**
* The Eloquent user model.
*
* #var string
*/
protected $model;
/**
* Create a new database user provider.
*
* #param \Illuminate\Contracts\Hashing\Hasher $hasher
* #param string $model
* #return void
*/
public function __construct(HasherContract $hasher, $model)
{
$this->model = $model;
$this->hasher = $hasher;
}
... the rest is similar to the original file (EloquentUserProvider)
Finally, my plan is to keep the ErsUserprovider quite similar to the EloquentUserProvider and to implement my check with the webservice in the validateCredentials() method as in this method I shoul know if a user exists with the requested username in the local DB, I will know if the user passes validation with the SOAP webservice I can then
Login the user
Login the user and create a new user based on the date returned by the webservice
refuse the login.
Is this a good plan?
I sweated but I made the first part work. The issue was within the boot method.My custom provider is a working replica of the original laravel 5.1 I can now customize it.
Here is the Service provider that works:
<?php
namespace App\Providers;
use App\Extensions\ErsUserProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Auth\UserProvider;
class ErsAuthServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
$this->app['auth']->extend('ers',function($app)
{
$model = $app['config']['auth.model'];
return new ErsUserProvider($app['hash'], $model);
});
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
I have made a Laravel Authentication system. It worked, but after updating laravel from 4.1.2.5 to 4.1.2.6 it stopped working. And I found out the problem, when the user tries to login, it fails since
Auth::attemp(array("user"=>$username, "password"=>$password))
regenrated the password Hashes and thus does not match against the one generated in time of registration.
Here is my User model:
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');
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
public function getAuthPassword(){
}
public function getAuthIdentifier(){
}
function getReminderEmail (){
}
}
Here is my Login attempt:
Auth::attempt(array("email"=>$data['email'], 'password'=>$data['password']))
There's a breaking change between 4.1.25 and 4.1.26. Please check the upgrade section here
I just have a question about add the three "RememberToken" public functions (getRememberToken(), setRememberToken(), and getRememberTokenName() ). For some reason if I tried to log in and create a new session my page would crash and I would get the "Class Foo contains 3 abstract methods..." until I had add all three to every model I had. The weird thing is that I was trying to sign in with Sessions class but I would get this error until I added the new RememberToken functions to every class I have. Is this normal? Do I need to add the "remember_token" to every table that I use now? If anyone could explain why this is or how I went wrong that would be greatly appreciated! Thanks so much!
Here is an example of one of my models with the 3 RememberToken functions:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Warranty extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable = array( 'id', 'created_by', 'street_address', 'warranty_serialized');
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'warranties';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the order.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the order.
*
* #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;
}
/*4.26 Update to RememberToken*/
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
}
You have to add it to the all models that
implements UserInterface, RemindableInterface
Because those are basically User tables.
I have two tables: one for User and one for Profile.
I'm trying to figure out how to update the profile table upon a user registering.
Here's my User and Profile Model classes:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface{
protected $fillable = array('fname','lname','email','password','create_at','updated_at');
/**
* 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;
}
/**
* #method to insert values into database.
*/
public static function create_user($data = array())
{
return User::create($data);
}
/**
*#method to validate a user in the database
*/
public static function validate_creds($data)
{
return Auth::attempt($data);
}
public function profile()
{
return $this->hasOne('Profile');
}
public function post()
{
return $this->hasMany('Post');
}
}
And my profile model:
<?php
class Profile extends Eloquent{
public static function createNewProfile($data)
{
return Profile::create($data);
}
public static function editProfile()
{
//
}
public function user()
{
return $this->belongsTo('User');
}
}
You can try events:
http://laravel.com/docs/events
Make a On.user.create event
and build a listener that does the things you need.
I'm not sure, but you could try to put that code that updates the Profile in the User Constructor.