-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d61bb6e
commit b506fc8
Showing
2 changed files
with
46 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from starlette.endpoints import HTTPEndpoint | ||
from starlette.exceptions import HTTPException | ||
from starlette.responses import JSONResponse | ||
from starlette.requests import Request | ||
from piccolo.extensions.user.tables import BaseUser | ||
|
||
from .tables import TokenAuth | ||
|
||
|
||
class TokenAuthLoginEndpoint(HTTPEndpoint): | ||
async def post(self, request: Request) -> JSONResponse: | ||
""" | ||
Return a token if the credentials are correct. | ||
""" | ||
json = await request.json() | ||
username = json.get("username") | ||
password = json.get("password") | ||
if username and password: | ||
user = await BaseUser.login(username=username, password=password) | ||
if user: | ||
token = ( | ||
TokenAuth.select(TokenAuth.token) | ||
.first() | ||
.where(TokenAuth.user == user) | ||
) | ||
return JSONResponse({"token": token}) | ||
else: | ||
raise HTTPException( | ||
status_code=401, detail="The credentials were incorrect" | ||
) | ||
else: | ||
raise HTTPException( | ||
status_code=401, detail="No credentials were found." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters