Skip to content
This repository was archived by the owner on Dec 22, 2022. It is now read-only.

Commit 836d568

Browse files
committed
improve login flow
1 parent 1cc0483 commit 836d568

File tree

1 file changed

+86
-63
lines changed

1 file changed

+86
-63
lines changed

stores/customer.ts

Lines changed: 86 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,77 +15,100 @@ import {
1515
customerAccessTokenDelete,
1616
customerCreate
1717
} from '@/api/customer/mutations'
18+
import {
19+
getCustomer
20+
} from '@/api/customer/queries'
1821

1922
type CustomerMutTokenCreate = {
2023
customerAccessTokenCreate: CustomerAccessTokenCreatePayload
2124
}
2225

23-
export const useCustomer = defineStore('customer', {
24-
state: () => ({
25-
customer: null as Customer | null,
26-
customerAccessToken: null as CustomerAccessToken | null,
27-
loading: false as boolean,
28-
customerUserErrors: null as CustomerUserError[],
29-
}),
30-
actions: {
31-
async customerCreate (input: CustomerCreateInput) {
32-
if (input) {
33-
const { data } = await useClient().mutate({
34-
mutation: customerCreate,
35-
variables: { input }
36-
})
37-
if (data.customerCreate.customer) {
38-
useRouter().push({ name: 'account-login', params: { email: input.email } })
39-
}
40-
if (data.customerCreate.customerUserErrors) {
41-
this.customerUserErrors = data.customerCreate.customerUserErrors
42-
}
43-
}
44-
},
45-
async getCustomer (accessToken: string) {
46-
console.log('getCustomer', accessToken)
47-
},
48-
async login (email: string, password: string) {
49-
try {
50-
this.loading = true
51-
if (!this.customerAccessToken) {
52-
const tokenCreateInput = { email, password }
53-
this.customerAccessTokenCreate(tokenCreateInput)
26+
type CustomerMutTokenDelete = {}
27+
28+
type CustomerGet = {
29+
customer: Customer
30+
}
31+
32+
export const useCustomer = defineStore(
33+
'customer',
34+
{
35+
state: () => ({
36+
customer: null as Customer | null,
37+
customerAccessToken: null as CustomerAccessToken | null,
38+
loading: false as boolean,
39+
customerUserErrors: null as CustomerUserError[],
40+
}),
41+
actions: {
42+
async customerCreate (input: CustomerCreateInput) {
43+
if (input) {
44+
const { data } = await useClient().mutate({
45+
mutation: customerCreate,
46+
variables: { input }
47+
})
48+
if (data.customerCreate.customer) {
49+
useRouter().push({ name: 'account-login', params: { email: input.email } })
50+
}
51+
if (data.customerCreate.customerUserErrors) {
52+
this.customerUserErrors = data.customerCreate.customerUserErrors
53+
}
5454
}
55-
if (this.customerAccessToken.accessToken) {
56-
this.getCustomer(this.customerAccessToken.accessToken)
55+
},
56+
async login (email: string, password: string) {
57+
try {
58+
this.loading = true
59+
if (!this.customerAccessToken) {
60+
await this.customerAccessTokenCreate({ email, password })
61+
}
62+
if (!this.customer) {
63+
await this.getCustomer(this.customerAccessToken.accessToken)
64+
}
65+
} catch (e) {
66+
return e
67+
} finally {
68+
this.loading = false
5769
}
58-
} catch (e) {
59-
return e
60-
} finally {
61-
this.loading = false
62-
}
63-
},
64-
async logout () {},
65-
async customerAccessTokenCreate (input: CustomerAccessTokenCreateInput) {
66-
try {
67-
const { data } = await useClient().mutate<CustomerMutTokenCreate>({
68-
mutation: customerAccessTokenCreate,
69-
variables: {
70-
input,
70+
},
71+
async logout () {},
72+
async customerAccessTokenCreate (input: CustomerAccessTokenCreateInput) {
73+
try {
74+
const { data } = await useClient().mutate<CustomerMutTokenCreate>({
75+
mutation: customerAccessTokenCreate,
76+
variables: {
77+
input,
78+
}
79+
})
80+
81+
if (data.customerAccessTokenCreate.customerAccessToken) {
82+
this.customerAccessToken = data.customerAccessTokenCreate.customerAccessToken
7183
}
72-
})
73-
if (data.customerAccessTokenCreate.customerAccessToken) {
74-
this.customerAccessToken = data.customerAccessTokenCreate.customerAccessToken
84+
if (data.customerAccessTokenCreate.customerUserErrors) {
85+
this.customerUserErrors = data.customerAccessTokenCreate.customerUserErrors
86+
}
87+
} catch (e) {
88+
return e
7589
}
76-
if (data.customerAccessTokenCreate.customerUserErrors) {
77-
this.customerUserErrors = data.customerAccessTokenCreate.customerUserErrors
90+
},
91+
async getCustomer (accessToken: string) {
92+
if (accessToken) {
93+
const { data } = await useClient().query({
94+
query: getCustomer,
95+
variables: { customerAccessToken: accessToken }
96+
})
97+
if (data.customer) {
98+
this.customer = data.customer
99+
} else {
100+
this.customer = null
101+
throw new Error('No customer found')
102+
}
78103
}
79-
} catch (e) {
80-
return e
81-
}
104+
},
105+
async customerAccessTokenDelete () {},
106+
},
107+
persist: {
108+
paths: ['customer', 'customerAccessToken'],
109+
},
110+
getters: {
111+
isSignedIn: (state) => !!state.customerAccessToken,
82112
},
83-
async customerAccessTokenDelete () {},
84-
},
85-
persist: {
86-
paths: ['customer', 'customerAccessToken'],
87-
},
88-
getters: {
89-
isSignedIn: (state) => !!state.customerAccessToken,
90-
},
91-
})
113+
}
114+
)

0 commit comments

Comments
 (0)