How to redirect in laravel jetstream? - laravel

I am using laravel 8 with jetstream I want to know that how can I direct different users like admin or user to different routes?
RouteServiceProvider has Public const Home = '';
but it only direct to a one route

I hope this can help this is how I solve the problem
I created a new view for admin "admin/index.blade.php"
Then I assign the role of a developer to the admin, then I use this in my route
Route::middleware(['role:developer'])->get('/admin', function () {
return view('admin.index');
})->name('admin');

Related

Trying to show the current signed in users profile. What am i doing wrong | Laravel 9

Trying to show the current signed in user's profile. What am i doing wrong.This is the function on the controller. I'm using Laravel 9
public function show(User $user)
{
return view('users.index', with('user', $user));
}
This is the routes
Route::resource('users', UsersController::class)->middleware('auth');
And my generic layout page
<x-dropdown-item href="/users/{{ auth()->user()->username }}" >Account</x-dropdown-item>
When i click the link i get user not found.
You're utilising route model binding which unless configured otherwise, requires you to provide a route with a model id. You're providing it with a username, so Laravel is throwing a 404 because it can't locate the relevant record in the database.
If you replace username with id, the binding should work.
<x-dropdown-item href="/users/{{ auth()->user()->id }}">
Account
</x-dropdown-item>
The code is fine. Are you sure you have a route with username, resource routes are using id.
To access model like that you need to specify id in the route like:
/users/{id}
However what you are trying to do here is access the logged in user model which you can access with the facade Auth::user();
You can access the user any time any where in the code and there is no need to pass it to an anchor link unless sending to an external system.

login automatically a user from Wordpress in Laravel

