audunru

audunru

Senior Content Advisor at Netlife Design

Member Since 3 Years Ago

Oslo

Experience Points
19,390
Total
Experience

610 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
143
Lessons
Completed
Best Reply Awards
7
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 4
19,390 XP
Oct
10
3 months ago
Activity icon

Replied to Store Business Hours Of A Company

Of course they did... :-)

Activity icon

Replied to Store Business Hours Of A Company

From experience, store each open and close time separately in the database. That way you can query easily for stores open now, doing things like closing in 30 minutes, etc. Make it human readable in the front end, not when you store it. As for naming the inputs, I dont know... perhaps using the day names makes it a little more explicit, otherwise you can have people looking at your code wonder if 0 is Monday or Sunday. But I think you can feel quite confident if you make it an array with 7x2 entries, thats not very likely to change.

Oct
08
3 months ago
Activity icon

Replied to Understanding Package.Json

The «Laravel way» is to build for production locally, then commit the built JS and CSS, push and deploy to production. Im generalizing here...

You should however commit your package-lock so that any others use the same versions of npm packages as you.

But the presence of this file does not mean that Laravel Mix runs on deploy magically. To do that you would have to add that step to your deploy, and typically you wouldnt do that with Laravel.

Now, if we were talking about deploying to Heroku, Netlify or some serverless platform, things would be different.

If you ARE building your assets on every deploy, then I suggest you move dev dependencies to dependencies, as from the looks of it you will need all of them to build your app.

Oct
03
3 months ago
Activity icon

Replied to How I Can Store Data In Customise Table After Google Authentication

Then I bet something goes wrong here (copied from the tutorial). $user is null in your case, I guess. So you need to debug why $user is null. Please include some code if you want help.

    public function callback(SocialGoogleAccountService $service)
    {
        $user = $service->createOrGetUser(Socialite::driver('google')->user());
        auth()->login($user);
        return redirect()->to('/home');
    }
Activity icon

Replied to How I Can Store Data In Customise Table After Google Authentication

It makes no sense at all to have a model called "SocialGoogleAccount" with a table name of new_users.

Is this the tutorial you're following? https://medium.com/@confidenceiyke/laravel-5-8-google-socialite-authentication-a8b57aa59241

If so, I think you should try again from the beginning, and do exactly as the tutorial says. Then, if you run into a problem, show us your code and if you've changed anything compared to the tutorial, you must explain why.

Sep
28
3 months ago
Activity icon

Replied to How I Can Store Data In Customise Table After Google Authentication

The social login, account or whatever you want to call it should be a separate model. Then a User should have a hasMany relationship with the social login. The provider and provider ID are attributes of the social login.

On login via Google, you look up a matching social login and get the user it belongs to.

Thats the approach taken in various packages that simplify what youre trying to do, including https://packagist.org/packages/mad-web/laravel-social-auth and https://packagist.org/packages/devmi/easy-socialite and https://packagist.org/packages/audunru/social-accounts (which Ive written)

Sep
27
3 months ago
Activity icon

Replied to Guzzle Http Client Request

I dont see billing_last_name in your JSON array.

Sep
26
3 months ago
Activity icon

Replied to Socialite With Google Complete Security

The Google Documentation has some good advice, but I believe they are mostly concerned with a situation where your backend (eg. Laravel) accepts a ID directly from the user.

Socialite gets its user info from Google's endpoint by sending a token: https://github.com/laravel/socialite/blob/4.0/src/Two/GoogleProvider.php#L61

I found a good description here https://www.oauth.com/oauth2-servers/signing-in-with-google/verifying-the-user-info/ if you scroll down to "Usingthe Access Token to Retrieve User Info"

Aug
30
4 months ago
Activity icon

Replied to Allowing Session-based Users To Access API - Is There A Nicer / Safer Way?

You need to take a look at https://laravel.com/docs/5.8/passport#consuming-your-api-with-javascript

You shouldn't have to add API routes to web.php. Your "own" users should authenticate in the way described in the link, and then they can use the regular API routes just like external parties who use access tokens.

Aug
21
4 months ago
Activity icon

Replied to Does The Built-in Token On Laravel (api_token) Expires?

If youre talking about the default API token, then the answer is no. You need to add it to the users table yourself, so unless you add some way to update it, it wont change.

