GraphQL Query

This commit is contained in:
SushantJadhav 2021-08-25 16:09:47 +05:30
parent 05021b5529
commit 56dbee6955
3 changed files with 29 additions and 8 deletions

View File

@ -9,6 +9,7 @@ const login: LoginEndpoint['handlers']['login'] = async ({
config, config,
commerce, commerce,
}) => { }) => {
console.log('login hit', email, password)
// TODO: Add proper validations with something like Ajv // TODO: Add proper validations with something like Ajv
if (!(email && password)) { if (!(email && password)) {
return res.status(400).json({ return res.status(400).json({
@ -16,12 +17,13 @@ const login: LoginEndpoint['handlers']['login'] = async ({
errors: [{ message: 'Invalid request' }], errors: [{ message: 'Invalid request' }],
}) })
} }
// TODO: validate the password and email
// Passwords must be at least 7 characters and contain both alphabetic
// and numeric characters.
try { try {
await commerce.login({ variables: { email, password }, config, res }) /*
const loginMutation = ` mutation loginAccount($input) { login($input) { } } `
const variables = { input: { email, password } }
const loginResponse = await config.fetch(loginMutation, { variables })
setCookie(res)
*/
} catch (error) { } catch (error) {
// Check if the email and password didn't match an existing account // Check if the email and password didn't match an existing account
if ( if (

View File

@ -5,14 +5,14 @@ import { useCallback } from 'react'
import { CommerceError } from '@commerce/utils/errors' import { CommerceError } from '@commerce/utils/errors'
import type { LoginHook } from '../types/login' import type { LoginHook } from '../types/login'
import useCustomer from '../customer/use-customer' import useCustomer from '../customer/use-customer'
import { loginMutation } from '../utils/mutations/login-mutation'
export default useLogin as UseLogin<typeof handler> export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<LoginHook> = { export const handler: MutationHook<LoginHook> = {
fetchOptions: { fetchOptions: {
url: '/api/login', url: '/api/login',
method: 'POST', method: 'POST',
query: '' query: loginMutation
}, },
async fetcher({ input: { email, password }, options, fetch }) { async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) { if (!(email && password)) {

View File

@ -0,0 +1,19 @@
export const loginMutation = `
mutation login($loginInput:CustomerUserAuthInfoInput!) {
account:createCustomerAuthTicket(customerUserAuthInfoInput:$loginInput) {
accessToken
userId
refreshToken
refreshTokenExpiration
customerAccount {
id
firstName
lastName
emailAddress
userName
}
}
}
`