Skip to content

Commit

Permalink
Change customer token into cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning committed Oct 18, 2023
1 parent 62a26fa commit 539a3f2
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@tailwindcss/typography": "^0.5.9",
"@vitejs/plugin-vue2": "^2.2.0",
"@vueuse/core": "^9.12.0",
"@vueuse/integrations": "^10.5.0",
"autoprefixer": "^10.4.15",
"axios": "^1.2.6",
"cross-env": "^7.0.3",
Expand All @@ -23,6 +24,7 @@
"rollup-plugin-visualizer": "^5.9.0",
"tailwind-scrollbar-hide": "^1.1.7",
"tailwindcss": "^3.3.3",
"universal-cookie": "^6.1.1",
"vite": "^4.0.4",
"vue": "^2.7",
"vue-clickaway": "^2.2.2",
Expand Down
5 changes: 3 additions & 2 deletions resources/js/axios.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import axios from 'axios'
window.axios = axios
import { token } from './stores/useUser'

window.axios = axios
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'

window.magento = axios.create()
window.magento.defaults.baseURL = config.magento_url + '/rest/' + config.store_code + '/V1/'

window.magentoUser = axios.create()
window.magentoUser.defaults.baseURL = config.magento_url + '/rest/' + config.store_code + '/V1/'
window.magentoUser.defaults.headers.common['Authorization'] = `Bearer ${localStorage.token}`
window.magentoUser.defaults.headers.common['Authorization'] = `Bearer ${token.value}`

