Chris1981

Experience

15,080

0 Best Reply Awards

  • Member Since 2 Years Ago
  • 125 Lessons Completed
  • 0 Favorites

30th August, 2018

Chris1981 left a reply on Must Be An Instance Of Illuminate\Http\Request, Instance Of StdClass Given • 3 weeks ago

Im thinking that might be the best option sujancse

Chris1981 left a reply on Must Be An Instance Of Illuminate\Http\Request, Instance Of StdClass Given • 3 weeks ago

@tisuchi It's already there.

My controller imports are as follows;

namespace App\Http\Controllers;

use JWTAuth;
use App\Models\User;
use JWTAuthException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Tymon\JWTAuth\Exceptions\JWTException;

Chris1981 started a new conversation Must Be An Instance Of Illuminate\Http\Request, Instance Of StdClass Given • 3 weeks ago

Hey Guys,

I'm having a bit of an issue figuring out how to pass a Request object from my javascript application to my laravel backend.

In my javascript framework i am doing the following;

var formData = new FormData();
formData.append("email", username);
formData.append("password", password);

and then passing the formData to my laravel endpoint.

My authenticate function is just a standard;

public function authenticate(Request $request)

The problem is that everytime i try to run my code, i am getting;

Argument 1 passed to App\Http\Controllers\AuthController::authenticate() must be an instance of Illuminate\Http\Request, instance of stdClass given

Does anyone know how I can solve this?

26th July, 2018

Chris1981 started a new conversation Mapping A Site To A Folder Of Another Site In Homestead • 1 month ago

Hey Guys,

I currently have a javascript framework being spooled up on a URL thats mapped in Homestead.yaml just as an example, lets say www.mysite.test

I am building the API in Laravel for the JS framework to get its data from.

I want to be able to have that at www.mysite.test/api

Is there any way to achieve this using the Homestead.yaml, or would this require some in depth messing around?

Thanks

12th July, 2018

Chris1981 left a reply on Am I Testing The Logout Correctly. • 2 months ago

@tykus hey again hahaha .. I think i may need to hire you hahaha

I am using the following; "tymon/jwt-auth": "dev-develop" in my composer.json.

This is my route;

    Route::group(['middleware' => 'auth.jwt'], function () {
        Route::get('user', '[email protected]');
    Route::get('logout', '[email protected]');
    });

Im using the request for the logout method as well.

I know this is a big ask, but i used the following tutorial to learn to implement it, is it a bad tutorial? https://tutsforweb.com/restful-api-in-laravel-56-using-jwt-authentication/

It doesn't use the Authorization header at all

Chris1981 started a new conversation Am I Testing The Logout Correctly. • 2 months ago

So im new to testing, and i want to make sure im doing it right.

I'm using JWT-Auth for my API token generation, i want to test that a user gets logged out properly.

Could someone have a look at the following and let me know if its any good, i kind of feel like its not the right thing, but i dont know how to make it better

    public function it_logs_out_properly()
    {
        $user = factory(User::class)->create([ 'password' => bcrypt('foobar') ]);
        $payload = [ 'email' => $user->email, 'password' => 'foobar' ];

        $token = JWTAuth::fromUser($user);

        $this->json('GET', '/api/user', ['token' => $token])->assertStatus(200);
        $this->json('GET', '/api/logout', ['token' => $token])->assertStatus(200);

        $this->json('GET', '/api/user', ['token' => $token])->assertStatus(401);
    }

The /api/user endpoint routes to the following method;

    public function getAuthUser(Request $request)
    {
        // Check for a token in the request
        $this->validate($request, [
            'token' => 'required'
        ]);

        // Get the user details based on the token
        $user = JWTAuth::authenticate($request->token);

        return response()->json(['user' => $user]);
    }

Any guidance would be greatly appreciated.

Chris1981 left a reply on AssertJson Isn't Seeing The Json That Is Being Returned • 2 months ago

hahaha don't facepalm, the assertJsonFragment you suggested has made my testing so much easier now ... Much respect!

Chris1981 left a reply on AssertJson Isn't Seeing The Json That Is Being Returned • 2 months ago

