DocTolly

DocTolly

Member Since 1 Year Ago

Experience Points
7,360
Total
Experience

2,640 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
68
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your 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-in-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 Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist 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 2
7,360 XP
Jul
14
3 months ago
Activity icon

Awarded Best Reply on Error: Call To A Member Function BelongsTo() On Null

Hi thanks for your help. I found the problem which was I was calling an load function in a controller which had the models wrong in it (i changed models after making that controller).

FYI the $this->assignment relationship seems to work?

Activity icon

Awarded Best Reply on Getting Axios To Automatically Use Passport Token (Laravel/React)

Does anybody know this magic? :)

Activity icon

Started a new Conversation Can Auth::user() Implement My Own Contracts As Well As Authenticatable?

Hello,

I have custom interfaces on my User model.

class User extends Authenticatable implements HasApiTokensInterface, CanAccessInterface

When I use Auth::user(), it only returns an implementation of Authenticatable contract.

I have tried creating macros in the RequestGuard that return User model of the individual contracts I want.

class RequestGuardProvider extends ServiceProvider
{
    public function register()
    {   
        RequestGuard::macro('tokenable', function(){

	    //This returns implementation of HasApiTokensInterface
            return (new TokenableUser(Auth::user()))->user();
        });

Obviously this just looks and feels wrong/messy.

Is there any way I can modify Auth::user() to implement all of the User model contracts instead of just Authenticatable?

Thank you

May
24
5 months ago
Activity icon

Awarded Best Reply on Can't Access Httponly Cookie In Cookie::get()

For anyone else who may come across this problem, add same-site: none when setting the cookie. https://web.dev/samesite-cookies-explained/

           Cookie::make(
            'cookie name',
            'cookie value',
            14400, // time to expire 
            null,
            null,
            false,
            true, 
            false,
            'none'//same-site   <-----
          )
Activity icon

Replied to Can't Access Httponly Cookie In Cookie::get()

For anyone else who may come across this problem, add same-site: none when setting the cookie. https://web.dev/samesite-cookies-explained/

           Cookie::make(
            'cookie name',
            'cookie value',
            14400, // time to expire 
            null,
            null,
            false,
            true, 
            false,
            'none'//same-site   <-----
          )
Activity icon

Replied to Can't Access Httponly Cookie In Cookie::get()

Instead of adding Cookie to queue, I have also tried manually sending it back in the response

return response()->json($response, 200)->withCookie($cookie);

Again, the cookie is sent in the headers and I can see it in the console, but it is not saved into storage and I can't recall it with Cookie::get on the refresh-token route

Activity icon

Replied to Can't Access Httponly Cookie In Cookie::get()

To add, I am using laravel-cors, this is my cors.php config


    'paths' => ['api/*'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['http://localhost:3000'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

My axios calls to the refresh-token route are using withCredentials:true

Activity icon

Started a new Conversation Can't Access Httponly Cookie In Cookie::get()

Laravel backend as API (rota.test), React Frontend (localhost:3000) (seperate from each other)

I create and queue a http only cookie successfully on hitting an authentication route. Access token / expiration time is returned to the user, and the refresh token is stored in a httponly cookie

    protected function setRefreshTokenCookie(string $refreshToken)
    {
       Cookie::queue(
           Cookie::make(
            self::$refresh_token_cookie_name,
            $refreshToken,
            14400, // 10 days
            null,
            null,
            false,
            true
            )
        );
    }

I can see see the cookie in the XHR request response http://prntscr.com/smwbez

When I send my user to a refresh-token route, the cookie cannot be retrieved (returns null)

    protected function getRefreshTokenCookie()
    {
        return Cookie::get(self::$refresh_token_cookie_name);
    }

I have AddQueuedCookiedToResponse in my API middleware. EncryptCookies is not in the APi middleware, but have tried adding the cookie name to encryption exceptions anyway (no luck)

Any explanation of why I can't retrieve the cookie?

May
22
5 months ago
May
21
5 months ago
Activity icon

Replied to Making My Controllers Easy To Understand, Where To Put Code?

To generalise your question and add to the answers above,

If you want to simply move code away from your controller, you can create responses and store in Http/Responses/ and pass it any extra info you need

class ExamsSendNotificationsResponse implements Responsable
{

   public function __construct(Exam $exam) {
	$this->exam = $exam; //you can now access this in the response 
}


    public function toResponse($request)
    {
     	//Your code would go here
    }
}

And in the controller you can call it something like this

    public function sendNotifications($request)
    {
	$exam = //some exam 

        return new ExamsSendNotificationsResponse($exam);
    }
Activity icon

Started a new Conversation Getting Axios To Automatically Use Passport Token (Laravel/React)

App Context:

  • Laravel API backend
  • React App frontend, contained within Laravel
  • Consuming my own API

Problem:

  • Making axios automatically use a bearer token or laravel token for authorisation

Current situation:

  • I can login via a login controller - (bare bones scaffolding, Auth::attempt()call which returns some json)
  • if I ->createToken()on login and return the token, then it works as a Bearer token (I manually add the token to axios headers in my React App)

My question is:

  • Can Laravel/Passport automate this authorisation/token process for me?
  • I don't want to re-invent the wheel if Laravel can automatically deal with this
  • I don't want to start manually messing around putting this token into storage and dealing with refreshing it etc

I have tried: (no luck)

Here are the http request headers automatically assigned by laravel/axios on any requests:

Cookie:  XSRF-TOKEN=___xyz____;  rota_session=___xyz____;

X-CSRF-TOKEN = ___xyz____

X-Requested-With: XMLHttpRequest

Accept: application/json

If I do not manually send the the bearer token (the one I manually created on login from ->createToken()), I get a 401 unauthorized

Happy to post more code snippets on request. Thank you.