Aug
18
5 months ago
Activity icon

Replied to Can't Get Store Api Call To Work.

Also wondering about what you're actually passing in the request... Is it a nested array?

Does

$Pemex = Pemex::create([
    "Economico" => "TLN-42"
    // anything else you need
]);

work? Or give a different error?

Aug
16
5 months ago
Activity icon

Replied to Slug Validation

I use https://www.npmjs.com/package/@sindresorhus/slugify in the frontend to do this.

Aug
12
5 months ago
Activity icon

Replied to Different Driver Locally Vs Production

Put different values in the .env file on your computer and the production server. Thats what its for. And if you do this, you need some way to check your migrations etc against something more realistic before you deploy to prod.

Activity icon

Replied to How To Make Any Route Except For '/login/'

Just add a Route::get() to your login route on the line above your any route.

The any route does not need to exclude the login route as long as its the last route in the file.

Aug
11
5 months ago
Activity icon

Replied to Would Like To Get Feedback In My First Package

Will answer what I can...

  1. That's a very good question to consider. For the sake of learning how, you could start with looking at how you can avoid relying on Laravel-specific helpers like config(). There's a section in this video about just that.

  2. If you're thinking of UrlShortenerService.php I don't think it's very long and it's quite readable so I wouldn't worry.

  3. I think it's totally fine, but it's also quite common to put your "Service" directly in the src/ directory together with the ServiceProvider. I guess since usually a package exposes one main "Service". Personally I think your folder structure makes sense, looks very similar to a lot of packages out there. Which is a good thing.

Other than that, I think perhaps it's time to add tests? Take a look at PHPUnit, https://travis-ci.org, https://styleci.io/ and https://coveralls.io/, those are good tools that will instill confidence in anyone considering your package.

Thanks for sharing.

Aug
10
5 months ago
Activity icon

Replied to Laravel Socalite - HandleProviderCallback Is Never Triggered

Logged in and looked at the options they have on their developer page, it should definitely work on localhost without https, but there are a lot of options, more than there used to be. So go through all of them and double check, I guess? That's my best guess now...

Activity icon

Replied to Laravel Socalite - HandleProviderCallback Is Never Triggered

At least with Google before, it took some minutes before the callbacks started working after changing them. It's not like that now as far as I know. Don't know how fast Facebook is, but perhaps you should wait a minute or so after changing a setting.

Activity icon

Replied to Laravel Socalite - HandleProviderCallback Is Never Triggered

And yeeet another thing you may want to look at is this: https://developers.facebook.com/blog/post/2018/06/08/enforce-https-facebook-login/

I think I last integrated with Facebook back in 2016 or 2017, so this was news to me. But looks like you may want to take a look at your settings to allow http:// and localhost.

Activity icon

Replied to Laravel Socalite - HandleProviderCallback Is Never Triggered

But more importantly, your Laravel config is wrong.

'redirect' => 'http://localhost/',

should be

'redirect' => '/login/facebook/callback',

and then in your Facebook developer setup, you should add the callback URL http://localhost:3000/login/facebook/callback (with the correct port number as I wrote in my previous post).

Activity icon

Replied to Laravel Socalite - HandleProviderCallback Is Never Triggered

Are you using php artisan serve on port 8000 and browsersync on port 3000? In that case, you should set the port in your urls to 8000, because that is what Socialite will think youre using.

Activity icon

Replied to Laravel Socalite - HandleProviderCallback Is Never Triggered

By the way, as a final step in handleProviderCallback, you should redirect to a page (like the homepage) in your app. See my take on this in https://github.com/audunru/social-accounts/blob/master/src/Controllers/ProviderController.php#L50

Activity icon

Replied to Laravel Socalite - HandleProviderCallback Is Never Triggered

Facebook does not connect to the callback route, it redirects you to the url after a successful authorization on their end. The callback url can definitely be on localhost, as long as you add it to the list of allowed URLs. The question is if you are ever redirected to the callback route? If not, the method wont be executed. If you are redirected, then something is wrong with your routes. But that code is not included in the original post.

Aug
07
5 months ago
Activity icon

Replied to Relation Between Same Users Table

The key (pun intended) to solve this is to specify the coach_id as the column name when you setup the relationships. You need to add that column in the database.

