Skip to content

Commit

Permalink
signup function finished
Browse files Browse the repository at this point in the history
  • Loading branch information
hereAlexT committed Nov 27, 2023
1 parent 4194574 commit dfe262b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion frontend/src/apis/AuthenticationAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
19 changes: 16 additions & 3 deletions frontend/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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 } })
}


Expand Down
43 changes: 34 additions & 9 deletions frontend/src/pages/Signup.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => {

Expand Down Expand Up @@ -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<HTMLFormElement>) => {
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 (
Expand Down Expand Up @@ -101,6 +112,20 @@ const Signup: React.FC = () => {
/>
</IonRow>

<IonRow>
<IonInput
type="password"
label="Comfirm Password"
helperText="Type your password"
labelPlacement="floating"
counter={true}
maxlength={32}
minlength={8}
onIonChange={(e) => setComfirmPassword(e.detail.value!)}
disabled={false}
/>
</IonRow>

<IonRow>
<IonCol className="ion-padding-top">
<IonButton type="submit" expand="block">Signup</IonButton>
Expand All @@ -118,7 +143,7 @@ const Signup: React.FC = () => {


</IonContent>
</IonPage>
</IonPage >
);
};

Expand Down

0 comments on commit dfe262b

Please sign in to comment.