Logging a user in manually on Laravel 5 - php

I'm using facebook JS to log in users to a laravel 5 application.
I'm able to add the information to the database correctly but I'm trying to log the user in manually and can't figure it out.
Here is my controller:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\lib\TopAdditions;
use App\Http\lib\GeneralFunctions;
use App\User;
class FacebookLoginController extends Controller{
public function FacebookRegistration(){
$facebook = new User();
$isThereAUser = User::where('name', $_GET['name'])->get();
if(!count($isThereAUser) > 0){
$facebook->name = $_GET['name'];
$facebook->email = $_GET['email'];
$facebook->password = sha1($_GET['password']);
$facebook->remember_token = $_GET['token'];
$facebook->save();
$id = User::where('name', $_GET['name'])->get();
$this->LoginUser($id);
}
}
public function LoginUser($id){
\Auth::login($id[0]['id'],true);
}
}
When I run that and there is no such user in the database, the user is created and I get this message when the manual login is attempted:
ErrorException in Guard.php line 425:
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, integer given
I've been searching the web for similar situations but couldn't find anything that will help...
Please help me!

You should pass the user object, not the id, to Auth::login. So just change your function to be like this:
public function LoginUser($user){
\Auth::login($user, true);
}

I had the same problem, this is the solution that worked for me in Laravel 5.4:
public function LoginUser($id){
$user = User::find($id);
auth()->login($user);
}

Related

How to get id from refresh token in Laravel Passport?

I am writing a controller in which I would like to get the refresh token id but I just cannot figure out how to do it in laravel.
There are no problems with decrypting the access token. I am using standard protocol and receive data using the public key of the passport.
i tried different ways.
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class RefreshTokenController extends Controller
{
public static $public_key;
public static function refresh_token(Request $request)
{
self::$public_key = file_get_contents(storage_path() . DIRECTORY_SEPARATOR . 'oauth-public.key');
$refresh_token = $request->get('refresh_token');
$data = base64_decode($refresh_token);
$public_key = openssl_get_publickey(self::$public_key);
openssl_public_decrypt($data, $decrypted, $public_key);
dd($decrypted);
}
}
I tried also standard decrypt();. But it gives an error too.
Example of incoming data.
def50200dc11ab81fdb16530806530f85fe41f7facc934447589b3a0daa202d56f5b4a8265db94b418e09280bcc8a05c88c3817612200d3c28de15c9adfd4a213f7cb136fca6f78099d686fce8d5ddf93f9bb29c1df081d7f7b75394875b5287a7ca5d9ca5eb513ea13a26195aeda41c897832467ec1b0348cdb3e5ed9476e5e7d8712b8628c18ee8ffb525de965faeb3f25b1a8eb4ee904a15341650fe6d3ed901600beda0f80c71b9b607f3e549ba8fbd9eda0e02fc7647ab8b4ea9161f7eb65833bfe46fb7f3e116a207282f947660ead392dc78ad2b9501331b4d28433a421a4193484e113baf8050d3878bd94b6ac860a1a766e79877f1af8d3ab5aee3779bf33c2a7e347552e51bb8b5448585435ef7b79d06853bcd775ce781b8d2e56289dbc8d4a10ec507aecde19bb496e90f88a687ee9bb33755046d50f860f7723d87bd4c079cd4d1242903bdb3ca447ce6ab03f2fd800182d0d8c5a3b7de5402ba2ecb4058d458cbee94da433635877e63120d6418e51eafdfd763191fceef399c16c088e27da9ffbf9
according to oauth_refresh_tokens table. All refresh token IDs are preserved. When calling the refresh token method, according to the refresh token, the corresponding access token matches in the database and is renewed.
I will be grateful for solutions.
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Defuse\Crypto\Crypto;
use Exception;
use Illuminate\Http\Request;
class RefreshTokenController extends Controller
{
public static function refresh_token(Request $request)
{
$refresh_token = $request->get('refresh_token');
$app_key = env('APP_KEY');
$enc_key = base64_decode(substr($app_key, 7));
try {
$crypto = Crypto::decryptWithPassword($refresh_token, $enc_key);
} catch (Exception $exception){
return $exception;
}
return json_decode($crypto, true);
}
}
Laravel Passport encrypts the password using the APP_ located in the ENV file.
Using the library Defuse\Crypto\Crypto;

Fatal error: Class 'App\User' not found in public_html/laravel/app/CreateUser.php

