Skip to content

Commit

Permalink
fix(builder,server) Add missing create user calls (Significant-Gravit…
Browse files Browse the repository at this point in the history
…as#7784)

* fix missing create user calls

* formating and linting
  • Loading branch information
aarushik93 authored Aug 9, 2024
1 parent 5b9caa4 commit f0ab795
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
7 changes: 6 additions & 1 deletion rnd/autogpt_builder/src/components/SupabaseProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createClient } from "@/lib/supabase/client";
import { SupabaseClient } from "@supabase/supabase-js";
import { useRouter } from "next/navigation";
import { createContext, useContext, useEffect, useState } from "react";
import AutoGPTServerAPI from "@/lib/autogpt-server-api";

type SupabaseContextType = {
supabase: SupabaseClient | null;
Expand All @@ -25,13 +26,17 @@ export default function SupabaseProvider({
const initializeSupabase = async () => {
setIsLoading(true);
const client = createClient();
const api = new AutoGPTServerAPI();
setSupabase(client);
setIsLoading(false);

if (client) {
const {
data: { subscription },
} = client.auth.onAuthStateChange(() => {
} = client.auth.onAuthStateChange((event, session) => {
if (event === "SIGNED_IN") {
api.createUser();
}
router.refresh();
});

Expand Down
5 changes: 5 additions & 0 deletions rnd/autogpt_builder/src/lib/autogpt-server-api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GraphMeta,
GraphExecuteResponse,
NodeExecutionResult,
User,
} from "./types";

export default class AutoGPTServerAPI {
Expand All @@ -25,6 +26,10 @@ export default class AutoGPTServerAPI {
this.wsUrl = `ws://${new URL(this.baseUrl).host}/ws`;
}

async createUser(): Promise<User> {
return this._request("POST", "/auth/user", {});
}

async getBlocks(): Promise<Block[]> {
return await this._get("/blocks");
}
Expand Down
5 changes: 5 additions & 0 deletions rnd/autogpt_builder/src/lib/autogpt-server-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,8 @@ export type NodeExecutionResult = {
start_time?: Date;
end_time?: Date;
};

export type User = {
id: string;
email: string;
};
17 changes: 14 additions & 3 deletions rnd/autogpt_server/autogpt_server/data/user.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
from typing import Optional

from fastapi import HTTPException
from prisma.models import User

from autogpt_server.data.db import prisma

DEFAULT_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
DEFAULT_EMAIL = "[email protected]"


async def get_or_create_user(user_data: dict) -> User:
user = await prisma.user.find_unique(where={"id": user_data["sub"]})

user_id = user_data.get("sub")
if not user_id:
raise HTTPException(status_code=401, detail="User ID not found in token")

user_email = user_data.get("email")
if not user_email:
raise HTTPException(status_code=401, detail="Email not found in token")

user = await prisma.user.find_unique(where={"id": user_id})
if not user:
user = await prisma.user.create(
data={
"id": user_data["sub"],
"email": user_data["email"],
"id": user_id,
"email": user_email,
"name": user_data.get("user_metadata", {}).get("name"),
}
)
Expand Down

0 comments on commit f0ab795

Please sign in to comment.