Giter VIP home page Giter VIP logo

fastapi-web-starter's Introduction

Typing SVG

Hi there ๐Ÿ‘‹, I'm Shin!

Blog: Medium Twitter: Medium

๐Ÿ† Github Profile Trophy

Shin's GitHub stats

Top Langs

GitHub Streak

Medium Articles

Recent Article 1

Recent Article 3

Repos

FastAPI Web Starter

Gitstart

Awesome Package Manager

Languages and Tools:

bash css3 git html5 Jupyter linux

fastapi-web-starter's People

Contributors

dependabot[bot] avatar shinokada avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastapi-web-starter's Issues

How would you handle login with FastAPI JWT?

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?

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.