Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Refactor/sdk user auth #727

Merged
merged 32 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
04db004
Create user class in sdk
orzechdev May 14, 2020
12eb205
Add signing method to user sdk
orzechdev May 14, 2020
9ad3a54
Implement user sign in and sign out in sdk
orzechdev May 15, 2020
d8a729b
Remove redundant signIn validation
orzechdev May 15, 2020
1593f01
Update gql types
orzechdev May 18, 2020
918bdfc
Use useAuth from sdk in components
orzechdev May 18, 2020
43bccea
Add auto sign in to useAuth
orzechdev May 19, 2020
0da3fba
Show sign in or out notification with new useAuth
orzechdev May 19, 2020
aa0440c
Refactor sdk state to support auth in sdk
orzechdev May 19, 2020
a353158
Update sdk state updating process
orzechdev May 20, 2020
9823e15
Update sdk state initialization
orzechdev May 20, 2020
0e86573
Update config usage in sdk
orzechdev May 21, 2020
a116963
Remove temporary ApolloClientInvalidTokenLinkAdapter
orzechdev May 22, 2020
ea907ea
Update app index
orzechdev May 28, 2020
db5abb2
Update changelog
orzechdev May 28, 2020
66f8eb4
Remove sdk auth api from storefront repo
orzechdev May 29, 2020
9bb2faa
Update sdk package dependency
orzechdev May 29, 2020
1b66fee
Update dependencies
orzechdev Jun 1, 2020
25b14b8
Fix cypress login tests
orzechdev Jun 1, 2020
339269b
Update sdk dependency
orzechdev Jun 1, 2020
cd23a93
Fix cypress tests
orzechdev Jun 1, 2020
9de7dcd
Fix cypress tests
orzechdev Jun 1, 2020
1a6cbd2
Update saleor provider
orzechdev Jun 3, 2020
8679687
Remove apollo provider
orzechdev Jun 3, 2020
cda4858
Update sdk dependency
orzechdev Jun 5, 2020
1bf10ae
Update sdk dependency
orzechdev Jul 7, 2020
35d15eb
Fix duplicated imports
orzechdev Jul 7, 2020
2f2a64c
Update SDK dependency
orzechdev Jul 9, 2020
fc8b656
Update failing tests
orzechdev Jul 10, 2020
de6292d
Update sdk version
orzechdev Jul 14, 2020
16055fd
Remove maybe from error handling
orzechdev Jul 14, 2020
facb851
Update tests
orzechdev Jul 14, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add Cypress tags to all of the buttons (also speed up tests) - #718 by @krzysztofwolski
- Automatically choose variant attributes from url in ProductVariantPicker - #708 by @AlicjaSzu
- Use sdk as a standalone package - #724 by @dominik-zeglen
- Use auth api from sdk - #727 by @orzechdev
- Fix CartRow tests - #749 by @dominik-zeglen
- Add prettier to precommit - #766 by @dominik-zeglen
- Do not use prettier on generated files - #773 by @dominik-zeglen
Expand Down
2 changes: 1 addition & 1 deletion cypress/elements/saleor-account/login-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export const LOGIN_SELECTORS = {
emailAddressInput: "[data-test=loginOverlay] input[name='email']",
emailPasswordInput: "[data-test=loginOverlay] input[name='password']",
signInButton: "[data-test=submit]",
warningCredentialMessage: ".form-error",
warningCredentialMessage: ".input__error",
};
22 changes: 11 additions & 11 deletions cypress/support/login/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { userBuilder } from "../generate";
const mainMenuButton = "[data-test=desktopMenuLoginOverlayLink]"
const allertPopupMessage = "[data-test=alert]"
const emailAddressInput = "[data-test=loginOverlay] input[name='email']"
const emailPasswordInput = "[data-test=loginOverlay] input[name='password']"
const signInButton = "[data-test=submit]"
const loggedInMainMenuButton = "[data-test=userButton]"
const logOutButton = "[data-test=desktopMenuLogoutLink]"
const mainMenuButton = "[data-test=desktopMenuLoginOverlayLink]";
const allertPopupMessage = "[data-test=alert]";
const emailAddressInput = "[data-test=loginOverlay] input[name='email']";
const emailPasswordInput = "[data-test=loginOverlay] input[name='password']";
const signInButton = "[data-test=submit]";
const loggedInMainMenuButton = "[data-test=userButton]";
const logOutButton = "[data-test=desktopMenuLogoutLink]";

