experimental api changes

This commit is contained in:
2022-10-10 18:47:10 -05:00
parent 4965480aaf
commit b47730758f
4 changed files with 27 additions and 19 deletions

View File

@@ -1,5 +1,4 @@
import { SupabaseClient } from "@supabase/supabase-js"; import { SupabaseClient, User } from "@supabase/supabase-js";
import { updateUser } from "./apiUtils";
export interface FormInput { export interface FormInput {
email: string email: string
@@ -22,7 +21,10 @@ export const handleRegister = async (supabase: SupabaseClient | undefined, input
const { email, password } = input; const { email, password } = input;
if (email && password) { if (email && password) {
const { user, session, error} = await supabase.auth.signUp({ email, password }); const { user, session, error} = await supabase.auth.signUp({ email, password });
if (!user) return;
if (error) throw error; if (error) throw error;
insertNewUser(user);
} }
} }
@@ -30,3 +32,22 @@ export const getSession = async (supabase: SupabaseClient | undefined) => {
if (!supabase) return; if (!supabase) return;
console.log(supabase.auth.session()); 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;
}

View File

@@ -62,19 +62,4 @@ module.exports = class UserModel {
throw new Error(e); 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);
}
}
} }

View File

@@ -30,7 +30,8 @@ module.exports = (app) => {
router.post('/', async (req, res, next) => { router.post('/', async (req, res, next) => {
try { try {
const data = req.body; const data = req.body;
const response = await UserServiceInstance.insert(data);
res.status(200).send(response);
} catch(e) { } catch(e) {
next(e); next(e);
} }

View File

@@ -27,8 +27,9 @@ module.exports = class UserService {
async insert(data) { async insert(data) {
try { try {
const user = await UserInstance.create(data); const user = await UserInstance.create(data);
return user;
} catch(e) { } catch(e) {
next(e); throw new Error(e);
} }
} }
} }