Laravel 5.2 Auth not persisting on other routes - laravel

I am new to Laravel. The Laravel app I am creating works perfectly on my local machine, but when I uploaded it on my host the Auth session is not persisting on other routes.
routes.php
Route::group([], function () {
Route::group(['domain' => 'admin.domain.com'], function() {
Route::get('/login', 'LoginController#login');
Route::get('/checklogin', 'CheckLoginController#check');
});
});
LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\User as User;
class LoginController extends Controller
{
//
public function login()
{
$user = User::find(1);
Auth::login($user);
return "logged in"; //returns logged in
}
}
CheckLoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
class CheckLoginController extends Controller
{
//
public function check()
{
if( !Auth::guest() ) :
echo "logged in";
else :
echo "Not logged in"; //returns this
endif;
}
}

Related

Laravel :Target class [LoginController] does not exist."

Even the controller is in the right folder an has the right name, when i test it in thunder client i get:
{ "error": "Error en la aplicación
(Illuminate\Contracts\Container\BindingResolutionException):Target
class [LoginController] does not exist." }
Here's my controller in app/http/Controllers/api
<?php
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use App\Models\Usuario;
class LoginController extends Controller
{
public function login(Request $request)
{
$usuario = Usuario::where('login', $request->login)->first();
if (
!$usuario ||
!Hash::check($request->password, $usuario->password)
) {
return response()->json(
['error' => 'Credenciales no válidas'],
401
);
} else {
$usuario->api_token = Str::random(60);
$usuario->save();
return response()->json(['token' => $usuario->api_token]);
}
}
}
and here's my route in the folder routes/api
Route::post('login', [LoginController::class, 'login']);
i have tried changing the name, i changed the route to
Route::post('login', [\App\Http\Controllers\Api\LoginController::class, 'login']);
and then said
{ "error": "Error en la aplicación
(Symfony\Component\ErrorHandler\Error\FatalError):Cannot declare
class LoginController, because the name is already in use" }
You are missing your namespace :
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
[...]

Class "App\Http\Controllers\Auth\Mail" not found

How can I resolve this error? I am trying to customize the default email template on laravel. This is the code for the controller that sends the email.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use App\Models\User;
use Illumunate\Auth;
class EmailVerificationNotificationController extends Controller
{
public function store(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(RouteServiceProvider::HOME);
}
Mail::send('email.template', $request->user(), function($mail) use($data){
$mail->to($request->user()->email, 'no-reply')->subject("Verify Email Address");
$mail->from('admin#raketlist.com','testing');
});
$request->user()->sendEmailVerificationNotification();
return back()->with('status', 'verification-link-sent');
}
}
Add use Illuminate\Support\Facades\Mail; to other uses.

Laravel,Can I use auth('api')->user() in Middleware?

I Would like to use auth('api')->user() in Middleware ,But felt that it wouldn't work.
And I don't use Auth::check() Because it Return null
P.S. auth('api')->user() Can use in my Controller.
-This code check online/offline user-
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
class LastUserActivity
{
public function handle($request, Closure $next)
{
$user = auth('api')->user();
if (isset($user)) {
$expiresAt = Carbon::now()->addMinutes(1);
Cache::put('user-is-online-' . $user->ID, true, $expiresAt);
}
return $next($request);
}
}

Auth::User() gives null in laravel 5.2

I am new to laravel. I am using multi authentication in my application. User and Admin are 2 type of user in my application. Iam developing change password for admin after logged in to application through admin's profile page. now I want to get logged in admin user detail so that i have use below code in my controller
if (Auth::guard('admin')->check()) {
$user = Auth::id();
echo '<pre>';
dd($user);
exit;
}
I have also add following code in controller
use App\Http\Controllers\Adminauth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Admin;
use Session;
use DB;
use App\Http\Controllers\Controller;
use Auth;
In route.php i have write following code
Route::group(['middleware' => ['admin']], function () {
//Login Routes...
Route::auth();
Route::get('admin/profile/change_password', 'Admin\AdminController#change_password');
});
But i am not able to get admin user detail. can anyone help me to solve this problem.
Try this
public function __construct() {
$this->middleware('auth:admin');
$this->middleware(function ($request, $next) {
$user = $this->user = Auth::user();
dd($user);
});
}
In routes
Route::get('/', function () {
//return view('welcome');
return view('auth.login');
});
Auth::user() is working only under auth middleware. You may call auth middleware on controller or call it on routes, but not on both.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class YourController extends Controller
{
public function __construct()
{
$this->middleware('auth')
}
public function yourFunction(Request $request)
{
$user = \Auth::user();
dd($user);
}
}
<?php
Route::get('your-uri', function () {
$user = \Auth::user();
dd($user);
})->middleware('auth');

Custom login laravel 5.5

I have problem with custom login laravel. This is code for authenticate.
This code don't work. have you idea?
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Users;
use DB;
class LoginController extends Controller {
public function dologin(Request $request){
$email = $request->input('u_email');
$password = $request->input('pwd1');
// Check validation
$checkLogin = DB::table('users')->where(['u_email'=>$email,'password'=>$password])->get();
if(count($checkLogin) >0){
echo "Login SuccessFull<br/>";;
}else{
echo "Login Faield Wrong Data Passed";
}
}
}
You can't do that because passwords are hashed in Laravel. Use the attempt() method instead:
// Check validation
if (auth()->attempt(['email' => $email, 'password' => $password])) {
echo "Login SuccessFull<br/>";;
} else {
echo "Login Failed Wrong Data Passed";
}

Resources