I did a very simple CreateUser.php under app folder as follows.
$user = new App\User;
$user->name='John Doe';
$user->email='john#example.com';
$user->password=bcrypt('password');
But I got error message Fatal error: Class 'App\User' not found in public_html/laravel/app/CreateUser.php. Any ideas? TIA.
You have to include User model first to work your query. See the link below, you will get good idea how to use it.
https://laravel.com/docs/5.2/controllers
Put this in your routes
Route::get('/createuser', function () {
'as' => 'createuser',
'uses' => 'UserController#creteUser'
});
In your controller create UserController.php
use App\User;
public function creteUser(User $user)
{
$user->name='John Doe';
$user->email='john#example.com';
$user->password=bcrypt('password');
$result = $user->save();
if($result){
return redirect ('createuser')->with('success', 'User Created Successfully');
}
return redirect ('createuser')->with('error', 'Failed to create User');
}
Have You tried
$user = new User;
instead of
$user = new App\User;
and, Make sure you are created model (User)and also specify namespace (use App\User) at the top of your controller.

how to perform sum() on two variables in laravel 5.2

$store_obj = new account;
$x = \Auth::user()->id
$old = account::where('id',$x)->first();
echo $old->wallet;
$new = Input::get("update");
echo $new;
$upd = sum($old,$new);// strucked here
echo $upd;
I couldn't find sum function in Laravel helper docs. AFAIK, sum is one of aggregate method of query builder. I don't get on what you're trying to achieve, but I'll just assume you're going to update the model with adding those model value with given input value.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\account;
use Auth;
use App\Http\Requests;
class YourController extends Controller
{
public function update(Request $request)
{
$update = $request->input('update');
$id = Auth::user()->id;
$account = account::where('id', $x);
$account->increment('wallet', $update); //<---
$updated = $account->first();
return $updated;
}
}
I don't recommend the usage of native PHP echo, print_r or var_dump for debugging purpose. Use built-in Laravel dump or dd instead.
Of course, from code above, you need to validate user input before storing to DB.

Laravel 5 input::get error

Why am I having this error? I tried to add use Illuminate\Http\Request;
but same error? Also is $username = $request->input('username'); the same as $username = Input::get('username');?
The error I get is:
FatalErrorException in LoginController.php line 22: Class 'App\Http\Controllers\Input' not found
class LoginController extends Controller {
// Display the login form
public function showLogin()
{
return View::make('login');
}
// Process submission of the login form by verifying user’s credentials
public function processLogin()
{
$username = Input::get('username');
$password = Input::get('password');
if ($username === 'prince' && $password === 'c#spiAN') {
return 'Access granted!';
} else {
return 'Access denied! Wrong username or password.';
}
}
}
Add use Input; after Namespace ... and then use as Input::get(..) or don't add use and call \Input::get(..). Find more here: http://php.net/manual/pl/language.namespaces.php
You have to follow the namespace, as others told you in the comments.
Imagine it as a capitalized folder structure, now you are under App\Htt\Controllers, so it looks for App\Http\Controllers\Input.
Anything that is not directly under the same namespace must be referenced before the class declaration or prefixed with the full namespace adding a backslash \ at the beginning.
It depends on how many times you need to use it. If just once, then call it using the full path from the root. In this case it would be \Input (\Input::get(...)), otherwise reference it on the top and call it like you did in the question's code.
To better understand, in case of a model you'd need to call the class User as use App\User; on the top or directly as \App\User::all()(as example) inline in your method's code.

How to mock Auth in a Laravel 5.2 Model

I'm trying to write some unit tests for a brand new mini app. I usually write functional tests so this is me branching out to try and do it properly with mocking and stubs and all those things that make it just about the code.
The model looks like this :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class myModel extends Model
{
public static function getUser()
{
$user = \Auth::user();
return $user->adldapUser->cn[0];
}
}
And the test :
class MyModelTest extends TestCase
{
public function testGetUser()
{
$mockResult = new StdClass();
$mockResult->adldapUser = new stdClass();
$mockResult->adldapUser->cn=array("test");
$auth = $this->getMock('Auth');
$auth
->method('user')
->will($this->returnValue($mockResult));
$this->assertEquals('test',\App\MyModel::getUser());
}
}
But when I run the unit test I get the following error message :
There was 1 error:
1) MyModelTest::testGetUser ErrorException: Trying to get property of
non-object
/home/aidan/web/vagrant-web-dev/src/apps/orcid/app/MyModel.php:61
/home/aidan/web/vagrant-web-dev/src/apps/orcid/tests/MyModelTest.php:18
and if I post out $user it's NULL.
What am I doing wrong here?
Here I go again answering my own question half an hour after asking it.
For prosperity's sake I'll leave it here.
The answer was in this post here : https://stackoverflow.com/a/17602763/808124
So I replaced the $this->getMock with
Auth::shouldReceive('user')->once()->andreturn($mockResult);
Working
I use it this way:
$user -factory(User:class)->create();
\Auth::shouldReceive('guard')->andReturnSelf()
->shouldReceive('user')->andReturn($user)
->shouldReceive('check')->andReturn(true);

Categories