diff --git a/rails/app/graphql/mutations/sign_in.rb b/rails/app/graphql/mutations/sign_in.rb index 731899f..e05f7f6 100644 --- a/rails/app/graphql/mutations/sign_in.rb +++ b/rails/app/graphql/mutations/sign_in.rb @@ -14,7 +14,13 @@ def resolve(**attributes) if user.valid_password?(attributes[:password]) context[:current_user] = user - MutationResult.call(obj: { object: user }, success: true) + MutationResult.call( + obj: { + object: user + }, + success: true, + errors: user.errors + ) else GraphQL::ExecutionError.new('Incorrect Email/Password') end diff --git a/react/src/components/pages/SignInPage.tsx b/react/src/components/pages/SignInPage.tsx index d22ab62..702bf77 100644 --- a/react/src/components/pages/SignInPage.tsx +++ b/react/src/components/pages/SignInPage.tsx @@ -2,10 +2,12 @@ import { useMutation } from "@apollo/client"; import { SetStateAction, useState } from "react"; import { SIGN_IN } from "../../hooks/SIGN_IN"; +import { useNavigate } from "react-router-dom"; function SignInPage() { const [emailInput, setEmail] = useState(""); const [passwordInput, setPassword] = useState(""); + const navigate = useNavigate(); let emailInputHandler = (e: { target: { value: SetStateAction }; @@ -20,10 +22,19 @@ function SignInPage() { }; const [signIn] = useMutation(SIGN_IN, { + errorPolicy: "ignore", // TO-DO: fix this variables: { email: emailInput, password: passwordInput, }, + onCompleted: (data) => { + localStorage.setItem("token", data.signIn.authenticationToken); + localStorage.setItem("email", "SIGNED IN"); + navigate("/investigations"); + }, + onError: (error) => { + console.log(error); + }, }); if (localStorage.getItem("email")) { diff --git a/react/src/hooks/SIGN_IN.tsx b/react/src/hooks/SIGN_IN.tsx index 4c1ceea..414732e 100644 --- a/react/src/hooks/SIGN_IN.tsx +++ b/react/src/hooks/SIGN_IN.tsx @@ -3,11 +3,8 @@ import { gql } from "@apollo/client"; export const SIGN_IN = gql` mutation ($email: String!, $password: String!) { signIn(input: { email: $email, password: $password }) { - user { - id - email - authenticationToken - } + authenticationToken + id } } `; diff --git a/react/src/root.tsx b/react/src/root.tsx index 9267d58..bda764d 100644 --- a/react/src/root.tsx +++ b/react/src/root.tsx @@ -1,9 +1,15 @@ -import './root.css' +import { Link } from "react-router-dom"; +import "./root.css"; export default function Root() { return ( <>

ey i'm a homepage

+ Register +
+ Sign In +
+ My investigations - ) + ); } diff --git a/react/src/signOutButton.tsx b/react/src/signOutButton.tsx index 02520a8..d0134c6 100644 --- a/react/src/signOutButton.tsx +++ b/react/src/signOutButton.tsx @@ -1,25 +1,14 @@ import { useMutation, gql } from "@apollo/client"; import { useNavigate } from "react-router-dom"; -const SIGN_OUT = gql` - mutation signOut { - signOut(input: {}) { - success - errors - } - } -`; - function SignOutButton() { const navigate = useNavigate(); - const [signOut] = useMutation(SIGN_OUT, { - errorPolicy: "ignore", // TO-DO: fix this - onCompleted: () => { - localStorage.setItem("token", ""); - localStorage.setItem("email", ""); - navigate("/"); - }, - }); + + function signOut() { + localStorage.setItem("token", ""); + localStorage.setItem("email", ""); + navigate("/"); + } return (