feat: init auth cookies

This commit is contained in:
swve 2022-09-22 23:48:51 +02:00
parent 8cc727e309
commit 9479a4b127
5 changed files with 27 additions and 15 deletions

11
app.py
View file

@ -2,6 +2,11 @@ from fastapi import FastAPI
from src.main import global_router
from fastapi.middleware.cors import CORSMiddleware
########################
# Pre-Alpha Version 0.1.0
# Author: @swve
# (c) LearnHouse 2022
########################
# Init
app = FastAPI(
@ -10,18 +15,18 @@ app = FastAPI(
version="0.1.0",
root_path="/"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_origins=["http://localhost:3000"],
allow_methods=["*"],
allow_credentials=True,
allow_headers=["*"],
allow_headers=["*"]
)
app.include_router(global_router)
@app.get("/")
async def root():
return {"Message": "Welcome to LearnHouse ✨"}

View file

@ -1,7 +1,7 @@
import React from "react";
import Layout from "../components/ui/layout";
import { Title } from "../components/ui/styles/title";
import { loginAndGetToken } from "../services/auth";
import { loginAndGetToken } from "../services/auth/auth";
const Login = () => {
const [email, setEmail] = React.useState("");

View file

@ -1,7 +1,7 @@
import React from "react";
import Layout from "../components/ui/layout";
import { Title } from "../components/ui/styles/title";
import { signup } from "../services/auth";
import { signup } from "../services/auth/auth";
const SignUp = () => {
const [email, setEmail] = React.useState("");

View file

@ -1,4 +1,4 @@
import { getAPIUrl } from "./config";
import { getAPIUrl } from "../config";
interface LoginAndGetTokenResponse {
access_token: "string";
@ -17,11 +17,13 @@ export async function loginAndGetToken(username: string, password: string): Prom
headers: HeadersConfig,
body: urlencoded,
redirect: "follow",
credentials: "include",
};
return fetch(`${getAPIUrl()}auth/token`, requestOptions)
.then((result) => result.json())
.catch((error) => console.log("error", error));
// fetch using await and async
const response = await fetch(`${getAPIUrl()}auth/token`, requestOptions);
const data = await response.json();
return data;
}
export async function getUserInfo(token: string): Promise<any> {
@ -30,6 +32,7 @@ export async function getUserInfo(token: string): Promise<any> {
method: "GET",
headers: HeadersConfig,
redirect: "follow",
credentials: "include"
};
return fetch(`${getAPIUrl()}auth/users/me`, requestOptions)

View file

@ -1,9 +1,9 @@
from fastapi import Depends, FastAPI, APIRouter, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel
from fastapi import Depends, APIRouter, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from src.services.auth import *
from src.services.users import *
from datetime import datetime, timedelta
from datetime import timedelta
from fastapi.responses import JSONResponse
router = APIRouter()
@ -24,4 +24,8 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires
)
return {"access_token": access_token, "token_type": "bearer"}
response = JSONResponse(content={"access_token" : access_token ,"token_type": "bearer"})
response.set_cookie(key="user_token", value=access_token, httponly=True, expires="3600",secure=True)
return response