Skip to content

Commit

Permalink
Merge pull request #86 from JSv4/JSv4/fix-django-auth
Browse files Browse the repository at this point in the history
Fix Django Auth
  • Loading branch information
JSv4 committed Mar 2, 2023
2 parents 750a7f4 + 3d7ffb5 commit a8af7c2
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 22 deletions.
14 changes: 7 additions & 7 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,13 @@

# CORS
# ------------------------------------------------------------------------------
# CORS_ORIGIN_WHITELIST = [
# "http://localhost:3000",
# "https://localhost:3000",
# "http://localhost:8000",
# "http://127.0.0.1:3000",
# "https://127.0.0.1:3000",
# ]
CORS_ORIGIN_WHITELIST = [
"http://localhost:3000",
"https://localhost:3000",
"http://localhost:8000",
"http://127.0.0.1:3000",
"https://127.0.0.1:3000",
]
CORS_ALLOW_METHODS = [
"DELETE",
"GET",
Expand Down
2 changes: 1 addition & 1 deletion frontend/.local.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ REACT_APP_APPLICATION_DOMAIN=dev-7ranai11.auth0.com
REACT_APP_APPLICATION_CLIENT_ID=318GitavTaWR7d17h4DKuoCme9VgjYDG
REACT_APP_AUDIENCE=http://localhost:3000
REACT_APP_API_ROOT_URL=http://localhost:8000
REACT_APP_USE_AUTH0=false
REACT_APP_USE_AUTH0=true
REACT_APP_USE_ANALYZERS=true
REACT_APP_ALLOW_IMPORTS=true
2 changes: 1 addition & 1 deletion frontend/src/.local.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ REACT_APP_APPLICATION_DOMAIN=dev-7ranai11.auth0.com
REACT_APP_APPLICATION_CLIENT_ID=318GitavTaWR7d17h4DKuoCme9VgjYDG
REACT_APP_AUDIENCE=http://localhost:3000
REACT_APP_API_ROOT_URL=http://localhost:8000
REACT_APP_USE_AUTH0=true
REACT_APP_USE_AUTH0=false
REACT_APP_USE_ANALYZERS=true
REACT_APP_ALLOW_IMPORTS=true
6 changes: 3 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const App = () => {
// Only use this if we're using Auth0 Authentication... otherwise we don't
// need to access the Auth0 SDK.
useEffect(() => {
if (REACT_APP_USE_AUTH0) {
if (REACT_APP_USE_AUTH0 === "true") {
if (user) {
try {
getAccessTokenSilently({
Expand All @@ -87,7 +87,7 @@ export const App = () => {
}
}
}
}, [getAccessTokenSilently, user?.sub]);
}, [getAccessTokenSilently, REACT_APP_USE_AUTH0, user?.sub]);

console.log("Cookie Accepted: ", show_cookie_modal);

Expand Down Expand Up @@ -140,7 +140,7 @@ export const App = () => {
</Dimmer>
<Routes>
<Route path="/" element={<Corpuses />} />
{REACT_APP_USE_AUTH0 ? (
{REACT_APP_USE_AUTH0 !== "true" ? (
<Route path="/login" element={<Login />} />
) : (
<></>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/layout/MobileNavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export const MobileNavMenu = () => {
const { pathname } = useLocation();
const navigate = useNavigate();

const user = REACT_APP_USE_AUTH0 ? auth0_user : cache_user;
const user = REACT_APP_USE_AUTH0 === "true" ? auth0_user : cache_user;

const show_export_modal = useReactiveVar(showExportModal);

let public_header_items = header_menu_items.filter((item) => !item.protected);
let private_header_items = header_menu_items.filter((item) => item.protected);

const requestLogout = (args: any) => {
if (REACT_APP_USE_AUTH0) {
if (REACT_APP_USE_AUTH0 === "true") {
logout(args);
} else {
authToken("");
Expand Down Expand Up @@ -58,7 +58,7 @@ export const MobileNavMenu = () => {
</Dropdown.Item>
));

if (REACT_APP_USE_AUTH0) {
if (REACT_APP_USE_AUTH0 === "true") {
return (
<Menu fluid inverted attached style={{ marginBottom: "0px" }}>
<Menu.Menu position="left">
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/components/layout/NavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export const NavMenu = () => {
const { pathname } = useLocation();
const navigate = useNavigate();

const user = REACT_APP_USE_AUTH0 ? auth0_user : cache_user;
const user = REACT_APP_USE_AUTH0 === "true" ? auth0_user : cache_user;

const show_export_modal = useReactiveVar(showExportModal);

let public_header_items = header_menu_items.filter((item) => !item.protected);
let private_header_items = header_menu_items.filter((item) => item.protected);

const requestLogout = (args: any) => {
if (REACT_APP_USE_AUTH0) {
if (REACT_APP_USE_AUTH0 === "true") {
logout(args);
} else {
authToken("");
Expand Down Expand Up @@ -55,7 +55,8 @@ export const NavMenu = () => {
</Menu.Item>
));

if (REACT_APP_USE_AUTH0) {
if (REACT_APP_USE_AUTH0 === "true") {

return (
<Menu fluid inverted attached style={{ marginBottom: "0px" }}>
<Menu.Item header>
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export function notEmpty<TValue>(
/**
* Types
*/

export enum ExportTypes {
LANGCHAIN = "LangChain Format",
OPEN_CONTRACTS = "Open Contracts Format"
}

export enum PermissionTypes {
CAN_PERMISSION = "CAN_PERMISSION",
CAN_PUBLISH = "CAN_PUBLISH",
Expand Down
52 changes: 52 additions & 0 deletions frontend/src/components/widgets/modals/SelectExportTypeModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react'
import { Button, Dropdown, Modal } from 'semantic-ui-react'
import { ExportTypes } from '../../types';


export function SelectExportTypeModal({
visible,
toggleModal,
startExport
}: {
visible: boolean;
toggleModal: (args?: any) => void | any;
startExport: (id: string, type: ExportTypes) => void | any;
}) {

return (
<Modal
size="small"
open={visible}
onClose={() => {}}
>
<Modal.Header>Delete Your Account</Modal.Header>
<Modal.Content>
<div style={{
"display": "flex",
"flexDirection": "row",
"justifyContent": "center",
"height": "100%"
}}>
<div>
{/* <Dropdown
placeholder="Select label"
search
selection
options={[]}
onChange={()=>{}}
value={}
/> */}
</div>
</div>
</Modal.Content>
<Modal.Actions>
<Button negative onClick={() => toggleModal()}>
Cancel
</Button>
<Button positive onClick={() => {}}>
Start
</Button>
</Modal.Actions>
</Modal>
)
}
3 changes: 2 additions & 1 deletion frontend/src/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SemanticICONS } from "semantic-ui-react";
import { TokenId } from "../components/annotator/context";
import { MultipageAnnotationJson } from "../components/types";
import { ExportTypes, MultipageAnnotationJson } from "../components/types";

export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
Expand Down Expand Up @@ -739,6 +739,7 @@ export type UserExportType = Node & {
created: Scalars["DateTime"];
started?: Maybe<Scalars["DateTime"]>;
finished?: Maybe<Scalars["DateTime"]>;
format?: ExportTypes;
errors: Scalars["String"];
creator: UserType;
isPublic?: Scalars["Boolean"];
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const onRedirectCallback = (appState: any) => {
);
};

const { REACT_APP_USE_AUTH0: USE_AUTH0, REACT_APP_API_ROOT_URL: api_root_url } =
const { REACT_APP_USE_AUTH0, REACT_APP_API_ROOT_URL: api_root_url } =
process.env;

console.log("OpenContracts is using Auth0: ", USE_AUTH0);
console.log("OpenContracts is using Auth0: ", REACT_APP_USE_AUTH0);
console.log("OpenContracts frontend target api root", api_root_url);

const authLink = new ApolloLink((operation, forward) => {
Expand All @@ -49,7 +49,10 @@ const client = new ApolloClient({
cache,
});

if (USE_AUTH0) {
if (REACT_APP_USE_AUTH0 === "true") {

console.log("Rendering with USE_AUTH0");

const providerConfig = {
domain: "dev-7ranai11.auth0.com",
clientId: "318GitavTaWR7d17h4DKuoCme9VgjYDG",
Expand All @@ -70,6 +73,9 @@ if (USE_AUTH0) {
document.getElementById("root")
);
} else {

console.log("Rendering with NO AUTH0");

ReactDOM.render(
<ApolloProvider client={client}>
<BrowserRouter>
Expand Down
11 changes: 11 additions & 0 deletions opencontractserver/types/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
from typing_extensions import TypedDict


class OpenContractsEnum(str, enum.Enum):
@classmethod
def choices(cls):
return [(key.value, key.name) for key in cls]


class ExportType(OpenContractsEnum):
LANGCHAIN = "LangChain Format"
OPEN_CONTRACTS = "Open Contracts Format"


class LabelType(str, enum.Enum):
DOC_TYPE_LABEL = "DOC_TYPE_LABEL"
TOKEN_LABEL = "TOKEN_LABEL"
Expand Down
19 changes: 19 additions & 0 deletions opencontractserver/users/migrations/0004_userexport_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.9 on 2023-03-02 04:48

from django.db import migrations, models
import opencontractserver.types.enums


class Migration(migrations.Migration):

dependencies = [
('users', '0003_create_initial_superuser'),
]

operations = [
migrations.AddField(
model_name='userexport',
name='format',
field=models.CharField(choices=[('LangChain Format', 'LANGCHAIN'), ('Open Contracts Format', 'OPEN_CONTRACTS')], default=opencontractserver.types.enums.ExportType['OPEN_CONTRACTS'], max_length=128),
),
]
9 changes: 9 additions & 0 deletions opencontractserver/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from guardian.models import GroupObjectPermissionBase, UserObjectPermissionBase

from opencontractserver.shared.utils import calc_oc_file_path
from opencontractserver.types.enums import ExportType
from opencontractserver.users.validators import UserUnicodeUsernameValidator


Expand Down Expand Up @@ -177,6 +178,14 @@ class UserExport(django.db.models.Model):
finished = django.db.models.DateTimeField(null=True)
errors = django.db.models.TextField(blank=True)

format = django.db.models.CharField(
max_length=128,
blank=False,
null=False,
choices=ExportType.choices(),
default=ExportType.OPEN_CONTRACTS,
)

# Backend stuff
backend_lock = django.db.models.BooleanField(
default=False
Expand Down

0 comments on commit a8af7c2

Please sign in to comment.