Skip to content

Commit

Permalink
added token in local storage, and genrated cookie when register to au…
Browse files Browse the repository at this point in the history
…to-login
  • Loading branch information
antonisdev committed Oct 10, 2024
1 parent 5711a12 commit 47828bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions backend/src/resolvers/UserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class UserResolver {
@Arg("email") email: string,
@Arg("firstname") firstname: string,
@Arg("lastname") lastname: string,
@Arg("password") password: string
@Arg("password") password: string,
@Ctx() context: any
) {
console.log("process", process.env);

Expand All @@ -89,7 +90,7 @@ class UserResolver {
{ id: userFromDB.id, email: userFromDB.email, role: userFromDB.role },
process.env.JWT_SECRET_KEY
);

context.res.setHeader("Set-Cookie", `token=${token}; Secure; HttpOnly`);
return token;
}

Expand All @@ -102,7 +103,7 @@ class UserResolver {
role: user.role,
firstname: user.firstname,
lastname: user.lastname,
isLoggedIn: true
isLoggedIn: true,
};
} else {
return { isLoggedIn: false };
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/pages/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { useCreateNewUserMutation } from "../generated/graphql-types";
import { Form, Input, Button, Card, message } from "antd";
import { UserOutlined, LockOutlined, MailOutlined } from "@ant-design/icons";
import "../styles/commonLoginRegister.css";
import { useContext } from "react";
import { UserContext } from "../components/Layout";

const Register = () => {
const [createUser] = useCreateNewUserMutation();
const navigate = useNavigate();
const userInfo = useContext(UserContext);

const onFinish = async (values: {
email: string;
Expand All @@ -21,6 +24,8 @@ const Register = () => {
});

if (data && data.createUser) {
localStorage.setItem("token", data.createUser);
userInfo.refetch();
navigate("/");
message.success("Registration successful!");
} else {
Expand Down

0 comments on commit 47828bf

Please sign in to comment.