Hey @tykus thanks for that, i didnt know assertJsonFragment existed, i had to add brackets around the messages though, but it worked ... The final working function is as follows;

    public function it_requires_name_and_email_and_password_to_register()
    {
        $response = $this->post('/api/register');
        $response->assertStatus(422)
            ->assertJsonFragment([
                'name' => ['The name field is required.'],
                'email' => ['The email field is required.'],
                'password' => ['The password field is required.'],
            ]);
    }

Chris1981 started a new conversation AssertJson Isn't Seeing The Json That Is Being Returned • 2 months ago

I'm trying to write a simple test to make sure the authentication is working correctly ( Laravel 5.6 ) as seen below;

public function it_requires_name_and_email_and_password_to_register()
{
    $response = $this->post('/api/register');
    $response->assertStatus(422)
        ->assertJson([
            'name' => 'The name field is required.',
            'email' => 'The email field is required.',
            'password' => 'The password field is required.',
        ]);
}

But for some reason its failing even though the request has the required information.

The following is what phpunit is returning;

There was 1 failure:

1) Tests\Unit\AuthenticationTest::it_requires_name_and_email_and_password_to_register
Unable to find JSON:

[{
    "name": "The name field is required.",
    "email": "The email field is required.",
    "password": "The password field is required."
}]

within response JSON:

[{
    "message": "The given data was invalid.",
    "errors": {
        "name": [
            "The name field is required."
        ],
        "email": [
            "The email field is required."
        ],
        "password": [
            "The password field is required."
        ]
    }
}].


Failed asserting that an array has the subset Array &0 (
    'name' => 'The name field is required.'
    'email' => 'The email field is required.'
    'password' => 'The password field is required.'
).
--- Expected
+++ Actual
@@ @@

         )

-    [name] => The name field is required.
-    [email] => The email field is required.
-    [password] => The password field is required.
 )

Am i missing something?

8th July, 2018

Chris1981 left a reply on Dealing With Redirections In Testing Environment • 2 months ago

Hey Guys,

Thanks for the answers, but how do i change the property value in the test?

If I do something like;

public function it_registers_successfully()
{
    $loginAfterSignUp = false;

    $user = factory(User::class)->create([ 'password' => bcrypt('foobar') ]);

    $payload = [ 'name' => $user->name, 'email' => $user->email, 'password' => 'foobar' ];

    $response = $this->post('/api/register', $payload);
    $response->assertStatus(200);
}

I still get the same result

Chris1981 left a reply on Dealing With Redirections In Testing Environment • 2 months ago

@lostdreamer_nl thanks for that ... But how would I change the value of loginAfterSignUp in a test if i move it to config?

Chris1981 started a new conversation Dealing With Redirections In Testing Environment • 2 months ago

I am using Laravel 5.6 with the JWT-Auth package, I have a simple test which checks a user is able to be registered, as follows;

public function it_registers_successfully()
{
    $user = factory(User::class)->create([ 'password' => bcrypt('foobar') ]);

    $payload = [ 'name' => $user->name, 'email' => $user->email, 'password' => 'foobar' ];

    $response = $this->post('/api/register', $payload);
    $response->assertStatus(200);
}

When i run the test i am receiving the following;

Expected status code 200 but received 302.
Failed asserting that false is true.

I am new to testing, but i think the problem is that the register function checks to see if the user should be logged in on a successful register and then does it, hence the redirection.

I know this is defeating the purpose and going backwards to how its meant to be done, but i would like some advice on how to deal with the redirection and see the 200 status.

The function is as follows;

public $loginAfterSignUp = true;

public function register(RegisterAuthRequest $request)
{
    // Build new user collection
    $user = new User();
    $user->name = $request->name;
    $user->email = $request->email;
    $user->password = bcrypt($request->password);

    // Save user
    $user->save();

    // Check if user should be automatically logged in
    if ($this->loginAfterSignUp) {
        return $this->login($request);
    }

    return response()->json([
        'success' => true,
        'data' => $user
    ], 200);
}

24th May, 2018

Chris1981 started a new conversation Purely API Implementation • 4 months ago

Hey Guys,

I'm setting up a purely API implementation on my Laravel install, I will also be using JWT for authentication and all that bollocks.

As i will be writing all new authentication logic, can i delete the Controllers/Auth directory that ships with Laravel or will that make things go tits up?

Is there anything else i can get rid of being a API only implementation?

I hope that makes sense.

Cheers,

12th March, 2018

