mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init auth cookies
This commit is contained in:
parent
8cc727e309
commit
9479a4b127
5 changed files with 27 additions and 15 deletions
11
app.py
11
app.py
|
|
@ -2,6 +2,11 @@ from fastapi import FastAPI
|
||||||
from src.main import global_router
|
from src.main import global_router
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
########################
|
||||||
|
# Pre-Alpha Version 0.1.0
|
||||||
|
# Author: @swve
|
||||||
|
# (c) LearnHouse 2022
|
||||||
|
########################
|
||||||
|
|
||||||
# Init
|
# Init
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
|
|
@ -10,18 +15,18 @@ app = FastAPI(
|
||||||
version="0.1.0",
|
version="0.1.0",
|
||||||
root_path="/"
|
root_path="/"
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"],
|
allow_origins=["http://localhost:3000"],
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_headers=["*"],
|
allow_headers=["*"]
|
||||||
)
|
)
|
||||||
|
|
||||||
app.include_router(global_router)
|
app.include_router(global_router)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
return {"Message": "Welcome to LearnHouse ✨"}
|
return {"Message": "Welcome to LearnHouse ✨"}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import Layout from "../components/ui/layout";
|
import Layout from "../components/ui/layout";
|
||||||
import { Title } from "../components/ui/styles/title";
|
import { Title } from "../components/ui/styles/title";
|
||||||
import { loginAndGetToken } from "../services/auth";
|
import { loginAndGetToken } from "../services/auth/auth";
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
const [email, setEmail] = React.useState("");
|
const [email, setEmail] = React.useState("");
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import Layout from "../components/ui/layout";
|
import Layout from "../components/ui/layout";
|
||||||
import { Title } from "../components/ui/styles/title";
|
import { Title } from "../components/ui/styles/title";
|
||||||
import { signup } from "../services/auth";
|
import { signup } from "../services/auth/auth";
|
||||||
|
|
||||||
const SignUp = () => {
|
const SignUp = () => {
|
||||||
const [email, setEmail] = React.useState("");
|
const [email, setEmail] = React.useState("");
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { getAPIUrl } from "./config";
|
import { getAPIUrl } from "../config";
|
||||||
|
|
||||||
interface LoginAndGetTokenResponse {
|
interface LoginAndGetTokenResponse {
|
||||||
access_token: "string";
|
access_token: "string";
|
||||||
|
|
@ -9,7 +9,7 @@ interface LoginAndGetTokenResponse {
|
||||||
|
|
||||||
export async function loginAndGetToken(username: string, password: string): Promise<LoginAndGetTokenResponse> {
|
export async function loginAndGetToken(username: string, password: string): Promise<LoginAndGetTokenResponse> {
|
||||||
// Request Config
|
// Request Config
|
||||||
const HeadersConfig = new Headers({ "Content-Type": "application/x-www-form-urlencoded", Origin: "http://localhost:3000" });
|
const HeadersConfig = new Headers({ "Content-Type": "application/x-www-form-urlencoded" , Origin: "http://localhost:3000" });
|
||||||
const urlencoded = new URLSearchParams({ username: username, password: password });
|
const urlencoded = new URLSearchParams({ username: username, password: password });
|
||||||
|
|
||||||
const requestOptions: any = {
|
const requestOptions: any = {
|
||||||
|
|
@ -17,11 +17,13 @@ export async function loginAndGetToken(username: string, password: string): Prom
|
||||||
headers: HeadersConfig,
|
headers: HeadersConfig,
|
||||||
body: urlencoded,
|
body: urlencoded,
|
||||||
redirect: "follow",
|
redirect: "follow",
|
||||||
|
credentials: "include",
|
||||||
};
|
};
|
||||||
|
|
||||||
return fetch(`${getAPIUrl()}auth/token`, requestOptions)
|
// fetch using await and async
|
||||||
.then((result) => result.json())
|
const response = await fetch(`${getAPIUrl()}auth/token`, requestOptions);
|
||||||
.catch((error) => console.log("error", error));
|
const data = await response.json();
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUserInfo(token: string): Promise<any> {
|
export async function getUserInfo(token: string): Promise<any> {
|
||||||
|
|
@ -30,6 +32,7 @@ export async function getUserInfo(token: string): Promise<any> {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: HeadersConfig,
|
headers: HeadersConfig,
|
||||||
redirect: "follow",
|
redirect: "follow",
|
||||||
|
credentials: "include"
|
||||||
};
|
};
|
||||||
|
|
||||||
return fetch(`${getAPIUrl()}auth/users/me`, requestOptions)
|
return fetch(`${getAPIUrl()}auth/users/me`, requestOptions)
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
from fastapi import Depends, FastAPI, APIRouter, HTTPException, status
|
from fastapi import Depends, APIRouter, HTTPException, status
|
||||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
from pydantic import BaseModel
|
|
||||||
from src.services.auth import *
|
from src.services.auth import *
|
||||||
from src.services.users import *
|
from src.services.users import *
|
||||||
from datetime import datetime, timedelta
|
from datetime import timedelta
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
@ -24,4 +24,8 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
|
||||||
access_token = create_access_token(
|
access_token = create_access_token(
|
||||||
data={"sub": user.username}, expires_delta=access_token_expires
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue