mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
✨ feat: verify customer
:%s
This commit is contained in:
3
next-env.d.ts
vendored
3
next-env.d.ts
vendored
@@ -1,6 +1,3 @@
|
|||||||
/// <reference types="next" />
|
/// <reference types="next" />
|
||||||
/// <reference types="next/types/global" />
|
/// <reference types="next/types/global" />
|
||||||
/// <reference types="next/image-types/global" />
|
/// <reference types="next/image-types/global" />
|
||||||
|
|
||||||
// NOTE: This file should not be edited
|
|
||||||
// see https://nextjs.org/docs/basic-features/typescript for more information.
|
|
||||||
|
34
pages/verify.tsx
Normal file
34
pages/verify.tsx
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { useRouter } from 'next/router'
|
||||||
|
import { useEffect } from 'react'
|
||||||
|
import { Layout } from 'src/components/common'
|
||||||
|
import LoadingCommon from 'src/components/common/LoadingCommon/LoadingCommon'
|
||||||
|
import { useVerifyCustomer } from 'src/components/hooks'
|
||||||
|
|
||||||
|
export default function VerifyCustomer() {
|
||||||
|
const router = useRouter()
|
||||||
|
const { error, loading, verify } = useVerifyCustomer()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const { token } = router.query
|
||||||
|
console.log("token: ", token)
|
||||||
|
if (token) {
|
||||||
|
verify({ token: token.toString() })
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{
|
||||||
|
loading ? <>
|
||||||
|
<LoadingCommon />
|
||||||
|
Verifing your account ....
|
||||||
|
</> : <div className="error">
|
||||||
|
{error?.message}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
VerifyCustomer.Layout = Layout
|
@@ -1,10 +1,11 @@
|
|||||||
import React, { useEffect, useRef } from 'react'
|
|
||||||
import { ButtonCommon, Inputcommon, InputPassword } from 'src/components/common'
|
|
||||||
import s from '../FormAuthen.module.scss'
|
|
||||||
import styles from './FormRegister.module.scss'
|
|
||||||
import SocialAuthen from '../SocialAuthen/SocialAuthen'
|
|
||||||
import classNames from 'classnames'
|
import classNames from 'classnames'
|
||||||
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
|
import { ButtonCommon, Inputcommon, InputPassword } from 'src/components/common'
|
||||||
import { CustomInputCommon } from 'src/utils/type.utils'
|
import { CustomInputCommon } from 'src/utils/type.utils'
|
||||||
|
import { useSignup } from '../../../../hooks'
|
||||||
|
import s from '../FormAuthen.module.scss'
|
||||||
|
import SocialAuthen from '../SocialAuthen/SocialAuthen'
|
||||||
|
import styles from './FormRegister.module.scss'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
isHide: boolean,
|
isHide: boolean,
|
||||||
@@ -13,6 +14,10 @@ interface Props {
|
|||||||
|
|
||||||
const FormRegister = ({ onSwitch, isHide }: Props) => {
|
const FormRegister = ({ onSwitch, isHide }: Props) => {
|
||||||
const emailRef = useRef<CustomInputCommon>(null)
|
const emailRef = useRef<CustomInputCommon>(null)
|
||||||
|
const { loading, signup, error } = useSignup()
|
||||||
|
const [email, setEmail] = useState<string>('')
|
||||||
|
const [password, setPassword] = useState<string>('')
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isHide) {
|
if (!isHide) {
|
||||||
@@ -20,6 +25,18 @@ const FormRegister = ({ onSwitch, isHide }: Props) => {
|
|||||||
}
|
}
|
||||||
}, [isHide])
|
}, [isHide])
|
||||||
|
|
||||||
|
const onSignup = () => {
|
||||||
|
// TODO: validate fields
|
||||||
|
signup({ email, password })
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (error) {
|
||||||
|
alert(error.message)
|
||||||
|
}
|
||||||
|
}, [error])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={classNames({
|
<section className={classNames({
|
||||||
[s.formAuthen]: true,
|
[s.formAuthen]: true,
|
||||||
@@ -27,14 +44,29 @@ const FormRegister = ({ onSwitch, isHide }: Props) => {
|
|||||||
})}>
|
})}>
|
||||||
<div className={s.inner}>
|
<div className={s.inner}>
|
||||||
<div className={s.body}>
|
<div className={s.body}>
|
||||||
<Inputcommon placeholder='Email Address' type='email' ref={emailRef}/>
|
<Inputcommon
|
||||||
<InputPassword placeholder='Password'/>
|
placeholder='Email Address'
|
||||||
|
type='email'
|
||||||
|
ref={emailRef}
|
||||||
|
value={email}
|
||||||
|
onChange={(val) => setEmail(val.toString())}
|
||||||
|
/>
|
||||||
|
<InputPassword
|
||||||
|
placeholder='Password'
|
||||||
|
value={password}
|
||||||
|
onChange={(val) => setPassword(val.toString())}
|
||||||
|
/>
|
||||||
|
|
||||||
<div className={styles.passwordNote}>
|
<div className={styles.passwordNote}>
|
||||||
Must contain 8 characters with at least 1 uppercase and 1 lowercase letter and either 1 number or 1 special character.
|
Must contain 8 characters with at least 1 uppercase and 1 lowercase letter and either 1 number or 1 special character.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.bottom}>
|
<div className={styles.bottom}>
|
||||||
<ButtonCommon size='large'>Create Account</ButtonCommon>
|
<ButtonCommon size='large'
|
||||||
|
loading={loading}
|
||||||
|
onClick={onSignup}>
|
||||||
|
Create Account
|
||||||
|
</ButtonCommon>
|
||||||
</div>
|
</div>
|
||||||
<SocialAuthen />
|
<SocialAuthen />
|
||||||
<div className={s.others}>
|
<div className={s.others}>
|
||||||
|
@@ -1 +1,3 @@
|
|||||||
export { default as useModalCommon } from './useModalCommon'
|
export { default as useModalCommon } from './useModalCommon'
|
||||||
|
export { default as useVerifyCustomer } from './useVerifyCustomer'
|
||||||
|
export { default as useSignup } from './useSignup'
|
||||||
|
@@ -22,8 +22,8 @@ const query = gql`
|
|||||||
|
|
||||||
interface SignupInput {
|
interface SignupInput {
|
||||||
email: string
|
email: string
|
||||||
firstName: string
|
firstName?: string
|
||||||
lastName: string
|
lastName?: string
|
||||||
password: string
|
password: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
43
src/components/hooks/useVerifyCustomer.tsx
Normal file
43
src/components/hooks/useVerifyCustomer.tsx
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { LoginMutation } from '@framework/schema'
|
||||||
|
import { useState } from 'react'
|
||||||
|
import { CommonError } from 'src/domains/interfaces/CommonError'
|
||||||
|
import rawFetcher from 'src/utils/rawFetcher'
|
||||||
|
import { VERIFY_CUSTOMER_ACCOUNT } from '../../graphql/mutation'
|
||||||
|
import useActiveCustomer from './useActiveCustomer'
|
||||||
|
|
||||||
|
interface VerifyInput {
|
||||||
|
token: string
|
||||||
|
password?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const useVerifyCustomer = () => {
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
|
const [error, setError] = useState<CommonError | null>(null)
|
||||||
|
// const { mutate } = useActiveCustomer()
|
||||||
|
|
||||||
|
const verify = (options: VerifyInput) => {
|
||||||
|
setError(null)
|
||||||
|
setLoading(true)
|
||||||
|
rawFetcher<LoginMutation>({
|
||||||
|
query: VERIFY_CUSTOMER_ACCOUNT,
|
||||||
|
variables: options,
|
||||||
|
})
|
||||||
|
.then(({ data, headers }) => {
|
||||||
|
console.log("data: ", data)
|
||||||
|
// if (data.login.__typename !== 'CurrentUser') {
|
||||||
|
// throw CommonError.create(data.login.message, data.login.errorCode)
|
||||||
|
// }
|
||||||
|
// const authToken = headers.get('vendure-auth-token')
|
||||||
|
// if (authToken != null) {
|
||||||
|
// localStorage.setItem('token', authToken)
|
||||||
|
// return mutate()
|
||||||
|
// }
|
||||||
|
})
|
||||||
|
.catch(setError)
|
||||||
|
.finally(() => setLoading(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
return { loading, verify, error }
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useVerifyCustomer
|
1
src/graphql/mutation/index.ts
Normal file
1
src/graphql/mutation/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './user.mutation'
|
16
src/graphql/mutation/user.mutation.ts
Normal file
16
src/graphql/mutation/user.mutation.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { gql } from 'graphql-request'
|
||||||
|
|
||||||
|
|
||||||
|
export const VERIFY_CUSTOMER_ACCOUNT = gql`
|
||||||
|
mutation verifyCustomerAccount(token: String!, password: String) {
|
||||||
|
verifyCustomerAccount( token: $token, password: $password) {
|
||||||
|
...on CurrentUser {
|
||||||
|
id
|
||||||
|
identifier
|
||||||
|
}
|
||||||
|
...ErrorResult
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
|
1
src/graphql/query/index.ts
Normal file
1
src/graphql/query/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// export * from './user.mutation'
|
1
src/graphql/query/user.query.ts
Normal file
1
src/graphql/query/user.query.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// query here
|
Reference in New Issue
Block a user