Skip to content

Commit

Permalink
Sign in not returning email in response, working otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
alepbloyd committed Aug 26, 2024
1 parent c139080 commit 0247275
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
8 changes: 7 additions & 1 deletion rails/app/graphql/mutations/sign_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions react/src/components/pages/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> };
Expand All @@ -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")) {
Expand Down
7 changes: 2 additions & 5 deletions react/src/hooks/SIGN_IN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
`;
10 changes: 8 additions & 2 deletions react/src/root.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import './root.css'
import { Link } from "react-router-dom";
import "./root.css";

export default function Root() {
return (
<>
<h1>ey i'm a homepage</h1>
<Link to="/register"> Register </Link>
<br />
<Link to="/signin"> Sign In </Link>
<br />
<Link to="/investigations"> My investigations </Link>
</>
)
);
}
23 changes: 6 additions & 17 deletions react/src/signOutButton.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<button
Expand Down

0 comments on commit 0247275

Please sign in to comment.