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 { CommerceError } from '@commerce/utils/errors'
import useLogin, { UseLogin } from '@commerce/auth/use-login'
import type { LoginHook } from '../types/login'
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 const handler: MutationHook<any> = {
fetchOptions: {
query: ''
url: 'Customers',
method: 'TokenViaPassword',
},
async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) {
@ -19,8 +19,17 @@ export const handler: MutationHook<any> = {
'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;
},
useHook: ({ fetch }) => () => {

View File

@ -1,17 +1,28 @@
import { MutationHook } from '@commerce/utils/types'
import useLogout, { UseLogout } from '@commerce/auth/use-logout'
import Cookies from 'js-cookie'
export default useLogout as UseLogout<typeof handler>
import useCustomer from '../customer/use-customer'
import { useCallback } from 'react'
export const handler: MutationHook<any> = {
fetchOptions: {
query: '',
query: '?',
},
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({
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
})
console.log("checking data..", epClient.Cart());
return useMemo(
() =>
Object.create(response, {

View File

@ -1,27 +1,33 @@
import { SWRHook } from '@commerce/utils/types'
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import getCustomerCookie from '../utils/get-customer-creds'
import epClient from '../utils/ep-client'
const creds = getCustomerCookie();
export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
url: 'Customers',
method: 'Get',
},
async fetcher() {
async fetcher({fetch, options, input}) {
const creds = getCustomerCookie();
// if user is not logged-in return null
if(!creds) {
if(!creds.id || !creds.token) {
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 {
...customer,
firstName: customer.name.split(" ")[0],
lastName: customer.name.split(" ")[1]
...data,
firstName: data.name.split(" ")[0],
lastName: data.name.split(" ")[1]
}
},
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'
const getCustomerCookie = () => {
const customerCookieObj = Cookies.get('customer_token');
const customerCookieObj = Cookies.get("user_token");
if(customerCookieObj) {
try {
return JSON.parse(atob(customerCookieObj));
return JSON.parse(customerCookieObj);
} catch(err) {
return false;
}