Chris1981 left a reply on Passport + Auth • 6 months ago

Anyone?

10th March, 2018

Chris1981 started a new conversation Passport + Auth • 6 months ago

Hey Guys,

Before i go ahead with setting everything up, im wondering if its possible to use Laravel Passport and the standard Laravel Auth on the same project.

So that i can offer an API to people and also have members access for my frontend.

Cheers,

1st February, 2018

Chris1981 left a reply on Homestead Script For Installing Nginx On Centos • 7 months ago

@bobbybouwmann .. Sorry to bug you .. But if i take the installation scripts from the homestead provision script and place it in a scripts/install-nginx.sh will that run on every vagrant up? or just when i do a vagrant provision --reload

Chris1981 left a reply on Homestead Script For Installing Nginx On Centos • 7 months ago

Ohhh ... I can use that to rip out the install scripts ... I didnt think of that, doiiiiii.

Thanks @bobbybouwmann

Chris1981 left a reply on Homestead Script For Installing Nginx On Centos • 7 months ago

@bobbybouwmann that just provisions Ubuntu doesnt it? ... I need it for CentOS

Chris1981 started a new conversation Homestead Script For Installing Nginx On Centos • 7 months ago

Hey Guys,

Im trying to replicate the Homestead vagrant box on CentOS 7, im using the centos/7 box and it doesnt come pre installed with Nginx like Homestead does.

In the git repo for Homestead there is a scripts folder which configures all the services.

Has anyone built a create-nginx.sh or does anyone know how the commands to go through for Nginx with PHP7.2

Any help would be greatly appreciated.

Thank you

6th December, 2017

Chris1981 started a new conversation Laravel, TDD And ExtDirect • 9 months ago

Hey Guys,

Im wondering if anyone here uses ExtJS, and specifically ExtDirect.

Im curious to know peoples approach to testing their Laravel API when Ext Direct only uses one endpoint.

Any advice would be awesome

24th November, 2017

Chris1981 started a new conversation Laravel Vs Lumen • 10 months ago

Hey guys,

I want to build a single page JS app using ExtJS. To build my API i want to build it in Laravel, but ive just also come across Lumen. Is Lumen just a Laravel app without views? Would i be better to build my API with Lumen?

Any thoughts would be great

Cheers

Chris1981 left a reply on ExtJs 5 And Laravel 5 • 10 months ago

This is a little old, but did you ever find any resources? ... Im looking at building in both as well

11th October, 2017

Chris1981 started a new conversation Sharing One Codebase Over Multiple Domains • 11 months ago

Hey Guys,

I was wondering if anyone could shoot me some advice.

Is there any way to share one codebase over multiple domains and also extend that to the authentication.

For example, lets say I have 3 domains, domainone.com, domaintwo.com and domainthree.com

I want one installation of Laravel to serve content to these 3 domains, also, if someone log's in on domainone.com they are automatically logged in on the other domains, same for logout etc

Im hoping someone has come across this before.

Any help would be greatly appreciated.

Cheers,

15th August, 2017

Chris1981 started a new conversation Telling Vagrant Which Box To Run • 1 year ago

Hey Guys,

I was wondering if someone could help me.

Im trying to install 2 different homestead boxes for my projects ( PHP5.6 and 7 ).

I have downloaded the boxes and which i hit up vagrant box list i get the following output

laravel/homestead (virtualbox, 0.3.3)
laravel/homestead (virtualbox, 3.0.0)

Obviously 0.3.3 is the 5.6 box and 3.0.0 is 7.

But when i go into the directory for the 5.6 install and do a vagrant up, i get the following;

[2.0][~/Homestead/PHP5.6]$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'laravel/homestead'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'laravel/homestead' is up to date...
A VirtualBox machine with the name 'homestead' already exists.
Please use another name or delete the machine with the existing
name, and try again.

Is there a way i can tell each install to run a specific box?

12th August, 2017

Chris1981 left a reply on Help With Mutator • 1 year ago

The answer has been revealed.

In my model i did the follwing;

    public function getCreatorNameAttribute()
    {
        if ($this->creator) {
            return $this->creator->name;
        }

        return 'Anonymous';
    }

Then in the view

{{ $gallery->creatorName }}

Very very close all.

Thanks for all your input

Chris1981 left a reply on Help With Mutator • 1 year ago

