I am new to Node.js, and I am trying to integrate a function I was able to do in PHP in some other project, creating an admin route so that when a regular user logs in, it takes the user to the user dashboard. When the admin logs in, it takes the user to the admin dashboard.
class AdminRouteProtect
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* #return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if(Auth::user()){
if(Auth::user()->hasRole('admin')){
return $next($request);
} else{
return redirect()->back();
}
}
return $next($request);
}
}
Related
what is the difference between Request and LoginRequest in laravel in these examples:
1- LoginRequest example:
/**
* Handle an incoming authentication request.
*
* #param \App\Http\Requests\Auth\LoginRequest $request
* #return \Illuminate\Http\RedirectResponse
*/
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
return redirect()->intended(RouteServiceProvider::HOME);
}
2- Request example:
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string|null ...$guards
* #return mixed
*/
public function handle(Request $request, Closure $next, ...$guards)
{
throw new Exception($request);
$guards=empty($guards)? [null] : $guards ;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
}
return $next($request);
}
I am using laravel 8.
I think my question is clear.
When you use Request, you cannot limit your form request. But if you use Custom Form Request, you can make it flexible like what you want, you can validate, authorize, make rules, and custom error message, like LoginRequest
You can make custom request with this command
php artisan make:request LoginRequest
Or you can read on documentation
https://laravel.com/docs/8.x/validation#form-request-validation
I'm new to laravel and I'm trying to use laravel basic auth for android application login. I can login without any problem to my website but using the same username and password in postman basic auth , I got the message "sorry, you are not authorized". I'm completely confused, can anyone help me please?
The basic auth middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
class BasicAuth
{
protected $auth;
/**
* Create a new middleware instance.
*
* #param \Illuminate\Contracts\Auth\Factory $auth
* #return void
*/
public function __construct(AuthFactory $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
return $this->auth->basic('phone') ?: $next($request);
}
}
The route that I call in postman is:
Route::middleware('basic.auth')->get('/user', function (Request $request) {
return $request->user();
});
Postman
I want to check if users are using the system.
I know have a last_login, with the standard laravel class UpdateLastLoggedInAt.
public function handle(Login $event)
{
$event->user->last_login = Carbon::now();
$event->user->save();
}
This doesn't work if users let their browser window stay open.
Is there a way to have it so that on every page refresh or route change (users navigates through the website), change the last_login to that time?
Thanks in advance.
I created a middleware for this and wrapper it around all routes.
class LogLastSeen
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$user = User::find(Auth()->user()->id);
$user->last_login = Carbon::now();
$user->save();
return $next($request);
}
I have looked at many similar questions bu they don't approach the real problem. I would like to redirect a user to a certain url just after login depending on a condition about the user.
I know this can be archieved with a middleware so I have tried this in app\Http\Middleware\RedirectIfAuthenticated.php
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string|null $guard
* #return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::User()->check()) {
$redirect = '/client';
if (Auth::user()->hasRole('admin')){
$redirect = '/admin';
}
return redirect($redirect);
}
return $next($request);
}
}
I realise now this will not work just after login. I'd like to redirect a user depending whether he/she is an admin or a client. I know I could use: protected $redirectPath = '/url/to/redirect'; but I have multiple pages to redirect to.
What is the best way to do this?
You could over-write the redirect method offered up by the trait in app/Http/Controllers/Auth/AuthController.php
public function redirectPath()
{
if (Auth::user()->hasRole('admin')){
return '/admin';
}
return '/client';
}
Put that in your AuthController.php.
AngularJs is woking
angular.module("myApp").run(function($http){
$http.defaults.headers.common['mytoken'] = $('meta[name=csrf-token]').attr('content');
})
Middleware Laravel 5
class BeforeCsrf implements Middleware {
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Session::token() !== (string)$request->header('mytoken')){
prin_r($request->header('mytoken'));
}
return $next($request);
}
}
but $request->header('mytoken') not working.
Sorry my english is bad
prin_r($request->header('mytoken'));
You write prin_r instead of print_r maybe you got an error and your server is hidding the erros from you.