const createUser = () => {
const user = userBuilder();
Expand All @@ -29,8 +29,8 @@ Cypress.Commands.add("loginUser", (email, password) => {
.type(password)
.get(signInButton)
.click()
.get(allertPopupMessage)
.should("contain", "You are now logged in", {timeoout: 20000});
.get(allertPopupMessage, { timeout: 3000 })
.should("contain", "You are now logged in", { timeoout: 20000 });
});

Cypress.Commands.add("logoutUser", () =>
Expand All @@ -39,6 +39,6 @@ Cypress.Commands.add("logoutUser", () =>
.click()
.get(logOutButton)
.click()
.get(allertPopupMessage)
.should("contain", "You are now logged out", {timeoout: 20000})
.get(allertPopupMessage, { timeout: 3000 })
.should("contain", "You are now logged out", { timeoout: 20000 })
);
21 changes: 14 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"@babel/runtime": "^7.5.5",
"@lhci/cli": "^0.4.1",
"@saleor/sdk": "github:mirumee/saleor-sdk#737c634",
"@saleor/sdk": "github:mirumee/saleor-sdk#a3c0a1f",
"@sentry/apm": "^5.15.5",
"@sentry/browser": "^5.15.5",
"@stripe/react-stripe-js": "^1.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { FormattedMessage, useIntl } from "react-intl";

import { commonMessages } from "@temp/intl";
import { useAccountUpdate, useUserDetails } from "@saleor/sdk";
import { useAccountUpdate, useAuth } from "@saleor/sdk";

import { Attribute, IconButton, Tile } from "@components/atoms";

Expand All @@ -12,8 +12,8 @@ import * as S from "./styles";
export const AccountTile: React.FC = () => {
const [isEditing, setIsEditing] = React.useState(false);
const [setAccountUpdate, { data, error }] = useAccountUpdate();
const { data: user } = useUserDetails();
const intl = useIntl();
const { user } = useAuth();

React.useEffect(() => {
if (data && !error) {
Expand Down
2 changes: 1 addition & 1 deletion src/@next/components/molecules/AccountTabTiles/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { PasswordTile } from "./PasswordTile";

jest.mock("@saleor/sdk", () => ({
useAccountUpdate: () => [jest.fn(), { data: null, error: null }],
useAuth: () => ({ user: { firstName: "John", lastName: "Doe" } }),
usePasswordChange: () => [jest.fn(), { data: null, error: null }],
useUserDetails: () => ({ data: { firstName: "John", lastName: "Doe" } }),
}));

describe("<PasswordTile />", () => {
Expand Down
6 changes: 3 additions & 3 deletions src/@next/pages/CartPage/CartPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCart, useCheckout, useUserDetails } from "@saleor/sdk";
import { useAuth, useCart, useCheckout } from "@saleor/sdk";
import { History } from "history";
import React from "react";
import { FormattedMessage } from "react-intl";
Expand Down Expand Up @@ -31,7 +31,7 @@ const getShoppingButton = (history: History) => (
</Button>
);

const getCheckoutButton = (history: History, user: UserDetails_me | null) => (
const getCheckoutButton = (history: History, user?: UserDetails_me | null) => (
<Button
testingContext="proceedToCheckoutButton"
onClick={() => history.push(user ? `/checkout/` : `/login/`)}
Expand Down Expand Up @@ -111,7 +111,7 @@ const generateCart = (

export const CartPage: React.FC<IProps> = ({}: IProps) => {
const history = useHistory();
const { data: user } = useUserDetails();
const { user } = useAuth();
const { checkout } = useCheckout();
const {
loaded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useIntl } from "react-intl";
import { RouteComponentProps, useHistory } from "react-router";

import { CheckoutAddress } from "@components/organisms";
import { useCheckout, useUserDetails } from "@saleor/sdk";
import { useAuth, useCheckout } from "@saleor/sdk";
import { ShopContext } from "@temp/components/ShopProvider/context";
import { CHECKOUT_STEPS } from "@temp/core/config";
import { commonMessages } from "@temp/intl";
Expand Down Expand Up @@ -49,7 +49,7 @@ const CheckoutAddressSubpageWithRef: RefForwardingComponent<
}));

const history = useHistory();
const { data: user } = useUserDetails();
const { user } = useAuth();
const {
checkout,
setShippingAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useIntl } from "react-intl";
import { RouteComponentProps, useHistory } from "react-router";

import { CheckoutPayment } from "@components/organisms";
import { useCart, useCheckout, useUserDetails } from "@saleor/sdk";
import { useAuth, useCart, useCheckout } from "@saleor/sdk";
import { ShopContext } from "@temp/components/ShopProvider/context";
import { CHECKOUT_STEPS } from "@temp/core/config";
import { commonMessages } from "@temp/intl";
Expand Down Expand Up @@ -42,7 +42,7 @@ const CheckoutPaymentSubpageWithRef: RefForwardingComponent<
ref
) => {
const history = useHistory();
const { data: user } = useUserDetails();
const { user } = useAuth();
const {
checkout,
billingAsShipping,
Expand Down
4 changes: 2 additions & 2 deletions src/components/CheckoutLogin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./scss/index.scss";
import React, { useContext, useState } from "react";
import { Redirect } from "react-router";

import { useUserDetails } from "@saleor/sdk";
import { useAuth } from "@saleor/sdk";

import { Offline, OfflinePlaceholder, Online, OverlayContext } from "..";

Expand All @@ -14,7 +14,7 @@ import SignInForm from "./SignInForm";
const CheckoutLogin: React.FC<{}> = () => {
const [resetPassword, setResetPassword] = useState(false);
const overlay = useContext(OverlayContext);
const { data: user } = useUserDetails();
const { user } = useAuth();
if (user) {
return <Redirect to="/checkout/" />;
}
Expand Down
22 changes: 12 additions & 10 deletions src/components/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import "./scss/index.scss";
import * as React from "react";
import { useIntl } from "react-intl";

import { useSignIn } from "@saleor/sdk";
import { useAuth } from "@saleor/sdk";
import { demoMode } from "@temp/constants";
import { commonMessages } from "@temp/intl";
import { maybe } from "@utils/misc";

import { Button, Form, TextField } from "..";

Expand All @@ -15,12 +14,19 @@ interface ILoginForm {
}

const LoginForm: React.FC<ILoginForm> = ({ hide }) => {
const [signIn, { loading, error }] = useSignIn();
const { signIn } = useAuth();
const [loading, setLoading] = React.useState(false);
const [errors, setErrors] = React.useState(null);

const handleOnSubmit = async (evt, { email, password }) => {
evt.preventDefault();
const authenticated = await signIn({ email, password });
if (authenticated && hide) {
setLoading(true);
const { data, dataError } = await signIn(email, password);
setLoading(false);
if (dataError?.error) {
setErrors(dataError.error);
} else if (data && hide) {
setErrors(null);
hide();
}
};
Expand All @@ -36,11 +42,7 @@ const LoginForm: React.FC<ILoginForm> = ({ hide }) => {

return (
<div className="login-form">
<Form
data={formData}
errors={maybe(() => error.extraInfo.userInputErrors, [])}
onSubmit={handleOnSubmit}
>
<Form data={formData} errors={errors || []} onSubmit={handleOnSubmit}>
<TextField
name="email"
autoComplete="email"
Expand Down
6 changes: 3 additions & 3 deletions src/components/MainMenu/MainMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { FormattedMessage } from "react-intl";
import { commonMessages } from "@temp/intl";
import { useCart, useSignOut, useUserDetails } from "@saleor/sdk";
import { useAuth, useCart } from "@saleor/sdk";

import Media from "react-media";
import { Link } from "react-router-dom";
import ReactSVG from "react-svg";
Expand Down Expand Up @@ -32,8 +33,7 @@ import {
import "./scss/index.scss";

const MainMenu: React.FC = () => {
const { data: user } = useUserDetails();
const [signOut] = useSignOut();
const { user, signOut } = useAuth();
const { items } = useCart();

const handleSignOut = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/OverlayManager/Cart/Cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ReactSVG from "react-svg";

import { TaxedMoney } from "@components/containers";
import { commonMessages } from "@temp/intl";
import { useCart, useCheckout, useUserDetails } from "@saleor/sdk";
import { useAuth, useCart, useCheckout } from "@saleor/sdk";

import {
Button,
Expand All @@ -26,7 +26,7 @@ import cartImg from "../../../images/cart.svg";
import closeImg from "../../../images/x.svg";

const Cart: React.FC<{ overlay: OverlayContextInterface }> = ({ overlay }) => {
const { data: user } = useUserDetails();
const { user } = useAuth();
const { checkout } = useCheckout();
const {
items,
Expand Down
Loading