I'm trying to use Wordpress as a frontend CMS/Shop and Laravel as a platform that provide more advanced services to users that acquire them from WP frontend shop. After logging to Wordpress they'll have in account a link to Laravel dashboard where they have all they're acquired services. For extracting data from WP I'm using Corcel but I need users already logged in to be automatically authenticated in Laravel.
All discussions I found are related to using same users, or login the other way, from Laravel to WP. Wp and laravel are on same domain, wp in root and laravel in a /laravel dir and they;re working ok. I tried a hook on login to call laravel inside wp but this would run a parallel instance of Laravel.
this in WP as a hook in funtions.php not working:
include $_SERVER['DOCUMENT_ROOT'].'/wplaravel/vendor/autoload.php';$app = include $_SERVER['DOCUMENT_ROOT'].'/wplaravel/bootstrap/app.php';$kernel = $app->make('Illuminate\Contracts\Http\Kernel');$kernel->handle($request = Illuminate\Http\Request::capture());
$id = (isset($_COOKIE[$app['config']['session.cookie']]) ? $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']], false) : null);
if ($id) {
$app['session']->driver()->setId(explode('|', $id)[1]);
$app['session']->driver()->start();
// Session::all();
// $app['auth']->getSession(); // Illuminate\Session\Store
// Auth::user();
// $app['auth']->user();
// $user = App\User::find(1);
$userl = App\Models\User::where('email',$user->data->user_email)->first();
Auth::login($userl);
//session()->put('wpuser', $user->data->user_email);
//print_r(session()->all());
}
I need to have a user that login to Wordpress to be able to be authorized in Laravel without logging in there also.
If your WordPress users also have a mobile number, this might be a good option for you.
It works like this in Laravel :
$user = User::query()->where(('mobile',$requset->mobile)->first();
Auth::login($user);
First of all your idea is completely wrong if you are trying login from wp to laravel.
Here you need to do following
1.Redirect the user to laravel application with a wp session or verified secret
wp_redirect( "laravelapp.com?secure=secretcodes );
2.Verify the secret from laravel with the given secret
$verify = $request->secure;
if($verify == "secretcodes"){
//verify your own logic here
}
3.Find the desired user and use this code bellow in any controller that you redirect from wp to laravel
$user = User::find(1);
Auth::login($user);
Then it will be logged in into laravel applicaiton

add routes after login page for user

I use backpack 4 for laravel(laravel 8)and can't write a route for the user page. After login user has to get /user/dashboard or /dashboard, but the user is redirected to /admin/dashboard. How can I solve this problem?
web.php
Route::get('/', function() {
return redirect()->route('backpack.auth.login');
});
Route::get('/user/dashboard', [DashboardUserController::class, 'index'])->middleware(['web','admin']);
If your users won't ever need to use Backpack and should only able to view the dashboard, you could add a new middleware to Backpack's set of middlewares that checks if your user is an admin or a normal user to redirect them if they are not an admin.
Because Backpack registers the login routes itself, you can't just add your middleware to the route on registration. Instead you'll need to add your middleware to Backpack's admin middleware group. That's the one that gets added to all admin routes.
Steps:
Create the new middleware:
php artisan make:middleware RedirectToDashboard
Open the new middleware at app/Http/Middleware/RedirectToDashboard.php
Change the handle method to look something like this:
public function handle(Request $request, Closure $next)
{
if($request->user()->isNormalUser()) { // You need to change this to the check if the user is a normal user, that's specific to your application
return redirect()->to('dashboard'); // 'dashboard' is the name of your route, you may need to change it.
}
return $next($request);
}
Add the new middleware to Backpack's default set of middlewares in config/backpack/base.php. Open the file and look for the middleware_class entry (most likely somewhere around line 220). Add \App\Http\Middleware\RedirectToDashboard::class to the array.
Log into Backpack as a normal user and check if you get redirected.
The /admin prefix comes from the file: config/backpack/base.php.
Remove route_prefix from the file.
Your users will be redirected to /dashboard.

How create middleware on laravel SPA from cretueusebiu

iam new and just learning about laravel SPA . i try to create project laravel spa from github : https://github.com/cretueusebiu/laravel-vue-spa .
so next step i want to add middleware , but in the structure i dont know what happens to do .
this folder on
js
middleware
admin.js
auth.js
check-auth.js
guest.js
locale.js
on admin.js
import store from '~/store'
export default (to, from, next) => {
if (store.getters['auth/user'].role !== 'admin') {
next({ name: 'home' })
} else {
next()
}
}
this role is admin but on table users i didnt see anything about role or anything , please tell me .
someone who already uses this , i just wanna ask , how to can create some middleware like , users ,admin ,super admin , or anything . thankyou
I think what you're looking to make are called guards (or gates) generally. This is usually accomplished by middleware alright but middleware can be used for a lot more.
In your specific instance you can enable the admin.js middleware in a component by adding middleware: 'admin' to the component script.
You're right about the role field on user. You'll need to add roles to the user table (and maybe the model too). You'll should be able to see it in the Vue dev tools.
You can add it as a text field or an enum really for now. There are better ways using a roles table. But an enum is quick and dirty.
admin.js only guards against a role of "admin" so you can just copy / paste the file and change store.getters['auth/user'].role !== 'admin' to whatever role you want to guard.
Any files in the js/middleware/ dir look to be autoloaded from js/router/index.js
For example if you make a middleware called superUser.js and change the guard to store.getters['auth/user'].role !== 'superUser'
You would add middleware: 'superUser' to your component.

Can't persist Auth::login with Laravel 5

I'm trying to manually log in a user within a Controller or a Route. This works during the scope of the page, but it doesn't persists.
Here is my code :
Route::get('check', function (){
if (Auth::loginUsingId(1)) { // User #1 is existing in the DB
return redirect()->intended('/check2');
}
});
Route::get('check2', function (){
dd(Auth::user());
});
And I got : null on my /check2 page
Can someone give me a quick hint?
PS: I tested the default Auth Laravel system and it works.
Thank you very much!
The issue was simple. On Laravel 5.2, you need to wrap all concerned Routes inside the middleware "web" in order to play with Authentification.
Thanks all.

Resources