class User extends Authenticatable
{
    public function coach()
    {
        return $this->belongsTo('App\User', 'coach_id');
    }

    public function runners()
    {
        return $this->hasMany('App\User', 'coach_id');
    }
}
Aug
05
5 months ago
Activity icon

Replied to Format Date Using Moment Or Vue

A quick tip, the localized formatting options are pretty neat if you need to support multiple languages. Check the section "Localized formats" in the link @cronix gave you.

If you can live with selection of formats moment offers, such as moment().format('L') it makes it very easy to offer the right date format for different locales.

Aug
04
5 months ago
Activity icon

Replied to What Test Choose Between Unit , Feature And Dusk Test ?

Unit tests, to me, have primarily been useful to detect errors in my business logic. In my primary project, I've got lots of business rules around taxes and calculating them, and believe me, when I changed the way I store currencies in my database it was very useful to see that the tests still passed...

Feature tests have, in that same project, mostly been useful for testing the API. Not all parts of an API are used by myself, so to have tests that cover even the lesser used parts of it are useful.

I've got some Dusk tests, but maybe it's just me, but when they fail it's 90% of the time something with the test and not the code...

Finally, I've got some Jest unit tests for my Vue components.

To summarize, I fully agree with @mikenewbuild . What type of tests you need depend on the project. I think in my case I've overdone it a little, but at least I've learned a lot.

Aug
03
5 months ago
Activity icon

Replied to Adding Social Authentication To API

In this case you would use https://my-backend-domain.com/login/google/callback as the callback URL.

I imagine the flow would be like this:

  1. User loads frontend. It tries to access the API in the backend, if unsuccesful it redirects user to https://my-backend-domain.com/login/google
  2. Backend redirects to Google for authorization.
  3. Google redirects to https://my-backend-domain.com/login/google/callback
  4. Backend creates user or finds user and signs in
  5. Backend redirects to frontend, and passes along a fresh token that can be used to make requests.

This would basically be the same flow as when you have your backend and frontend on the same domain, but in step 5 in this case you have to have some way of passing the token to the frontend. If you were on the same domain, you would use the web middleware that Passport has that automatically includes a token in a cookie, but since you're on another domain you can't access the backend's cookies from the frontend domain...

Activity icon

Replied to Adding Social Authentication To API

Maybe I misunderstood something when I first read your post. You're consuming your own API from a React application on a different domain?

I think in that case you would have to redirect to your react app and pass it the token once the user has been created or logged in.

I've modified some code from one of my own projects to approximate what you're trying to achieve. It's not a 1:1 with your situation as in my case we only allow Google logins from existing employees, but I think you get it. :-D

GoogleLoginController.php:

<?php

namespace App\Http\Controllers\Auth;

use App\Employee;
use App\Http\Controllers\Controller;
use App\Http\Traits\FindOrCreateUser;
use Illuminate\Support\Facades\Auth;
use Socialite;

class GoogleLoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use FindOrCreateUser;

    protected $provider;
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     */
    public function __construct()
    {
        $this->provider = 'google';
    }

    /**
     * Redirect the user to the Google authentication page.
     *
     * @return \Illuminate\Http\Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver($this->provider)->with(['hd' => env('GOOGLE_HD')])->redirect();
    }

    /**
     * Obtain the user information from Google.
     *
     * @return \Illuminate\Http\Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver($this->provider)->user();

        abort_unless($user->user['hd'] === env('GOOGLE_HD'), 401, 'Domain is not allowed');
        abort_unless(Employee::where('email', $user->email)->first(), 401, 'User is not in employee database');

        $authUser = $this->findOrCreateUser($user->email, $user->name, $this->provider, $user->id);
        Auth::login($authUser, true);

        $token = $authUser->createToken('Token Name')->accessToken;

        return redirect('https://my-frontend-domain.com/dashboard?access_token='.$token);
    }
}

FindOrCreateUser.php:

<?php

namespace App\Http\Traits;

use App\User;
use App\UserRole;

trait FindOrCreateUser
{
    public function findOrCreateUser($email, $name, $provider, $provider_id)
    {
        if ($user = User::where(['email' => $email, 'provider' => $provider, 'provider_id' => $provider_id])->first()) {
            return $user;
        }

        return User::create([
            'name'     => $name,
            'email'    => $email,
            'provider' => $provider,
            'provider_id' => $provider_id,
            'user_role_id' => UserRole::where('name', 'employee')->pluck('id')->first(),
        ]);
    }
}

With this example, I imagine you would do the following:

  1. In your frontend app, if you already have an access_token, make an API request to https://my-backend-domain.com/api/some-resource.
  2. If the response is 401 Unauthorized, redirect the user to https://my-backend-domain.com/login/google or whatever.
  3. That endpoint corresponds to the handleProviderCallback() function, which redirects you back to https://my-frontend-domain.com/dashboard?access_token=bla-bla-bla with a new access_token. You can store that token in a cookie if you'd like and use that to make requests to the API.
Aug
02
5 months ago
Activity icon

Replied to Adding Social Authentication To API

Hmmm, I think you should just be able to login the user in Laravel after a successful registration or social login and not deal with creating tokens manually. The token necessary to make further requests to the API can be added automatically by Passport, take a look at https://laravel.com/docs/5.8/passport#consuming-your-api-with-javascript

Aug
01
5 months ago
Activity icon

Replied to Writing A Mock Api For Algolia Testing (ideas And Feedback Needed)

I'm sure you can set up multiple indexes or whatever they are called in Algolia, and then run your tests against your dummy index.

I just finished watching this https://laracasts.com/lessons/to-mock-external-services and it's basically the same reasoning behind why I wrote my tests to connect to the API.

Here are some test examples from the project I was referring to: https://github.com/audunru/fiken-api-php-client/blob/master/tests/Feature/ContactTest.php

Jul
31
5 months ago
Activity icon

Replied to Writing A Mock Api For Algolia Testing (ideas And Feedback Needed)

I was in the same situation recently. Not Algolia, but something else. I ended up writing tests that run against a sandboxed version of the external API. I thought about mocking the external API responses (I.e. saving the JSON response to a file and just using that from then in), but decided against it as there were just so many unknowns about integrating with the external API that I felt I had to actually connect to it to check that my code does what its supposed to. I guess when my own code is stable I could revert to mocking it, but in the process of learning how the external API works I thought this was a good approach.

Activity icon

Replied to Consuming Payment Gateway API In Laravel Controller

That error is from sandbox.gateway.com. You should probably dump the full response from the gateway to see if they included an error message.

$response = $payment->post($uri, $paylaod);

$contents = $response->getBody()->getContents();

dump($contents)

Run php artisan dump before hitting the endpoint to see the contents in your console.

Jul
30
5 months ago
Activity icon

Replied to Telescope Data Pruning Outside Of Php Artisan

When I run php artisan telescope:prune --hours=12 is this a one-time event? Or does this mean that, for this Laravel project, all entries older than 12 hours will be pruned going forwards?

It's a one time event.

I'm assuming the above is a one-time event and that I need to include $schedule->command('telescope:prune --hours=12'); somewhere in my Laravel code is I want pruning to happen when the app is run. If this is the case, where should I include the code?

It should go in app/Console/Kernel.php. Take a look at https://laravel.com/docs/5.8/scheduling#defining-schedules

In the code with the $schedule variable, I have excluded ->daily() because I am running the "prune" at intervals of less than daily. Is this correct?

I can see what you mean, but if you omit daily() you need to tell how often you want the command to run.

As you can read in https://laravel.com/docs/5.8/scheduling#introduction the scheduler should be setup to run every minute. This just means that Laravel will run every minute, checking which tasks in Kernel.php should be run. So you could add $schedule->command('telescope:prune --hours=12')->daily() or perhaps $schedule->command('telescope:prune --hours=12')->hourly() if you want to get rid of old data quicker.

Activity icon

Replied to Why Laravel Over Any NodeJS Framework For Your SPA?

Ive used Express and Laravel. I personally like that Laravel is more opinionated. Want to send an email? Heres how you do it in Laravel. In Express, I feel I cant just check the documentation to learn how to do these things that I see myself doing in almost all projects.

I guess like many others, I had almost two decades of PHP experience before I started getting serious with JavaScript. So I also think like in any discussion online about this, if you know one language and not the other, you will probably be faster if you use the one you know...