mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Merge pull request #70 from KieIO/feature/m1-authen
refactor folder hooks/auth
This commit is contained in:
@@ -1,8 +1,5 @@
|
|||||||
import { gql } from 'graphql-request'
|
export const verifyCustomerAccountMutaton = /* GraphQL */ `
|
||||||
|
mutation verifyCustomerAccount($token: String!, $password: String) {
|
||||||
|
|
||||||
export const VERIFY_CUSTOMER_ACCOUNT = gql`
|
|
||||||
mutation verifyCustomerAccount($token: String!, $password: String) {
|
|
||||||
verifyCustomerAccount( token: $token, password: $password) {
|
verifyCustomerAccount( token: $token, password: $password) {
|
||||||
__typename
|
__typename
|
||||||
...on CurrentUser {
|
...on CurrentUser {
|
||||||
@@ -16,5 +13,3 @@ mutation verifyCustomerAccount($token: String!, $password: String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@@ -1,6 +1,6 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { Layout } from 'src/components/common'
|
import { Layout } from 'src/components/common'
|
||||||
import useActiveCustomer from 'src/components/hooks/useActiveCustomer'
|
import { useActiveCustomer } from 'src/components/hooks/auth'
|
||||||
import { AccountPage, AccountSignIn } from 'src/components/modules/account'
|
import { AccountPage, AccountSignIn } from 'src/components/modules/account'
|
||||||
|
|
||||||
const Account = () => {
|
const Account = () => {
|
||||||
|
@@ -22,7 +22,7 @@ import {
|
|||||||
import Logo from '../../../Logo/Logo'
|
import Logo from '../../../Logo/Logo'
|
||||||
import s from './HeaderMenu.module.scss'
|
import s from './HeaderMenu.module.scss'
|
||||||
import { useLogout } from '../../../../hooks/auth'
|
import { useLogout } from '../../../../hooks/auth'
|
||||||
import useActiveCustomer from 'src/components/hooks/useActiveCustomer'
|
import { useActiveCustomer } from 'src/components/hooks/auth'
|
||||||
interface Props {
|
interface Props {
|
||||||
children?: any
|
children?: any
|
||||||
isFull?: boolean
|
isFull?: boolean
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import classNames from 'classnames'
|
import classNames from 'classnames'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import useActiveCustomer from 'src/components/hooks/useActiveCustomer'
|
import { useActiveCustomer } from 'src/components/hooks/auth'
|
||||||
import { ROUTE } from 'src/utils/constanst.utils'
|
import { ROUTE } from 'src/utils/constanst.utils'
|
||||||
import ModalCommon from '../ModalCommon/ModalCommon'
|
import ModalCommon from '../ModalCommon/ModalCommon'
|
||||||
import FormLogin from './components/FormLogin/FormLogin'
|
import FormLogin from './components/FormLogin/FormLogin'
|
||||||
|
@@ -3,7 +3,7 @@ import Link from 'next/link'
|
|||||||
import React, { useEffect, useRef } from 'react'
|
import React, { useEffect, useRef } from 'react'
|
||||||
import { ButtonCommon, InputFiledInForm, InputPasswordFiledInForm } from 'src/components/common'
|
import { ButtonCommon, InputFiledInForm, InputPasswordFiledInForm } from 'src/components/common'
|
||||||
import { useMessage } from 'src/components/contexts'
|
import { useMessage } from 'src/components/contexts'
|
||||||
import useLogin from 'src/components/hooks/useLogin'
|
import useLogin from 'src/components/hooks/auth/useLogin'
|
||||||
import { ROUTE } from 'src/utils/constanst.utils'
|
import { ROUTE } from 'src/utils/constanst.utils'
|
||||||
import { LANGUAGE } from 'src/utils/language.utils'
|
import { LANGUAGE } from 'src/utils/language.utils'
|
||||||
import { CustomInputCommon } from 'src/utils/type.utils'
|
import { CustomInputCommon } from 'src/utils/type.utils'
|
||||||
|
@@ -10,7 +10,7 @@ import { useMessage } from 'src/components/contexts'
|
|||||||
import { LANGUAGE } from 'src/utils/language.utils'
|
import { LANGUAGE } from 'src/utils/language.utils'
|
||||||
import { CustomInputCommon } from 'src/utils/type.utils'
|
import { CustomInputCommon } from 'src/utils/type.utils'
|
||||||
import * as Yup from 'yup'
|
import * as Yup from 'yup'
|
||||||
import { useSignup } from '../../../../hooks'
|
import { useSignup } from '../../../../hooks/auth'
|
||||||
import s from '../FormAuthen.module.scss'
|
import s from '../FormAuthen.module.scss'
|
||||||
import SocialAuthen from '../SocialAuthen/SocialAuthen'
|
import SocialAuthen from '../SocialAuthen/SocialAuthen'
|
||||||
import styles from './FormRegister.module.scss'
|
import styles from './FormRegister.module.scss'
|
||||||
|
@@ -1,3 +1,6 @@
|
|||||||
|
export { default as useSignup } from './useSignup'
|
||||||
|
export { default as useLogin } from './useLogin'
|
||||||
export { default as useLogout } from './useLogout'
|
export { default as useLogout } from './useLogout'
|
||||||
|
export { default as useVerifyCustomer } from './useVerifyCustomer'
|
||||||
|
export { default as useActiveCustomer } from './useActiveCustomer'
|
||||||
|
|
||||||
|
11
src/components/hooks/auth/useActiveCustomer.tsx
Normal file
11
src/components/hooks/auth/useActiveCustomer.tsx
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { ActiveCustomerQuery } from '@framework/schema'
|
||||||
|
import { activeCustomerQuery } from '@framework/utils/queries/active-customer-query'
|
||||||
|
import gglFetcher from 'src/utils/gglFetcher'
|
||||||
|
import useSWR from 'swr'
|
||||||
|
|
||||||
|
const useActiveCustomer = () => {
|
||||||
|
const { data, ...rest } = useSWR<ActiveCustomerQuery>([activeCustomerQuery], gglFetcher)
|
||||||
|
return { customer: data?.activeCustomer, ...rest }
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useActiveCustomer
|
@@ -1,4 +1,3 @@
|
|||||||
import { gql } from 'graphql-request'
|
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import useActiveCustomer from './useActiveCustomer'
|
import useActiveCustomer from './useActiveCustomer'
|
||||||
import { CommonError } from 'src/domains/interfaces/CommonError'
|
import { CommonError } from 'src/domains/interfaces/CommonError'
|
||||||
@@ -6,21 +5,7 @@ import rawFetcher from 'src/utils/rawFetcher'
|
|||||||
import { LoginMutation } from '@framework/schema'
|
import { LoginMutation } from '@framework/schema'
|
||||||
import { LOCAL_STORAGE_KEY } from 'src/utils/constanst.utils'
|
import { LOCAL_STORAGE_KEY } from 'src/utils/constanst.utils'
|
||||||
import { errorMapping } from 'src/utils/errrorMapping'
|
import { errorMapping } from 'src/utils/errrorMapping'
|
||||||
|
import { loginMutation } from '@framework/utils/mutations/log-in-mutation'
|
||||||
const query = gql`
|
|
||||||
mutation login($username: String!, $password: String!) {
|
|
||||||
login(username: $username, password: $password) {
|
|
||||||
__typename
|
|
||||||
... on CurrentUser {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
... on ErrorResult {
|
|
||||||
errorCode
|
|
||||||
message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
interface LoginInput {
|
interface LoginInput {
|
||||||
username: string
|
username: string
|
||||||
@@ -38,7 +23,7 @@ const useLogin = () => {
|
|||||||
setError(null)
|
setError(null)
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
rawFetcher<LoginMutation>({
|
rawFetcher<LoginMutation>({
|
||||||
query,
|
query: loginMutation,
|
||||||
variables: options,
|
variables: options,
|
||||||
})
|
})
|
||||||
.then(({ data, headers }) => {
|
.then(({ data, headers }) => {
|
@@ -4,7 +4,7 @@ import { useState } from 'react'
|
|||||||
import { CommonError } from 'src/domains/interfaces/CommonError'
|
import { CommonError } from 'src/domains/interfaces/CommonError'
|
||||||
import { LOCAL_STORAGE_KEY } from 'src/utils/constanst.utils'
|
import { LOCAL_STORAGE_KEY } from 'src/utils/constanst.utils'
|
||||||
import rawFetcher from 'src/utils/rawFetcher'
|
import rawFetcher from 'src/utils/rawFetcher'
|
||||||
import useActiveCustomer from '../useActiveCustomer'
|
import useActiveCustomer from './useActiveCustomer'
|
||||||
|
|
||||||
const useLogout = () => {
|
const useLogout = () => {
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
|
@@ -1,24 +1,9 @@
|
|||||||
import { gql } from 'graphql-request'
|
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import useActiveCustomer from './useActiveCustomer'
|
import useActiveCustomer from './useActiveCustomer'
|
||||||
import { SignupMutation } from '@framework/schema'
|
import { SignupMutation } from '@framework/schema'
|
||||||
import fetcher from 'src/utils/fetcher'
|
import fetcher from 'src/utils/fetcher'
|
||||||
import { CommonError } from 'src/domains/interfaces/CommonError'
|
import { CommonError } from 'src/domains/interfaces/CommonError'
|
||||||
|
import { signupMutation } from '@framework/utils/mutations/sign-up-mutation'
|
||||||
const query = gql`
|
|
||||||
mutation signup($input: RegisterCustomerInput!) {
|
|
||||||
registerCustomerAccount(input: $input) {
|
|
||||||
__typename
|
|
||||||
... on Success {
|
|
||||||
success
|
|
||||||
}
|
|
||||||
... on ErrorResult {
|
|
||||||
errorCode
|
|
||||||
message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
interface SignupInput {
|
interface SignupInput {
|
||||||
email: string
|
email: string
|
||||||
@@ -39,7 +24,7 @@ const useSignup = () => {
|
|||||||
setError(null)
|
setError(null)
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
fetcher<SignupMutation>({
|
fetcher<SignupMutation>({
|
||||||
query,
|
query: signupMutation,
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
firstName,
|
firstName,
|
@@ -2,8 +2,8 @@ import { VerifyCustomerAccountMutation } from '@framework/schema'
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { CommonError } from 'src/domains/interfaces/CommonError'
|
import { CommonError } from 'src/domains/interfaces/CommonError'
|
||||||
import rawFetcher from 'src/utils/rawFetcher'
|
import rawFetcher from 'src/utils/rawFetcher'
|
||||||
import { VERIFY_CUSTOMER_ACCOUNT } from '../../graphql/mutation'
|
|
||||||
import useActiveCustomer from './useActiveCustomer'
|
import useActiveCustomer from './useActiveCustomer'
|
||||||
|
import { verifyCustomerAccountMutaton } from '@framework/utils/mutations/verify-customer-account-mutation'
|
||||||
|
|
||||||
interface VerifyInput {
|
interface VerifyInput {
|
||||||
token: string
|
token: string
|
||||||
@@ -22,7 +22,7 @@ const useVerifyCustomer = () => {
|
|||||||
setError(null)
|
setError(null)
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
rawFetcher<VerifyCustomerAccountMutation>({
|
rawFetcher<VerifyCustomerAccountMutation>({
|
||||||
query: VERIFY_CUSTOMER_ACCOUNT,
|
query: verifyCustomerAccountMutaton,
|
||||||
variables: options,
|
variables: options,
|
||||||
})
|
})
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
@@ -1,3 +1 @@
|
|||||||
export { default as useModalCommon } from './useModalCommon'
|
export { default as useModalCommon } from './useModalCommon'
|
||||||
export { default as useVerifyCustomer } from './useVerifyCustomer'
|
|
||||||
export { default as useSignup } from './useSignup'
|
|
||||||
|
@@ -1,22 +0,0 @@
|
|||||||
import { ActiveCustomerQuery } from '@framework/schema'
|
|
||||||
import { gql } from 'graphql-request'
|
|
||||||
import gglFetcher from 'src/utils/gglFetcher'
|
|
||||||
import useSWR from 'swr'
|
|
||||||
|
|
||||||
const query = gql`
|
|
||||||
query activeCustomer {
|
|
||||||
activeCustomer {
|
|
||||||
id
|
|
||||||
firstName
|
|
||||||
lastName
|
|
||||||
emailAddress
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
const useActiveCustomer = () => {
|
|
||||||
const { data, ...rest } = useSWR<ActiveCustomerQuery>([query], gglFetcher)
|
|
||||||
return { customer: data?.activeCustomer, ...rest }
|
|
||||||
}
|
|
||||||
|
|
||||||
export default useActiveCustomer
|
|
@@ -5,7 +5,7 @@ import Image from 'next/image'
|
|||||||
import avatar from '../../assets/avatar.png'
|
import avatar from '../../assets/avatar.png'
|
||||||
|
|
||||||
import { ButtonCommon } from 'src/components/common'
|
import { ButtonCommon } from 'src/components/common'
|
||||||
import useActiveCustomer from 'src/components/hooks/useActiveCustomer'
|
import { useActiveCustomer } from 'src/components/hooks/auth'
|
||||||
|
|
||||||
interface AccountProps {
|
interface AccountProps {
|
||||||
name: string
|
name: string
|
||||||
|
@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
|
|||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { ButtonCommon } from 'src/components/common'
|
import { ButtonCommon } from 'src/components/common'
|
||||||
import LoadingCommon from 'src/components/common/LoadingCommon/LoadingCommon'
|
import LoadingCommon from 'src/components/common/LoadingCommon/LoadingCommon'
|
||||||
import { useModalCommon, useVerifyCustomer } from 'src/components/hooks'
|
import { useModalCommon } from 'src/components/hooks'
|
||||||
|
import { useVerifyCustomer } from 'src/components/hooks/auth'
|
||||||
import { ROUTE } from 'src/utils/constanst.utils'
|
import { ROUTE } from 'src/utils/constanst.utils'
|
||||||
import s from './VerifyCustomerAccount.module.scss'
|
import s from './VerifyCustomerAccount.module.scss'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
|
@@ -1 +0,0 @@
|
|||||||
export * from './user.mutation'
|
|
@@ -1 +0,0 @@
|
|||||||
// export * from './user.mutation'
|
|
@@ -1 +0,0 @@
|
|||||||
// query here
|
|
Reference in New Issue
Block a user