@edoc Hmmmm .. im getting the same error

ErrorException: Trying to get property of non-object

Chris1981 left a reply on Help With Mutator • 1 year ago

@edoc Yeh, i can do that, but it means i have to do that throughout the app, I would like to learn how to take care of this automatically with the Mutators.

Chris1981 started a new conversation Help With Mutator • 1 year ago

Hey Guys,

I was wondering if someone could give me a bit of help.

I want to write a mutator that returns a users name as Anonymous if no user_id is present on the gallery table, but im not sure how to go about it

On my blade template I have the following;

<a href="#">{{ $gallery->creator->name }}</a>

The controller looks like this;

    public function show(Gallery $gallery)
    {
        return view('galleries.show', compact('gallery'));
    }

and the creator method in the Gallery model is as follows;

    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

Basically, every gallery a user_id field, but for the people not logged in, that field is set to null.

Any help would be awesome

11th August, 2017

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

Beautiful ... Alright, once again, onwards and upwards .. Its all learning :)

Thanks again and again and again hahaha

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

ok ... I needed to create the user

This is what i have come up with .. is this good?

    /** @test */
    public function a_gallery_has_a_creator()
    {
        $user = factory('App\User')->create(['id' => 1]);

        $gallery = factory('App\Gallery')->create(['user_id' => 1]);

        $this->assertInstanceOf('App\User', $gallery->creator);
    }

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

If i dd the $thread .. It spits out a user with an id of 1

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

That didnt work

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews Oh dear ... Im back again ... So so sorry bud.

My test suite it going tits up now.

Now that my model factory inserts null

'user_id' => null,

My test is failing

    /** @test */
    public function a_gallery_has_a_creator()
    {
        $gallery = factory('App\Gallery')->create(['user_id' => 1]);

        $this->assertInstanceOf('App\User', $gallery->creator);
    }

The error im getting is

Failed asserting that null is an instance of class "App\User".

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews you sir, are a true legend ... Thank you so much for all your help, i really do appreciate it

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

Hey @danmatthews yeh thats what i was thinking, the only problem i see is that the test would fail wouldn't it?

    /** @test */
    public function a_thread_has_a_creator()
    {
        $this->assertInstanceOf('App\User', $this->thread->creator);
    }

9th August, 2017

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews Hey Mate, really really really sorry to bug you again, but i was wondering if you could help me with the last part of the puzzle im trying to solve.

Now that a Thread can belong to a 'null' user. in my Thread model i have the creator() method to associate that Thread with a user. Im guessing this has to be re-written somehow. How would you approach this bit.

This is the original code

public function creator()
{
    return $this->belongsTo(User::class, 'user_id');
}

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews damn that makes sense hahaha

Thank you very much

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews oh that makes sense.

So in my model factory define i would just do

'user_id' => null,

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews ... Sorry to bug you again.

Would you know how to setup the Model Factory now that things have changed.

At the moment it is as follows;

$factory->define(App\Thread::class, function ($faker) {

    return [
        'user_id' => function () {
            return factory('App\User')->create()->id;
        },
        'title' => $faker->sentence,
        'body' => $faker->paragraph
    ];

});

If the user_id can now be both filled and nullable

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews .. nice, thank you for that. Its given me something to work on.

Cheers,

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews ... Awesome, i get where your coming from.

Leaving the user_id in the replies field can be left because only logged in users are allowed to comment.

That works.

Instead of a cookie, what about adding the IP address to the database, but i guess if a user has a dynamic IP their posts wouldn't be associated.

Chris1981 left a reply on Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

@danmatthews ... Thank you for the input, do you think this approach would be better than giving all 'non logged in' users a generic user account with an obscure id?

8th August, 2017

Chris1981 started a new conversation Anybody Hitting An Endpoint Can Add A Thread • 1 year ago

Hey Guys,

Im wondering if someone could help me out a little.

I am following Jeffery's tutorial on 'Let's build a forum with Laravel and TDD' -> https://laracasts.com/series/lets-build-a-forum-with-laravel

In the beginning we used the authentication scaffolding to set up user accounts.

Now its on episode 6 and we are allowing users to publish threads. A user has to be logged in to publish a thread and the thread is then associated with that user.

