login automatically a user from Wordpress in Laravel - 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

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.

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

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.

Check Laravel 5.7 login from external script

I have a Laravel app and I need to check if a user is logged and who from a external script. I'm using the following lines to load Laravel and try to check it.
require_once __DIR__.'/../../../vendor/autoload.php';
$app = require_once __DIR__.'/../../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Http\Kernel')
->handle(Illuminate\Http\Request::capture());
/*if (Cookie::get(config('session.cookie')) != "") {
$id = Cookie::get(config('session.cookie'));
Session::driver()->setId($pericod);
Session::driver()->start();
}*/
$isAuthorized = Auth::check();
if(!$isAuthorized){
echo "NO AUTORIZADO";
exit();
}
With this lines I can access any Laravel function and I can check the login if I made GET request to the external scripts, but when the request is POST it always fails. I'm unable to check the login and I see that the session changes because can't get the existing session.
I have made many tests and I think that somethings of Laravel are not working fine, like routes or middlewares. I can made it work if I disable all encryption of the cookies and the session, but I want to use this security functions.
I'm using updated Laravel 5.7 and I had this code working in Laravel 5.4
Thank you for your help.
I discovered the problem,
The trick is that the route is external to laravel so laravel's route resolver identifies the current route as /.
It was working on GET requests because in my routes file I have the / route only as get. If I set the / route as any, everything works.
I wasn't seeing the problem because I was not terminating Laravel's execution. If I change the logged user verification to this, it shows the error:
$isAuthorized = Auth::check();
if(!$isAuthorized){
echo "NO AUTORIZADO";
$response->send();
$kernel->terminate($request, $response);
exit();
}
This two lines ends laravel execution and returns the error "405 Method Not Allowed".
$response->send();
$kernel->terminate($request, $response);
Thank you for your help.

Tell Joomla to be set to logged-in status via outside PHP script

I have a site that I am migrating from Joomla 1.7 to 3.3 and the previous site had a PHP script that when someone logged into a different server that handles all membership accounts (and completely separate from Joomla) to go back to the Joomla site and change to logged-in status as a registered user. How would I accomplish this in Joomla 3.3?
Here's a possible start (but incomplete):
$user = JFactory::getUser();
if($_GET['login']=='true'){
$user->guest = 0;
}
Something like this might work, but at the very least I don't know what all settings Joomla needs to determine that the current user is logged in.
Try below code:
if($_GET['login']=='true'){
$userid = $_GET['userid']; //This is the user id which you get from remote website.
$user = JFactory::getUser($userid);
$session =& JFactory::getSession();
$session->set("user",$user);
}
Using about code the user will be logged in on Joomla website.

Resources