How create middleware on laravel SPA from cretueusebiu - laravel

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.

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.

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 to redirect in laravel jetstream?

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');

Is it possible to get Laravel user data from Vue JS?

i have a Laravel 5.4 application where i do all Authentication based logic through PHP and then redirect the user to a catchAll route when they are authenticated, and let VueRouter take it from there...
I'd like to also use Entrust because my app will have several types of users and some elements (like an Edit User button) will only be visible to some user Roles.
I might also want to implement specific permissions, like some Admins can edit user Permissions, while others do not.
The issue is, alright i'm in Javascript territory now, so how do i know what my current Auth user is? Setting a global JS variable for Auth::user doesn't seem like a good idea to me.
Perhaps i would instead pass just an ID, but how exactly without making it globally visible as a window variable?
I think you may create an auth/check API call, like this:
protected function check()
{
if(Auth::guard('api')->check()) {
return Auth::guard('api')->user();
}
return ['success' => false];;
}
And then get current user with this call.

How to make two types of users in Laravel

I want to ask how to make two types of users in laravel. I have two tables, one for the customer and one for the client and my question is how to make that difference. Do I have to make two different models or to use model User and make some functions in middleware?
Thank you.
If you're looking for the simpliest solution, you can add role column to users table. Then you can check if user is a client or a customer globally with:
if (auth()->user()->role === 1) {
// It's a client.
}
You can add some helper methods to check if user is a client or a customer:
public function isClient()
{
return auth()->user() && auth()->user()->role === 1;
}
To open the part of the website for client only you should use route groups and middleware.
I've run into the same situation and I've resolved it by using a package that handles multi-authentication:
Check out this package:
https://packagist.org/packages/hesto/multi-auth
There are also a blog post about this situation:
http://santo.sh/multi-login-for-laravel-5-3/
and of course more StackOverflow questions:
Using multiple Auth in Laravel 5.3?
Multiple Authentication in Laravel 5.3

Resources