I want to be able to allow everyone to post a thread, not just logged in users, but random users with no account.

What would be the best way to approach this? I would love to hear from some people, im really struggling to figure out how to do this.

7th August, 2017

Chris1981 started a new conversation Random URL From Submitted Form • 1 year ago

Hey guys,

I was wondering if anyone could point me to a tutorial or something that would help me with the following;

  1. User submits a form with information, for example, title, body, image
  2. The user is taken to a page that displays the submitted information, but is also given a random URL segment to share, kind of like how Imgur and Pastebin works.

Any help would be great.

Cheers,

4th January, 2017

Chris1981 started a new conversation Adding An 'active' Column To The Users Table And Checking Before Login • 1 year ago

Hey Guys,

I have added an 'active' column to my users table like so

$table->enum('active', ['1', '0'])->default('0');

Now i want the auth system to only allow users to login that have an active status of 1

I found a snippet of code online which says to override the default login procedures, but it seems to be a bit outdated and doesn't work with 5.3

The code is below ... Has anyone done this with the latest version of Laravel that could share some code.

public function postLogin(Request $request)
{
    $this->validate($request, [
        'email' => 'required|email', 'password' => 'required',
    ]);

    $credentials = $this->getCredentials($request);

    // This section is the only change
    if (Auth::validate($credentials)) {
        $user = Auth::getLastAttempted();
        if ($user->active) {
            Auth::login($user, $request->has('remember'));
            return redirect()->intended($this->redirectPath());
        } else {
            return redirect($this->loginPath()) // Change this to redirect elsewhere
                ->withInput($request->only('email', 'remember'))
                ->withErrors([
                    'active' => 'You must be active to login.'
                ]);
        }
    }

    return redirect($this->loginPath())
        ->withInput($request->only('email', 'remember'))
        ->withErrors([
            'email' => $this->getFailedLoginMessage(),
        ]);

}

3rd January, 2017

Chris1981 started a new conversation User Registration And Stripe Cashier Payment In One • 1 year ago

Hey Guys.

Have any of you used a self-hosted stripe cashier form and user registration in one step?

I was wondering if you could outlay the steps that you used.

Im using the following, but am unsure if i am doing it right

  1. Use a try catch block to register user with an inactive status
  2. Use a try catch block to submit the payment to stripe
  3. Use a try catch block to change the user status from inactive to active

So, in this scenario i am using 3 different try catch blocks.

If anyone has done the cashier and user registration in one form, could they give me a bit of insight.

Cheers,

30th December, 2016

Chris1981 started a new conversation Self Hosted Cashier Form With User Registration In One • 1 year ago

Hey guys,

I want to create an app that allows users to select a plan, then signup and pay on the same form using a self hosted cashier form.

Does anyone know of any tutorials on this? I can only find tuts with the stripe popup form or doing it in a 2 step process.

Any help would be grand.

Peace

29th December, 2016

Chris1981 left a reply on Retain User Input Through Pages • 1 year ago

@leber yeh i was thinking the same thing, just know if there was a better solution.

Thanks :)

Chris1981 started a new conversation Retain User Input Through Pages • 1 year ago

Hey Guys,

I was wondering if i could get some advice.

What would be the best way to retain user input through several pages to insert into a database after the last page submit?

For example:

Say i have a small app that is backing up a users database, on the first page, they enter their host, username, password etc once they click submit it goes to the next page where they select the DB they want to back up, then on the next page it lets them select which tables from that db to backup ... Once they hit submit on that page it all gets collected and saved into the database.

What would be the best way to save that data through each of the pages?

Cheers

15th December, 2016

Chris1981 started a new conversation Queue Not Running In Background • 1 year ago

Hey guys,

I was wondering if i could get some help figuring out this queue business.

I have a bunch of stuff that i need a user to select, then i want to send it to a queue for the system to do in the background without any effect on the user experience.

So in my controller i write the following code

dispatch(new UserTriggeredProcess($id));

Then in my UserTriggeredProcess class, all the things that i want to do get processed.

I have selected the 'database' queue setting and migrated the queue tables.

then in my terminal i run

php artisan queue:work

Im not getting any errors from any of it, but the code that i have defined in the UserTriggeredProcess is not running in the background and the user is being made to wait until that process finishes which could be minutes.

Any help on what im doing wrong?

Cheers,

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.