Laravel custom authentication - php

I'm starting to discover Laravel 5, so I might need a bit of your help to understand a few things.
First, I want to develop a login page. It seems that Laravel has a whole authentication system, and I guess I should use it.
Nevertheless, I want to display a login page (I know how to do this!), but after this, I would like to send the credentials to a server via an API call. The server will then tell me if the user is allowed to log in or not.
As far I understand Laravel and authentication, it seems that the authentication system works only with a local DB.
Can you confirm I need to use a custom authentication driver to do this?
I've been following this solution
but I get this error when loading my page:
FatalErrorException in CustomUserProvider.php line 6:
Interface 'Illuminate\Auth\UserProviderInterface' not found
Any help would be appreciated, feel free to ask me for more information if you need it.
Thanks

I tried following the same thread you mentioned and arrived at the very same results. Then I checked the implementation of native UserProviders (Illuminate/Auth/EloquentUserProvider and Illuminate/Auth/DatabaseUserProvider) and ended up using the same set as in EloquentUserProvider:
<?php namespace App\Auth;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class MyUserProvider implements UserProvider {
// Implementation goes here
}
I believe this to be more correct approach as the suggestions from the forum thread seem to be possibly for an older/beta version of L5.

Here is my CustomUserProvider file:
<?php namespace App\Auth;
use Illuminate\Contracts\Auth\UserProvider as UserProviderInterface;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\GenericUser;
class CustomUserProvider implements UserProviderInterface {
It's now working :-)

Related

Class 'App\Http\Controllers\Saml2' not found

I'm on Laravel 5, I'm trying to integrate SAML 2.0 with it. I've found this package = https://github.com/aacotroneo/laravel-saml2
I tried follow their steps, but at the end when I use
<?php
namespace App\Http\Controllers;
class SAMLController extends Controller {
public function adminSignIn(){
return Saml2::login(URL::full());
}
}
I've already added
provider
'Aacotroneo\Saml2\Saml2ServiceProvider',
aliases
'Saml2' => 'Aacotroneo\Saml2\Facades\Saml2Auth',
Why do I still get this error?
Class 'App\Http\Controllers\Saml2' not found
Note : I've even retry after sudo composer dumpauto, same result.
You need to use full namespace for the facade:
\Saml2::login(URL::full());
Or add this to the top of the class:
use Saml2;
you need to explicitly write "use" on top
use Saml2;
This might work.

How to fix Class 'App\Http\Controllers\Notification' not found in laravel?

I have a Controller which listens to a new Schedule creation and sends the result back to the view via ajax. Inside of it I want to add a Notification to send email to the user once the Schedule cannot be completed due to a lack of resources at that specific date and time.
The problem is that I get the error below:
Class 'App\Http\Controllers\Notification' not found in /laravel/app/Http/Controllers/DadosAgendamentoController.php on line 89
The folder structure is this:
-app
-Http
-Controllers
DadosAgendamentoController.php
-Notifications
AgendamentoPendente.php
DadosAgendamentoController.php head code:
namespace App\Http\Controllers;
use Input;
use Request;
use App\Servicos;
use App\Disponibilidades;
use App\Estabelecimentos;
use App\HorariosEstabelecimento;
use App\Agendamento;
use App\User;
use App\Notifications\AgendamentoPendente;
line 88 and 89:
$user = User::where('id',1)->get();
Notification::send($user, new AgendamentoPendente(1));
Trough my Controller I can access all the classes above, but not the AgendamentoPendente
My goal is to send an email do the admin so he can suggest a new date and time to the client when the resources are not available at the desired date and time.
How can it be fixed? Can I access the class in this Controller? How?
Notifications may be sent in two ways: using the notify method of the Notifiable trait or using the Notification facade.
https://laravel.com/docs/5.3/notifications#sending-notifications
Option 1
You can use notify() method:
$user->notify(new AgendamentoPendente(1));
Also, make sure User class uses Notifiable trait:
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
Option 2
Using facade with full namespace:
\Notification::send($user, new AgendamentoPendente(1));
Add use Notification; in your controller
OR
alternatively, use \Notification::send($user, new AgendamentoPendente(1));
add this at the top of the controller:
use App\Notifications\AgendamentoPendente;
i was having the same problem and this fixed it
Also note that if you are using the facade, make sure your User queries the email field from your database
$users = User::select("email")->get();
\Notification::send($users, new AgendamentoPendente(1));
You have to use facades at the top
use Illuminate\Support\Facades\Notification;
You can refer to this tutorial
https://thecodingsolution.com/view/laravel-notofication-laravel-database-notification
You can pull in the notification library used Lumen 8.0:
"illuminate/notifications": "5.3.*"
into your composer.json then running composer update to pull in the notification libraries.
You will also need to add
$app->register(Illuminate\Notifications NotificationServiceProvider::class);
to your bootstrap/app.php
This process working for me.
Thanks

How to use Laravels spatie/laravel analytics package

I just downloaded this package for Laravel.
spatie/laravel-analytics
Its a Google Anayltics package, and I followed all the steps for setting up an account. What I'm having trouble is calling the methods. For example when it says:
Here is an example to retrieve visitors and pageview data for the current day and the last seven days.
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
I tried doing this in my function like this:
<?php
namespace App\Http\Controllers\Admin;
use Carbon\Carbon;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Spatie\Analytics\Analytics;
use Illuminate\Support\Collection;
class DashboardController extends Controller {
public function index() {
$analytics = Analytics::fetchVisitorsAndPageViews(Period::days(7));
dd($analytics);
return view('admin.dashboard-v2');
}
}
Its giving me errors like:
Non-static method Spatie\Analytics\Analytics::fetchVisitorsAndPageViews() should not be called statically
Am I missing something here? I couldn't find any specific documentation online except for the Github ReadMe file
If you want to use the facade to access the class, you'll need to change use Spatie\Analytics\Analytics; to use Analytics;. That should take care of that error.
If you are going to use Period::days(7) then you will need to add use Spatie\Analytics\Period; because that's an actual static method, not a facade.

is it possible to incept a class which being used by vendor's code in php?

I'm using Laravel 5 framework.
I extended the Illuminate\Http\Request Class and added some functions. So I changed bootstrap to boot with my Custom Http Request Class. They work well.
But when it come to integrated test. by extending their TestCase, they use the Request class as below
use Illuminate\Http\Request;
$request = Request::create();
Is there a way for me to override their class from using Illuminate\Http\Request to use MyApp\Http\Request at my own class? I don't want to change their code.
No.
Their code calls it directly, so there's unfortunately nothing you can do.

Mapping Exception when trying out the Symfony2 example from the cookbook

I just started learning Symfony2 and I'm following the examples from "The Cookbook" from Symfony's website.
When trying the code from the chapter about loading users from database (Entity Provider) (Link to the chapter) I get the following error:
MappingException: Class Acme\UserBundle\Entity\User is not a valid entity or mapped super class.
... and can't find out wthat I am doing wrong. I do think I followed all the steps provided in the chapter.
Thanks for any help,
I finally found the problem when revising step by step the whole code.
I forgot to register UserBundle in AppKernel.php.
I have the same problem. I looked at symfony+Mapping error but that solution not works for me. Then I found, that Michi solution works https://stackoverflow.com/a/10935672/2910183
So, here is what I do at all:
register bundle in AppKernel.php:
new Acme\UserBundle\AcmeUserBundle(),
create this bundle (it is just a copy of FOS\UserBundle\FOSUserBundle) and save as src/Acme/UserBundle/AcmeUserBundle.php
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
class AcmeUserBundle extends Bundle {
}

Categories