mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init collections index + delete
This commit is contained in:
parent
c5bb590b2d
commit
924656ce13
14 changed files with 167 additions and 22 deletions
|
|
@ -28,7 +28,7 @@ const AuthProvider = (props: any) => {
|
||||||
userInfo = await getUserInfo(access_token);
|
userInfo = await getUserInfo(access_token);
|
||||||
isAuthenticated = true;
|
isAuthenticated = true;
|
||||||
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||||
} else if (!access_token) {
|
} else{
|
||||||
isAuthenticated = false;
|
isAuthenticated = false;
|
||||||
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,19 +8,8 @@ export const Header = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Menu></Menu>
|
<Menu></Menu>
|
||||||
<PreAlphaLabel>🚧 Pre-Alpha</PreAlphaLabel>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const PreAlphaLabel = styled.div`
|
|
||||||
position: fixed;
|
|
||||||
bottom: 0;
|
|
||||||
right: 0;
|
|
||||||
padding: 9px;
|
|
||||||
background-color: #080501;
|
|
||||||
color: white;
|
|
||||||
font-size: 19px;
|
|
||||||
font-weight: bold;
|
|
||||||
border-radius: 5px 0 0 0px;
|
|
||||||
`;
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ const Layout = (props: any) => {
|
||||||
<meta name="description" content={props.description} />
|
<meta name="description" content={props.description} />
|
||||||
<link rel="icon" href="/favicon.ico" />
|
<link rel="icon" href="/favicon.ico" />
|
||||||
</Head>
|
</Head>
|
||||||
<Header></Header>
|
<PreAlphaLabel>🚧 Pre-Alpha</PreAlphaLabel>
|
||||||
<Main className="min-h-screen">{props.children}</Main>
|
<Main className="min-h-screen">{props.children}</Main>
|
||||||
|
|
||||||
<Footer>
|
<Footer>
|
||||||
|
|
@ -43,4 +43,16 @@ const Footer = styled.footer`
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const PreAlphaLabel = styled.div`
|
||||||
|
position: fixed;
|
||||||
|
bottom: 0;
|
||||||
|
right: 0;
|
||||||
|
padding: 9px;
|
||||||
|
background-color: #080501;
|
||||||
|
color: white;
|
||||||
|
font-size: 19px;
|
||||||
|
font-weight: bold;
|
||||||
|
border-radius: 5px 0 0 0px;
|
||||||
|
`;
|
||||||
|
|
||||||
export default Layout;
|
export default Layout;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
import type { NextPage } from "next";
|
import type { NextPage } from "next";
|
||||||
import { Title } from "../components/ui/styles/title";
|
import { Title } from "../components/ui/styles/title";
|
||||||
import Layout from "../components/ui/layout";
|
import Layout from "../components/ui/layout";
|
||||||
|
import { Header } from "../components/ui/header";
|
||||||
|
|
||||||
const Home: NextPage = () => {
|
const Home: NextPage = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Layout title="Index">
|
<Layout title="Index">
|
||||||
|
<Header></Header>
|
||||||
<Title>Home</Title>
|
<Title>Home</Title>
|
||||||
</Layout>
|
</Layout>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { Header } from "../components/ui/header";
|
||||||
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/auth";
|
import { loginAndGetToken } from "../services/auth/auth";
|
||||||
|
|
@ -25,6 +26,7 @@ const Login = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Layout title="Login">
|
<Layout title="Login">
|
||||||
|
<Header></Header>
|
||||||
<Title>Login</Title>
|
<Title>Login</Title>
|
||||||
|
|
||||||
<form>
|
<form>
|
||||||
|
|
|
||||||
67
front/pages/organizations/index.tsx
Normal file
67
front/pages/organizations/index.tsx
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
import Link from "next/link";
|
||||||
|
import React from "react";
|
||||||
|
import Layout from "../../components/ui/layout";
|
||||||
|
import { Title } from "../../components/ui/styles/title";
|
||||||
|
import { deleteOrganizationFromBackend, getUserOrganizations } from "../../services/orgs";
|
||||||
|
|
||||||
|
const Organizations = () => {
|
||||||
|
const [userOrganizations, setUserOrganizations] = React.useState([]);
|
||||||
|
const [isLoading, setIsLoading] = React.useState(false);
|
||||||
|
|
||||||
|
async function fetchUserOrganizations() {
|
||||||
|
const response = await getUserOrganizations();
|
||||||
|
setUserOrganizations(response);
|
||||||
|
console.log(response);
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteOrganization(org_id:any) {
|
||||||
|
const response = await deleteOrganizationFromBackend(org_id);
|
||||||
|
const newOrganizations = userOrganizations.filter((org:any) => org.org_id !== org_id);
|
||||||
|
setUserOrganizations(newOrganizations);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
setIsLoading(true);
|
||||||
|
fetchUserOrganizations();
|
||||||
|
setIsLoading(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
|
||||||
|
<Layout>
|
||||||
|
<Title>
|
||||||
|
Your Organizations{" "}
|
||||||
|
<Link href={"/organizations/new"}>
|
||||||
|
<a>
|
||||||
|
<button>+</button>
|
||||||
|
</a>
|
||||||
|
</Link>
|
||||||
|
</Title>
|
||||||
|
<hr />
|
||||||
|
{isLoading ? (
|
||||||
|
<p>Loading...</p>
|
||||||
|
) : (
|
||||||
|
<div>
|
||||||
|
{userOrganizations.map((org: any) => (
|
||||||
|
<div key={org.org_id}>
|
||||||
|
<Link href={`/organizations/${org.org_id}`}>
|
||||||
|
<a>
|
||||||
|
<h3>{org.name}</h3>
|
||||||
|
</a>
|
||||||
|
</Link>
|
||||||
|
<button onClick={() => deleteOrganization(org.org_id)}>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Organizations;
|
||||||
13
front/pages/organizations/new.tsx
Normal file
13
front/pages/organizations/new.tsx
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import React from 'react'
|
||||||
|
import Layout from '../../components/ui/layout'
|
||||||
|
import { Title } from '../../components/ui/styles/title'
|
||||||
|
|
||||||
|
const Organizations = () => {
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<Title>New Organization</Title>
|
||||||
|
</Layout>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Organizations
|
||||||
0
front/pages/organizations/update.tsx
Normal file
0
front/pages/organizations/update.tsx
Normal file
|
|
@ -1,4 +1,5 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { Header } from "../components/ui/header";
|
||||||
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/auth";
|
import { signup } from "../services/auth/auth";
|
||||||
|
|
@ -30,6 +31,7 @@ const SignUp = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Layout title="Sign up">
|
<Layout title="Sign up">
|
||||||
|
<Header></Header>
|
||||||
<Title>Sign up </Title>
|
<Title>Sign up </Title>
|
||||||
|
|
||||||
<form>
|
<form>
|
||||||
|
|
|
||||||
37
front/services/orgs.ts
Normal file
37
front/services/orgs.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import { getAPIUrl } from "./config";
|
||||||
|
|
||||||
|
export async function getUserOrganizations() {
|
||||||
|
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||||
|
|
||||||
|
const requestOptions: any = {
|
||||||
|
method: "GET",
|
||||||
|
headers: HeadersConfig,
|
||||||
|
redirect: "follow",
|
||||||
|
credentials: "include",
|
||||||
|
};
|
||||||
|
|
||||||
|
return fetch(`${getAPIUrl()}orgs/user/page/1/limit/10`, requestOptions)
|
||||||
|
.then((result) => result.json())
|
||||||
|
.catch((error) => console.log("error", error));
|
||||||
|
}
|
||||||
|
|
||||||
|
// export async function createNewOrganization(user_id) {}
|
||||||
|
|
||||||
|
// export async function getOrganizationData(org_id) {}
|
||||||
|
|
||||||
|
export async function deleteOrganizationFromBackend(org_id:any) {
|
||||||
|
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||||
|
|
||||||
|
const requestOptions: any = {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: HeadersConfig,
|
||||||
|
redirect: "follow",
|
||||||
|
credentials: "include",
|
||||||
|
};
|
||||||
|
|
||||||
|
return fetch(`${getAPIUrl()}orgs/${org_id}`, requestOptions)
|
||||||
|
.then((result) => result.json())
|
||||||
|
.catch((error) => console.log("error", error));
|
||||||
|
}
|
||||||
|
|
||||||
|
// export async function updateOrganization(org_id) {}
|
||||||
|
|
@ -59,6 +59,7 @@ async def login(Authorize: AuthJWT = Depends(), form_data: OAuth2PasswordRequest
|
||||||
access_token = Authorize.create_access_token(subject=form_data.username)
|
access_token = Authorize.create_access_token(subject=form_data.username)
|
||||||
refresh_token = Authorize.create_refresh_token(subject=form_data.username)
|
refresh_token = Authorize.create_refresh_token(subject=form_data.username)
|
||||||
Authorize.set_refresh_cookies(refresh_token)
|
Authorize.set_refresh_cookies(refresh_token)
|
||||||
|
Authorize.set_access_cookies(access_token)
|
||||||
return {"access_token": access_token , "refresh_token": refresh_token}
|
return {"access_token": access_token , "refresh_token": refresh_token}
|
||||||
|
|
||||||
@router.delete('/logout')
|
@router.delete('/logout')
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from src.services.auth import get_current_user
|
from src.services.auth import get_current_user
|
||||||
from src.services.orgs import Organization, create_org, delete_org, get_organization, get_orgs, update_org
|
from src.services.orgs import Organization, create_org, delete_org, get_organization, get_orgs, get_orgs_by_user, update_org
|
||||||
from src.services.users import User
|
from src.services.users import User
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -24,11 +24,18 @@ async def api_get_org(org_id: str, current_user: User = Depends(get_current_user
|
||||||
|
|
||||||
|
|
||||||
@router.get("/page/{page}/limit/{limit}")
|
@router.get("/page/{page}/limit/{limit}")
|
||||||
async def api_get_org_by(page: int, limit: int, current_user: User = Depends(get_current_user)):
|
async def api_get_org_by(page: int, limit: int):
|
||||||
"""
|
"""
|
||||||
Get orgs by page and limit
|
Get orgs by page and limit
|
||||||
"""
|
"""
|
||||||
return await get_orgs(page, limit, current_user)
|
return await get_orgs(page, limit)
|
||||||
|
|
||||||
|
@router.get("/user/page/{page}/limit/{limit}")
|
||||||
|
async def api_user_orgs(page: int, limit: int, current_user: User = Depends(get_current_user)):
|
||||||
|
"""
|
||||||
|
Get orgs by page and limit by user
|
||||||
|
"""
|
||||||
|
return await get_orgs_by_user(current_user.user_id, page, limit)
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{org_id}")
|
@router.put("/{org_id}")
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,10 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/login")
|
||||||
#### JWT Auth ####################################################
|
#### JWT Auth ####################################################
|
||||||
class Settings(BaseModel):
|
class Settings(BaseModel):
|
||||||
authjwt_secret_key: str = "secret"
|
authjwt_secret_key: str = "secret"
|
||||||
authjwt_token_location = {"cookies", "headers"}
|
authjwt_token_location = {"cookies"}
|
||||||
authjwt_cookie_csrf_protect = False
|
authjwt_cookie_csrf_protect = False
|
||||||
|
authjwt_access_token_expires = False # (pre-alpha only) # TODO: set to 1 hour
|
||||||
|
|
||||||
|
|
||||||
@AuthJWT.load_config
|
@AuthJWT.load_config
|
||||||
def get_config():
|
def get_config():
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ async def update_org(org_object: Organization, org_id: str, current_user: User):
|
||||||
async def delete_org(org_id: str, current_user: User):
|
async def delete_org(org_id: str, current_user: User):
|
||||||
await check_database()
|
await check_database()
|
||||||
|
|
||||||
await verify_org_rights(org_id, current_user)
|
await verify_org_rights(org_id, current_user,"delete")
|
||||||
|
|
||||||
orgs = learnhouseDB["organizations"]
|
orgs = learnhouseDB["organizations"]
|
||||||
|
|
||||||
|
|
@ -116,14 +116,25 @@ async def delete_org(org_id: str, current_user: User):
|
||||||
|
|
||||||
|
|
||||||
async def get_orgs(page: int = 1, limit: int = 10):
|
async def get_orgs(page: int = 1, limit: int = 10):
|
||||||
## TODO : auth
|
## TODO : Deprecated
|
||||||
await check_database()
|
await check_database()
|
||||||
orgs = learnhouseDB["orgs"]
|
orgs = learnhouseDB["organizations"]
|
||||||
|
|
||||||
# get all orgs from database
|
# get all orgs from database
|
||||||
all_orgs = orgs.find().sort("name", 1).skip(10 * (page - 1)).limit(limit)
|
all_orgs = orgs.find().sort("name", 1).skip(10 * (page - 1)).limit(limit)
|
||||||
|
|
||||||
return [json.loads(json.dumps(house, default=str)) for house in all_orgs]
|
return [json.loads(json.dumps(org, default=str)) for org in all_orgs]
|
||||||
|
|
||||||
|
|
||||||
|
async def get_orgs_by_user(user_id: str, page: int = 1, limit: int = 10):
|
||||||
|
await check_database()
|
||||||
|
orgs = learnhouseDB["organizations"]
|
||||||
|
print(user_id)
|
||||||
|
# find all orgs where user_id is in owners or admins arrays
|
||||||
|
all_orgs = orgs.find({"$or": [{"owners": user_id}, {"admins": user_id}]}).sort("name", 1).skip(10 * (page - 1)).limit(limit)
|
||||||
|
|
||||||
|
return [json.loads(json.dumps(org, default=str)) for org in all_orgs]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### Security ####################################################
|
#### Security ####################################################
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue