Skip to content

Commit

Permalink
query-params
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 13, 2020
1 parent b14dd1d commit f5c2c4f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
11 changes: 3 additions & 8 deletions web/src/pages/change-password/[token].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { withUrqlClient } from "next-urql";
import { createUrqlClient } from "../../utils/createUrqlClient";
import NextLink from "next/link";

const ChangePassword: NextPage<{ token: string }> = ({ token }) => {
const ChangePassword: NextPage = () => {
const router = useRouter();
const [, changePassword] = useChangePasswordMutation();
const [tokenError, setTokenError] = useState("");
Expand All @@ -22,7 +22,8 @@ const ChangePassword: NextPage<{ token: string }> = ({ token }) => {
onSubmit={async (values, { setErrors }) => {
const response = await changePassword({
newPassword: values.newPassword,
token,
token:
typeof router.query.token === "string" ? router.query.token : "",
});
if (response.data?.changePassword.errors) {
const errorMap = toErrorMap(response.data.changePassword.errors);
Expand Down Expand Up @@ -69,10 +70,4 @@ const ChangePassword: NextPage<{ token: string }> = ({ token }) => {
);
};

ChangePassword.getInitialProps = ({ query }) => {
return {
token: query.token as string,
};
};

export default withUrqlClient(createUrqlClient)(ChangePassword);
8 changes: 6 additions & 2 deletions web/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ const Login: React.FC<{}> = ({}) => {
if (response.data?.login.errors) {
setErrors(toErrorMap(response.data.login.errors));
} else if (response.data?.login.user) {
// worked
router.push("/");
if (typeof router.query.next === "string") {
router.push(router.query.next);
} else {
// worked
router.push("/");
}
}
}}
>
Expand Down
2 changes: 1 addition & 1 deletion web/src/utils/useIsAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useIsAuth = () => {
const router = useRouter();
useEffect(() => {
if (!fetching && !data?.me) {
router.replace("/login");
router.replace("/login?next=" + router.pathname);
}
}, [fetching, data, router]);
};

0 comments on commit f5c2c4f

Please sign in to comment.