// It's not possible to set global interceptors like headers
// or the base url can; so we set them for all instances.
Expand Down
3 changes: 2 additions & 1 deletion resources/js/components/Checkout/CheckoutSuccess.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import GetCart from './../Cart/mixins/GetCart'
import { token } from '../../stores/useUser'
export default {
mixins: [GetCart],
Expand All @@ -26,7 +27,7 @@ export default {
},
created() {
this.token ??= localStorage.token
this.token ??= token.value
this.mask ??= localStorage.mask
this.refreshOrder()
Expand Down
3 changes: 2 additions & 1 deletion resources/js/components/GraphqlMutation.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import InteractWithUser from './User/mixins/InteractWithUser'
import { token } from '../stores/useUser'
export default {
mixins: [InteractWithUser],
Expand Down Expand Up @@ -103,7 +104,7 @@ export default {
let options = { headers: {} }
if (this.$root.loggedIn) {
options['headers']['Authorization'] = `Bearer ${localStorage.token}`
options['headers']['Authorization'] = `Bearer ${token.value}`
}
if (this.recaptcha) {
Expand Down
20 changes: 8 additions & 12 deletions resources/js/components/Product/Price.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ export default {
let special_price = options.special_price ?? false
let displayTax = this.$root.includeTaxAt(location)
let price = special_price ? product.special_price ?? product.price ?? 0 : product.price ?? 0
let price = (special_price ? product.special_price ?? product.price ?? 0 : product.price ?? 0) * 1
if (options.tier_price) {
price = options.tier_price.price
price = options.tier_price.price * 1
}
if (options.product_options) {
price += this.calculateOptionsValue(price, product, options.product_options)
}
let taxMultiplier = this.getTaxPercent(product) + 1
let taxMultiplier = this.getTaxPercent(product) * 1 + 1
if (window.config.tax.calculation.price_includes_tax == displayTax) {
return price
Expand All @@ -55,11 +55,9 @@ export default {
},
calculateOptionsValue(basePrice, product, customOptions) {
let addition = 0
Object.entries(customOptions).forEach(([key, val]) => {
return Object.entries(customOptions).reduce((priceAddition, [key, val]) => {
if (!val) {
return
return priceAddition
}
let option = product.options.find((option) => option.option_id == key)
Expand All @@ -68,13 +66,11 @@ export default {
: option.price
if (optionPrice.price_type == 'fixed') {
addition += parseFloat(optionPrice.price)
} else {
addition += (parseFloat(basePrice) * parseFloat(optionPrice.price)) / 100
return (priceAddition * 1) + parseFloat(optionPrice.price)
}
})
return addition
return (priceAddition * 1) + (parseFloat(basePrice) * parseFloat(optionPrice.price)) / 100
}, 0)
},
},
Expand Down
17 changes: 15 additions & 2 deletions resources/js/stores/useUser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { useLocalStorage, useSessionStorage, StorageSerializers } from '@vueuse/core'
import { useSessionStorage, StorageSerializers } from '@vueuse/core'
import { clear as clearCart } from './useCart'
import { computed, watch } from 'vue'
import { useCookies } from '@vueuse/integrations/useCookies';
const cookies = useCookies(['customer_token']);

export const token = useLocalStorage('token', '')
export const token = computed({
get() {
return cookies.get('customer_token');
},
set(value) {
if (value === null || value === undefined) {
cookies.remove('customer_token');
}

cookies.set('customer_token', value);
},
})
const userStorage = useSessionStorage('user', {}, { serializer: StorageSerializers.object })
let isRefreshing = false

Expand Down
1 change: 0 additions & 1 deletion resources/views/product/partials/addtocart.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
</div>

@include('rapidez::product.partials.options')

<ul class="flex flex-col" v-if="tierPrices && Object.keys(tierPrices).length" v-cloak>
<li v-for="tier in tierPrices">
@lang('Order :amount and pay :price per item', [
Expand Down
2 changes: 1 addition & 1 deletion resources/views/product/partials/options.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="flex flex-col space-y-3">
@foreach($product->options->sortBy('sort_order') as $option)
<div>
@include('rapidez::product.partials.options.'.$option->type_id)
@include('rapidez::product.partials.options.'.($option->type_id ?? $option->type))
</div>
@endforeach
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/Auth/MagentoCustomerTokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

class MagentoCustomerTokenGuard extends TokenGuard implements Guard
{
public function getTokenForRequest()
{
$token = parent::getTokenForRequest();

if (empty($token)) {
$token = $_COOKIE[$this->inputKey] ?? null;
}

return $token;
}


/**
* Get the currently authenticated user.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function scopeWhereToken(Builder $query, string $token)
DecodeJwt::isJwt($token),
fn (Builder $query) => $query
->where(
$this->qualifyColumn('customer_id'),
$this->getQualifiedKeyName(),
DecodeJwt::decode($token)
->claims()
->get('uid')
Expand Down
2 changes: 1 addition & 1 deletion src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function register()
protected function bootAuth(): self
{
auth()->extend('magento-customer', function (Application $app, string $name, array $config) {
return new MagentoCustomerTokenGuard(auth()->createUserProvider($config['provider']), request(), 'token', 'token');
return new MagentoCustomerTokenGuard(auth()->createUserProvider($config['provider']), request(), 'customer_token', 'customer_token');
});

config([
Expand Down
59 changes: 59 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@
lodash.merge "^4.6.2"
postcss-selector-parser "6.0.10"

"@types/cookie@^0.5.1":
version "0.5.3"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.5.3.tgz#3f98076ede5e467783507284d3c19215327fff8f"
integrity sha512-SLg07AS9z1Ab2LU+QxzU8RCmzsja80ywjf/t5oqw+4NSH20gIGlhLOrBDm1L3PBWzPa4+wkgFQVZAjE6Ioj2ug==

"@types/parse-json@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
Expand All @@ -354,6 +359,11 @@
resolved "https://registry.yarnpkg.com/@types/web-bluetooth/-/web-bluetooth-0.0.16.tgz#1d12873a8e49567371f2a75fe3e7f7edca6662d8"
integrity sha512-oh8q2Zc32S6gd/j50GowEjKLoOVOwHP/bWVjKJInBwQqdOYMdPrf1oVlelTlyfFK3CKxL1uahMDAr+vy8T7yMQ==

"@types/web-bluetooth@^0.0.18":
version "0.0.18"
resolved "https://registry.yarnpkg.com/@types/web-bluetooth/-/web-bluetooth-0.0.18.tgz#74bd1c8fd3a2058cb6fc76b188fcded50a83d866"
integrity sha512-v/ZHEj9xh82usl8LMR3GarzFY1IrbXJw5L4QfQhokjRV91q+SelFqxQWSep1ucXEZ22+dSTwLFkXeur25sPIbw==

"@vitejs/plugin-vue2@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@vitejs/plugin-vue2/-/plugin-vue2-2.2.0.tgz#7453207197d6ac2b7023cedc7133b142c604c356"
Expand All @@ -373,6 +383,16 @@
postcss "^8.4.14"
source-map "^0.6.1"

"@vueuse/[email protected]":
version "10.5.0"
resolved "https://registry.yarnpkg.com/@vueuse/core/-/core-10.5.0.tgz#04d1e6d26592bb997bb755a4830ea7583c3e8612"
integrity sha512-z/tI2eSvxwLRjOhDm0h/SXAjNm8N5ld6/SC/JQs6o6kpJ6Ya50LnEL8g5hoYu005i28L0zqB5L5yAl8Jl26K3A==
dependencies:
"@types/web-bluetooth" "^0.0.18"
"@vueuse/metadata" "10.5.0"
"@vueuse/shared" "10.5.0"
vue-demi ">=0.14.6"

"@vueuse/core@^9.12.0":
version "9.12.0"
resolved "https://registry.yarnpkg.com/@vueuse/core/-/core-9.12.0.tgz#e5b20f901e081c7ae5fe0e5f3af217929034eefe"
Expand All @@ -383,11 +403,32 @@
"@vueuse/shared" "9.12.0"
vue-demi "*"

"@vueuse/integrations@^10.5.0":
version "10.5.0"
resolved "https://registry.yarnpkg.com/@vueuse/integrations/-/integrations-10.5.0.tgz#38f00bd5a1cd0160645f0c75efd5d9579061e3d6"
integrity sha512-fm5sXLCK0Ww3rRnzqnCQRmfjDURaI4xMsx+T+cec0ngQqHx/JgUtm8G0vRjwtonIeTBsH1Q8L3SucE+7K7upJQ==
dependencies:
"@vueuse/core" "10.5.0"
"@vueuse/shared" "10.5.0"
vue-demi ">=0.14.6"

"@vueuse/[email protected]":
version "10.5.0"
resolved "https://registry.yarnpkg.com/@vueuse/metadata/-/metadata-10.5.0.tgz#7501a88cf5cbf7a515a03f0b8bbe3cecf30cad11"
integrity sha512-fEbElR+MaIYyCkeM0SzWkdoMtOpIwO72x8WsZHRE7IggiOlILttqttM69AS13nrDxosnDBYdyy3C5mR1LCxHsw==

"@vueuse/[email protected]":
version "9.12.0"
resolved "https://registry.yarnpkg.com/@vueuse/metadata/-/metadata-9.12.0.tgz#19a0fefcba6a66a2382af10a7a67ebad6eec1f27"
integrity sha512-9oJ9MM9lFLlmvxXUqsR1wLt1uF7EVbP5iYaHJYqk+G2PbMjY6EXvZeTjbdO89HgoF5cI6z49o2zT/jD9SVoNpQ==

"@vueuse/[email protected]":
version "10.5.0"
resolved "https://registry.yarnpkg.com/@vueuse/shared/-/shared-10.5.0.tgz#b3ac8c190a5dae41db5e1b60fe304a9b4247393c"
integrity sha512-18iyxbbHYLst9MqU1X1QNdMHIjks6wC7XTVf0KNOv5es/Ms6gjVFCAAWTVP2JStuGqydg3DT+ExpFORUEi9yhg==
dependencies:
vue-demi ">=0.14.6"

"@vueuse/[email protected]":
version "9.12.0"
resolved "https://registry.yarnpkg.com/@vueuse/shared/-/shared-9.12.0.tgz#e6597da80084cba8fc3d6545f4c2fa9817b80428"
Expand Down Expand Up @@ -640,6 +681,11 @@ convert-source-map@^1.5.0, convert-source-map@^1.5.1:
dependencies:
safe-buffer "~5.1.1"

cookie@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==

core-js@^3.6.5:
version "3.26.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.26.1.tgz#7a9816dabd9ee846c1c0fe0e8fcad68f3709134e"
Expand Down Expand Up @@ -1693,6 +1739,14 @@ ts-interface-checker@^0.1.9:
resolved "https://registry.yarnpkg.com/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz#784fd3d679722bc103b1b4b8030bcddb5db2a699"
integrity sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==

universal-cookie@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/universal-cookie/-/universal-cookie-6.1.1.tgz#2d619e21804c93b39ff0c77fe47dafd7a492346d"
integrity sha512-33S9x3CpdUnnjwTNs2Fgc41WGve2tdLtvaK2kPSbZRc5pGpz2vQFbRWMxlATsxNNe/Cy8SzmnmbuBM85jpZPtA==
dependencies:
"@types/cookie" "^0.5.1"
cookie "^0.5.0"

update-browserslist-db@^1.0.11:
version "1.0.11"
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.11.tgz#9a2a641ad2907ae7b3616506f4b977851db5b940"
Expand Down Expand Up @@ -1758,6 +1812,11 @@ vue-demi@*:
resolved "https://registry.yarnpkg.com/vue-demi/-/vue-demi-0.13.11.tgz#7d90369bdae8974d87b1973564ad390182410d99"
integrity sha512-IR8HoEEGM65YY3ZJYAjMlKygDQn25D5ajNFNoKh9RSDMQtlzCxtfQjdQgv9jjK+m3377SsJXY8ysq8kLCZL25A==

vue-demi@>=0.14.6:
version "0.14.6"
resolved "https://registry.yarnpkg.com/vue-demi/-/vue-demi-0.14.6.tgz#dc706582851dc1cdc17a0054f4fec2eb6df74c92"
integrity sha512-8QA7wrYSHKaYgUxDA5ZC24w+eHm3sYCbp0EzcDwKqN3p6HqtTCGR/GVsPyZW92unff4UlcSh++lmqDWN3ZIq4w==

vue-highlight-words@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/vue-highlight-words/-/vue-highlight-words-1.2.0.tgz#73c2d49a46ecdb0638358b7ad749013c190ece5c"
Expand Down

0 comments on commit 539a3f2

Please sign in to comment.