Languages and Tools:
shinokada / fastapi-web-starter Goto Github PK
View Code? Open in Web Editor NEWA static simple website ready to deploy using FastAPI and Bootstrap 5
A static simple website ready to deploy using FastAPI and Bootstrap 5
Thank you for this! ๐
It really helped me to understand the pieces and how you put them together for a web frontend.
I have a question about adding a login page. I have a FastAPI REST API, some of the routes require authentication before use (JWT in the header).
I've added a new route to serve the page app/routers/login.py
,
import os
from fastapi import Request, APIRouter, Depends
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
#from dotenv import load_dotenv
#load_dotenv()
templates = Jinja2Templates(directory="/proj/app/templates/")
router = APIRouter()
@router.get("/", response_class=HTMLResponse)
async def login_page(request: Request):
res = templates.TemplateResponse("login.html", {"request": request})
return res
and template template/login.html
,
{% extends "base.html" %}
{% set active_page = "upload" %}
{% block title %}H5 File Upload{% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block page_content %}
<div id="top">
<h1>Please login</h1>
<br>
<form action="/api/v1/login/access-token" method="post">
<input type="text" placeholder="Username" name="username" value="{{
request.form.username }}">
<input type="password" placeholder="Password" name="password" value="{{
request.form.password }}">
<input class="btn btn-default" type="submit" value="Login">
</form>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}
{% endif %}
</div>
{% endblock %}
{% block scripts %}
{{ super() }}
{% endblock %}
When the submit button is clicked, what currently happens is that page refreshes and displays the JSON content of the response,
{
"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTA5ODU5MjYsInN1YiI6ImRhbmllbC5mYXJyZWxsQHRlcmF2aWV3LmNvbSJ9.tVJTo4zhuzi5JA8FGoJIJq4ZDcEeOvnKl3JnpxDKLZw",
"token_type":"bearer"
}
What I would like to do is save the response and store the header information somewhere so that subsequent calls to the API always send the authentication token. Any pointers on how to do that?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.