GithubHelp home page GithubHelp logo

Comments (10)

dillingham avatar dillingham commented on May 11, 2024 7

@narwy had this issue too.. easy fix is to update app/Http/Middlewares/Authenticate.php

protected function redirectTo($request)
{
    if($request->is('api/*')) {
        abort(401, 'Access denied');
    }

    if (! $request->expectsJson()) {
        return route('login');
    }
}

from sanctum.

n10000k avatar n10000k commented on May 11, 2024 3

@driesvints That's correct but if you do a clean install of laravel + require this package then visit the example route without the header you get a 500 error.

from sanctum.

n10000k avatar n10000k commented on May 11, 2024 2

@lloy0076 thats correct but if you don’t have a login page and use laravel as a api only solution for your spa and someone accessed that route from their browsers it defaults to a 500 error. Sentry and other tools love this.

@koenhoeijmakers it’s because the Authenticate class has the expectsJson statement, if not being the redirect to Login. But that shouldn’t be a default option. Passport has this same issue also. But I’m questioning it within Airlock now to see if it’s something we can fix going forward.

There’s obviously a way around it with a big of tweaking and adding a middleware etc but I’m just thinking of those already using it for SPA and could effect those error reporting tools if someone decides they want to access direct.

from sanctum.

lloy0076 avatar lloy0076 commented on May 11, 2024

I used a Laravel 6 installation, and setup Airlock.

I go to:

curl --location --request GET 'http://laravel-playground.test/api/user'
--header 'Authorization: Bearer hFiORTL2G9LAFQjn3b4rKadJZ1tFuJPUlkjIAr29rhngJqUbbImmN2FYTDcJrNjdpffpdXKWY1IEnDig '

And get:

{
    "id": 1,
    "name": "David Lloyd",
    "email": "[email protected]",
    "email_verified_at": null,
    "created_at": "2019-08-24 00:28:59",
    "updated_at": "2019-12-28 11:58:54"
}

Which is precisely what I expect...

composer.zip

from sanctum.

lloy0076 avatar lloy0076 commented on May 11, 2024

If I remove the authorisation header I get a dump of the Login page (as HTML) which is also what I'd expect.

from sanctum.

koenhoeijmakers avatar koenhoeijmakers commented on May 11, 2024

This is not an Airlock issue, this has something to do with the Authenticate middleware which defaults to a redirect to the login route, you're free to change the default to your needs.

from sanctum.

driesvints avatar driesvints commented on May 11, 2024

Passing the accept header is required to let the logincontroller return a json response.

from sanctum.

dillingham avatar dillingham commented on May 11, 2024

Wondering if having a middleware add it for api routes makes sense @driesvints 🤔

public function handle($request, Closure $next)
{
    $request->headers->set('accept', 'application/json', true);

    return $next($request);
}

from sanctum.

driesvints avatar driesvints commented on May 11, 2024

@dillingham I don't think it's wise to solve this with a middleware. API consumers should pass the correct headers.

from sanctum.

dillingham avatar dillingham commented on May 11, 2024

@driesvints yeah you're right :) probably best to just add withHeaders in setUp

from sanctum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.