fix(builder,server) Add missing create user calls (#7784)

* fix missing create user calls

* formating and linting
pull/7790/head
Aarushi 2024-08-09 16:49:21 +01:00 committed by GitHub
parent 5b9caa4345
commit f0ab795248
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 4 deletions

View File

@ -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;
@ -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();
});

View File

@ -7,6 +7,7 @@ import {
GraphMeta,
GraphExecuteResponse,
NodeExecutionResult,
User,
} from "./types";
export default class AutoGPTServerAPI {
@ -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");
}

View File

@ -167,3 +167,8 @@ export type NodeExecutionResult = {
start_time?: Date;
end_time?: Date;
};
export type User = {
id: string;
email: string;
};

View File

@ -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 = "default@example.com"
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"),
}
)