From dfe262b689e1accbfc7f564921ad0d64e4d7ef6b Mon Sep 17 00:00:00 2001 From: Alex T <79433869+hereAlexT@users.noreply.github.com> Date: Tue, 28 Nov 2023 00:49:09 +1100 Subject: [PATCH] signup function finished --- frontend/src/apis/AuthenticationAPI.ts | 2 +- frontend/src/contexts/AuthContext.tsx | 19 ++++++++++-- frontend/src/pages/Signup.tsx | 43 ++++++++++++++++++++------ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/frontend/src/apis/AuthenticationAPI.ts b/frontend/src/apis/AuthenticationAPI.ts index b774a3f..d2c2edb 100644 --- a/frontend/src/apis/AuthenticationAPI.ts +++ b/frontend/src/apis/AuthenticationAPI.ts @@ -9,7 +9,7 @@ import type { const Signup = async (email: string, password: string) => { try { - const { data: { user, session }, error } = await supabase.auth.signUp({ + const { data: { user, session } } = await supabase.auth.signUp({ email: email, password: password, }); diff --git a/frontend/src/contexts/AuthContext.tsx b/frontend/src/contexts/AuthContext.tsx index 02f4dd9..bbf6228 100644 --- a/frontend/src/contexts/AuthContext.tsx +++ b/frontend/src/contexts/AuthContext.tsx @@ -28,7 +28,7 @@ function reducer(state: State, action: Action) { return { ...state, user: null, session: null, isAuthenticated: false }; case "signup": console.log("reducer : signup") - return { ...state, user: action.payload, isAuthenticated: true, session:null }; + return { ...state, user: action.payload, isAuthenticated: true, session: null }; default: throw new Error("Unknown action type"); } @@ -86,10 +86,23 @@ function AuthProvider({ children }: AuthProviderProps) { dispatch({ type: "logout" }); } - function signup(email: string, password: string) { + const signup = async (email: string, password: string) => { console.log("AuthenContext - signup") + try { + console.log("hehh") + const { user, session } = await ApiSignup(email, password) + .catch(error => { + console.error('Error during signup:', error); + throw error; + });; + console.log(user) + console.log(session) + dispatch({ type: "signup", payload: { email, password } }) + } catch (error) { + console.error(error) + throw error; + } - dispatch({ type: "signup", payload: { email, password } }) } diff --git a/frontend/src/pages/Signup.tsx b/frontend/src/pages/Signup.tsx index 6fdbebd..ee8b506 100644 --- a/frontend/src/pages/Signup.tsx +++ b/frontend/src/pages/Signup.tsx @@ -1,7 +1,7 @@ import { IonContent, IonHeader, IonInput, IonPage, IonText, IonTitle, IonToolbar, IonButton, IonGrid, IonCol, IonRow, IonButtons, IonMenuButton } from '@ionic/react'; -import { useState } from 'react'; -import { Signup as ApiSignup } from '../apis/AuthenticationAPI'; +import { useState, useEffect } from 'react'; import { useHistory } from "react-router-dom"; +import { useAuth } from '../contexts/AuthContext'; const Signup: React.FC = () => { @@ -35,22 +35,33 @@ const Signup: React.FC = () => { const history = useHistory(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); + const [comfirmPassword, setComfirmPassword] = useState(""); + + const { signup, isAuthenticated } = useAuth(); + + useEffect(() => { + if (isAuthenticated) { + history.push("/timeline"); + } + }, [isAuthenticated]); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); + if (password !== comfirmPassword) { + alert("Password and comfirm password are not matched"); + return; + } + + try { - const { user, session } = await ApiSignup(email, password); - if (user?.identities?.length === 0) { - alert("This user already exists"); - history.push("/login") - } + await signup(email, password); } catch (error) { alert(error) console.log(error); } - }; + return ( @@ -101,6 +112,20 @@ const Signup: React.FC = () => { /> + + setComfirmPassword(e.detail.value!)} + disabled={false} + /> + + Signup @@ -118,7 +143,7 @@ const Signup: React.FC = () => { - + ); };