Skip to content
This repository has been archived by the owner on Dec 26, 2021. It is now read-only.

Add API features - WIP #218

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Add API features - WIP #218

wants to merge 6 commits into from

Conversation

josephmancuso
Copy link
Member

The first iteration of API improvements in M4 will be pretty small. Just the requirements.

So to build an API using these features is something like this:

Step 1

You can create a normal controller or create a resource controller:

python craft controller UserController -r

Step 2

Create your route, could be normal routes or you can use the new resource method which maps several routes to the matching controller method:

ROUTES = [
    Route.resource('users', 'UserController')
]

Step 3

Add the authentication controller:

python craft api:install

This will scaffold out the authentication controller so you can modify it

Step 4

Call /api/auth with your username and password and get back a token which is saved to the users table. You can use this token to make requests.

The JWTAuthenticationMiddleware will verify this token is passed and that it is valid

Copy link
Contributor

@girardinsamuel girardinsamuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a quick review !

src/masonite/api/middleware/JWTAuthenticationMiddleware.py Outdated Show resolved Hide resolved
user = auth.attempt(request.input("username"), request.input("password"))

if user:
return {"data": user.generate_jwt()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here it means that a new JWT token is generated each time we make a request to this controller, replacing the token in user table with the newly generated ?

And what's the lifetime of this token ?

Also should we rename data to token ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Once you have a token you would not need to hit this endpoint anymore.

The lifetime of the token will be inside the JWT token which I have to add inside the generate_gwt() method. Somehow I still need to hook this all up to the normal feature convention we have so I have access to the configuration settings in the same way we do with other features

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep it would be great. because I feel we would like to define some settings to generate JWT.

@josephmancuso josephmancuso changed the title Add API features Add API features - WIP Dec 23, 2021
@josephmancuso josephmancuso self-assigned this Dec 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants