So i have been working with Laravel and Auth0 for some time now and i think i am at the end :)
I am able to log into my application using a link to the widget / hosted page
Now everything seems to be working and once the page callbacks to my site the user I saved in my database.
However, it doesn't seem that my application remembers the user.
When i attempt to check if a user is logged in:
$isLoggedIn = \Auth::check();
it says false
I have tried debugging multiple functions however with no results so i am kind of lost on where to start with this.
Does anyone know why this is happening?
My configuration
So this is my AuthController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class AuthController extends Controller
{
public function __construct()
{
}
public function login()
{
return \App::make('auth0')->login(null, null, ['scope' => 'openid profile email'], 'code');
}
public function logout()
{
\Auth::logout();
return \Redirect::intended('/');
}
public function dump()
{
$isLoggedIn = \Auth::check();
return view('dump')
->with('isLoggedIn', $isLoggedIn)
->with('user',\Auth::user()->getUserInfo())
->with('accessToken',\Auth::user()->getAuthPassword());
}
}
Then in my web:
Route::get('/login', ['as' => 'login', 'uses' => 'AuthController#login']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'AuthController#logout'])->middleware('auth');
Route::get('/dump', ['as' => 'dump', 'uses' => 'AuthController#dump', 'middleware' => 'auth'])->middleware('auth');
Route::get('/auth0/callback', '\Auth0\Login\Auth0Controller#callback');
Related
I created a custom authentication for my Laravel app following that tutorial: https://medium.com/#nasrulhazim/laravel-using-different-table-and-guard-for-login-bc426d067901
I adapted it to my needs but I didn't have to change much.
In the end, when I try to go the the /home route, but it says: "Route [login] not defined."
My guess is that a default behavior of the authentication call the login route instead of the /fidelite/login I've created.
Here is my provider:
fidelite' => [
'driver' => 'eloquent',
'model' => App\Fidelite::class,
],
And the guard
'fidelite' => [
'redirectTo' => 'fidelite.home',
'driver' => 'session',
'provider' => 'fidelite',
],
The routes defined in the web.php file
Route::prefix('fidelite')
->as('fidelite.')
->group(function() {
Route::get('/home', 'Home\FideliteHomeController#index')->name('home');
Route::namespace('Auth\Login')
->group(function() {
Route::get('login', 'FideliteController#showLoginForm')->name('login');
Route::post('login', 'FideliteController#login')->name('login');
Route::post('logout', 'FideliteController#logout')->name('logout');
Route::get('register', 'FideliteController#showRegisterForm')->name('register');
});
});
Basically, there is two controllers; the first one, FideliteController adds the middleware and show the needed forms to login / register
class FideliteController extends DefaultLoginController
{
protected $redirectTo = '/fidelite/home';
public function __construct()
{
$this->middleware('guest:fidelite')->except('logout');
}
public function showLoginForm()
{
return view('auth.login.fidelite');
}
public function showRegisterForm()
{
return view('auth.compte');
}
public function username()
{
return 'email';
}
protected function guard()
{
return Auth::guard('fidelite');
}
}
And the other one returns the /fidelite/home page when the user is logged
class FideliteHomeController extends Controller
{
public function __construct()
{
$this->middleware('auth:fidelite');
}
public function index()
{
return view('home.fidelite');
}
}
There is something I missing, but what ?
Many thanks for your help and time...
Found it ! Thanks to the Alpha whom helps me find the problem !
The problem was that the middleware I was using (Authenticate.php) was redirecting to the route('login') instead of the custom route I needed.
You are duplicating the login name route. change the name of login to any specific name that properly define your route behavior.
I have tried every thing and even checked the whole of stack overflow. My Laravel blade views are not being rendered. There is an internal server error. the default root view shows but the rest aren't. The permissions are fine, currently 777 on all folders and files. I'm really stuck. What could it be?
Route::group(['middleware' => ['web']], function () {
//Routes using UserController
Route::get('/welcome1ab', [
'uses'=>'UserController#getWelcome1ab',
'as' => 'welcome1ab',
]);
Route::post('/welcome1ab', [
'uses'=>'UserController#postWelcome1ab',
'as' => 'register1',
]);
Route::get('/', [
'uses'=>'UserController#getWelcome',
'as' => 'welcome',
]);
Route::get('/welcome1a', [
'uses'=>'UserController#getWelcome1a',
'as' => 'welcome1a',
]);
Controller functions:
class UserController extends Controller{
public function getWelcome1ab(Request $request){
return view('welcome1ab');
}
public function getWelcome1a(Request $request){
return view('welcome1a');
}
public function getWelcome1(Request $request){
return view('welcome1');
}
public function getWelcome(Request $request){
return view('welcome');
}
I have created separate table called subscribers in mysql changed config/auth.php settings to 'model' => App\Subscribers::class, 'table' => 'subscribers'.
I have login form on home page, that submits to the home page.
so in routes i have below
Route::get('/', function () {
return view('home');
});
Route::post('/', 'LoginController#validate');
my LoginController
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
public function validate()
{
// attempt to do the login
$auth = Auth::attempt(
[
'email' => strtolower(Input::get('email')),
'password' => Hash::make(Input::get('password'))
]
);
if ($auth) {
return Redirect::to('dashboard');
}
}
}
when i login i get below error
Declaration of App\Http\Controllers\LoginController::validate() should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)
You can't use 'validate' as a name for a function. It will conflict with:
App\Http\Controllers\Controller::validate
Also add an 'else' to your if statement so if your authentication fails you can redirect the user back to the login screen for example.
I am trying to run the code from a book "Laravel blueprints", chapter 4: "Personal blog" on Laragon and Bitnami Laravel stacks.
I put all the controllers, views, models and routes in respective app folder, set up database so it shows connection and tables migrated, however, when I want to run home page, it says: "Post class not found". I can`t return the home view of the blog. Please help: what I have missed in configurations, how to run examples from book on Laragon and Bitnami Laravel, what files to copy, what remain unchanged and what to configure?
Thanks
<?php
class PostsController extends BaseController{
public function getIndex()
{
$posts = Posts::with('Author')->orderBy('id', 'DESC')->paginate(5);
return View::make('index')
->with('posts',$posts);
}
public function getAdmin()
{
return View::make('addpost');
}
public function postAdd()
{
Posts::create(array(
'title' => Input::get('title'),
'content' => Input::get('content'),
'author_id' => Auth::user()->id
));
return Redirect::route('index');
}
}
-
class Post extends Eloquent {
//the variable that sets the table name
protected $table = 'posts';
//the variable that sets which columns can be edited
protected $fillable = array('title','content','author_id');
public $timestamps = true;
public function Author(){
return $this->belongsTo('User','author_id');
}
}
<?php
/*
|--------------------------------------------------------------------------
Route::get('/', array('as' => 'index', 'uses' =>
'PostsController#getIndex')); Route::get('/admin', array('as' =>
'admin_area', 'uses' => 'PostsController#getAdmin'));
Route::post('/add', array('as' => 'add_new_post', 'uses' =>
'PostsController#postAdd')); Route::post('/login', array('as' =>
'login', 'uses' => 'UsersController#postLogin'));
Route::get('/logout', array('as' => 'logout', 'uses' =>
'UsersController#getLogout'));
Looks like a namespacing error, I had struggle with it at the beggining too.
Please show us you Post class code, your routes.php file and the controller that is throwing the error.
Actually before doing this, change all places in script where you use Post:: for \App\Post::
How do I add username to url?
Example: www.domain.com/feeds/username
I want to add username to every route url.
::Route::
Route::get('feeds', array('before' => 'auth', 'as' => 'feeds', 'uses' => 'FeedsController#getFeedsPage'));
::Controller::
class FeedsController extends BaseController {
public function getFeedsPage() {
return View::make('feeds.index');
}
}
Thanks in advance.
Michael.
You may try this, declare the route like this (Notice {username}):
Route::get('feeds/{username}', array('before' => 'auth', 'as' => 'feeds', 'uses' => 'FeedsController#getFeedsPage'));
Declare the class:
class FeedsController extends BaseController {
// Use a parameter to receive the username
public function getFeedsPage($username) {
// You may access the username passed via route
// inside this method using $username variable
return View::make('feeds.index')->with('username', $username);
}
}
Now you may use a URI like these:
www.domain.com/feeds/username1
www.domain.com/feeds/michaelsangma