diff --git a/client/src/util/authHelpers.ts b/client/src/util/authHelpers.ts index e688571..21cbda0 100644 --- a/client/src/util/authHelpers.ts +++ b/client/src/util/authHelpers.ts @@ -1,5 +1,4 @@ -import { SupabaseClient } from "@supabase/supabase-js"; -import { updateUser } from "./apiUtils"; +import { SupabaseClient, User } from "@supabase/supabase-js"; export interface FormInput { email: string @@ -22,7 +21,10 @@ export const handleRegister = async (supabase: SupabaseClient | undefined, input const { email, password } = input; if (email && password) { const { user, session, error} = await supabase.auth.signUp({ email, password }); + if (!user) return; if (error) throw error; + + insertNewUser(user); } } @@ -30,3 +32,22 @@ export const getSession = async (supabase: SupabaseClient | undefined) => { if (!supabase) return; console.log(supabase.auth.session()); } + +export const insertNewUser = async (data: User) => { + if (!data) return; + const { email } = data; + const formattedData = { + email: email, + supabaseUser: data + } + + const response = await fetch("https://mikayla-spice-market-api.com/users/", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(formattedData) + }) + + return response; +} diff --git a/models/UserModel.js b/models/UserModel.js index 3ade8de..003156b 100644 --- a/models/UserModel.js +++ b/models/UserModel.js @@ -62,19 +62,4 @@ module.exports = class UserModel { throw new Error(e); } } - - async createFromSupabase(data) { - try { - const statement = ''; - const result = await db.query(statement); - - if (result.rows.length) { - return result.rows[0]; - } - - return null; - } catch(e) { - throw new Error(e); - } - } } \ No newline at end of file diff --git a/routes/user.js b/routes/user.js index b04b272..9b55199 100644 --- a/routes/user.js +++ b/routes/user.js @@ -30,7 +30,8 @@ module.exports = (app) => { router.post('/', async (req, res, next) => { try { const data = req.body; - + const response = await UserServiceInstance.insert(data); + res.status(200).send(response); } catch(e) { next(e); } diff --git a/services/UserService.js b/services/UserService.js index 194dc70..6bb27a5 100644 --- a/services/UserService.js +++ b/services/UserService.js @@ -27,8 +27,9 @@ module.exports = class UserService { async insert(data) { try { const user = await UserInstance.create(data); + return user; } catch(e) { - next(e); + throw new Error(e); } } } \ No newline at end of file