Login & Token

- Changed the loging fetch
 - Store the token and user id in cookie
 - Ue the cookie to make user calls like get user
This commit is contained in:
GunaTrika 2021-09-28 10:32:22 +05:30
parent e5a36389f9
commit b5051f50e4
6 changed files with 54 additions and 25 deletions

View File

@ -2,15 +2,15 @@ import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types' import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors' import { CommerceError } from '@commerce/utils/errors'
import useLogin, { UseLogin } from '@commerce/auth/use-login' import useLogin, { UseLogin } from '@commerce/auth/use-login'
import type { LoginHook } from '../types/login'
import useCustomer from '../customer/use-customer' import useCustomer from '../customer/use-customer'
import epClient from '../utils/ep-client' import Cookies from 'js-cookie'
export default useLogin as UseLogin<typeof handler> export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<any> = { export const handler: MutationHook<any> = {
fetchOptions: { fetchOptions: {
query: '' url: 'Customers',
method: 'TokenViaPassword',
}, },
async fetcher({ input: { email, password }, options, fetch }) { async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) { if (!(email && password)) {
@ -19,8 +19,17 @@ export const handler: MutationHook<any> = {
'A first name, last name, email and password are required to login', 'A first name, last name, email and password are required to login',
}) })
} }
const {data:token} = await fetch({
...options,
variables:{
params: [email, password]
}
});
let token = await epClient.Customers.TokenViaPassword(email, password); let expireTime = Math.round(token.expires/(1000*24*60*60));
Cookies.set("user_token",
JSON.stringify(token),
{ expires: expireTime });
return token || null; return token || null;
}, },
useHook: ({ fetch }) => () => { useHook: ({ fetch }) => () => {

View File

@ -1,17 +1,28 @@
import { MutationHook } from '@commerce/utils/types' import { MutationHook } from '@commerce/utils/types'
import useLogout, { UseLogout } from '@commerce/auth/use-logout' import useLogout, { UseLogout } from '@commerce/auth/use-logout'
import Cookies from 'js-cookie'
export default useLogout as UseLogout<typeof handler> export default useLogout as UseLogout<typeof handler>
import useCustomer from '../customer/use-customer'
import { useCallback } from 'react'
export const handler: MutationHook<any> = { export const handler: MutationHook<any> = {
fetchOptions: { fetchOptions: {
query: '', query: '?',
}, },
async fetcher() { async fetcher() {
return null Cookies.remove("user_token");
},
useHook: ({ fetch }) => () => {
const { mutate } = useCustomer()
return useCallback(
async function logout() {
const data = await fetch()
await mutate(null, false)
return data
},
[fetch, mutate]
)
}, },
useHook:
({ fetch }) =>
() =>
async () => {},
} }

View File

@ -19,7 +19,6 @@ export const handler: SWRHook<any> = {
const response = useData({ const response = useData({
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
}) })
console.log("checking data..", epClient.Cart());
return useMemo( return useMemo(
() => () =>
Object.create(response, { Object.create(response, {

View File

@ -1,27 +1,33 @@
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import getCustomerCookie from '../utils/get-customer-creds' import getCustomerCookie from '../utils/get-customer-creds'
import epClient from '../utils/ep-client'
const creds = getCustomerCookie();
export default useCustomer as UseCustomer<typeof handler> export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<any> = { export const handler: SWRHook<any> = {
fetchOptions: { fetchOptions: {
query: '', url: 'Customers',
method: 'Get',
}, },
async fetcher() { async fetcher({fetch, options, input}) {
const creds = getCustomerCookie(); const creds = getCustomerCookie();
if(!creds.id || !creds.token) {
// if user is not logged-in return null
if(!creds) {
return null; return null;
} }
console.log('moltin sdk', epClient);
const {data:customer} = await epClient.Customers.Get(creds.customer_id, creds.token); const {data} = await fetch({
...options,
variables:{
params: [creds.id, creds.token]
}
});
console.log(data);
return { return {
...customer, ...data,
firstName: customer.name.split(" ")[0], firstName: data.name.split(" ")[0],
lastName: customer.name.split(" ")[1] lastName: data.name.split(" ")[1]
} }
}, },
useHook: ({ useData }) => (input) => { useHook: ({ useData }) => (input) => {

View File

@ -0,0 +1,5 @@
import * as Core from '@commerce/types/customer'
export * from '@commerce/types/customer'
export type CustomerSchema = Core.CustomerSchema

View File

@ -1,11 +1,10 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
const getCustomerCookie = () => { const getCustomerCookie = () => {
const customerCookieObj = Cookies.get('customer_token'); const customerCookieObj = Cookies.get("user_token");
if(customerCookieObj) { if(customerCookieObj) {
try { try {
return JSON.parse(atob(customerCookieObj)); return JSON.parse(customerCookieObj);
} catch(err) { } catch(err) {
return false; return false;
} }