Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TAS-2061] ✨ Retrieve JWT token during signLogin #1823

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/AuthRequiredView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default {
},
mounted() {
const { login } = this.$route.query;
if (!this.walletIsMatchedSession && login) {
if (!this.getSessionWallet && login) {
this.onClickLogin();
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/components/EventModal/Collect/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ export default {
try {
switch (method) {
case 'crypto': {
if (!this.getAddress) {
if (!this.getSessionWallet) {
const isConnected = await this.connectWallet();
if (!isConnected) return;
}
Expand Down Expand Up @@ -755,7 +755,7 @@ export default {
await this.collectNFTWithStripe(classId, { memo: this.memo });
break;
case 'free': {
if (!this.getAddress) {
if (!this.getSessionWallet) {
const isConnected = await this.connectWallet();
if (!isConnected) return;
}
Expand Down
19 changes: 10 additions & 9 deletions src/components/SiteHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</Dropdown>

<ButtonV2
v-if="!loginAddress"
v-if="!getSessionWallet"
class="hidden laptop:flex"
preset="secondary"
:text="$t('header_button_connect_to_wallet')"
Expand All @@ -70,7 +70,7 @@

<Dropdown class="hidden laptop:block ml-[4px]">
<template #trigger="{ toggle }">
<div v-if="loginAddress" class="relative">
<div v-if="getSessionWallet" class="relative">
<Identity
class="cursor-pointer"
:avatar-url="walletUserAvatar"
Expand Down Expand Up @@ -136,7 +136,7 @@

{{ /* phone version */ }}
<ButtonV2
v-if="!loginAddress"
v-if="!getSessionWallet"
class="laptop:hidden"
preset="plain"
@click="handleOpenSlider"
Expand Down Expand Up @@ -171,7 +171,7 @@
@close="isShowMobileMenu = false"
>
<ButtonV2
v-if="!loginAddress"
v-if="!getSessionWallet"
class="w-full"
preset="secondary"
@click="handleConnectWallet"
Expand Down Expand Up @@ -292,6 +292,7 @@
'getUserId',
'getNotificationCount',
'shoppingCartBookProductList',
'getSessionWallet',
]),
currentLocale() {
return this.$i18n.locale;
Expand All @@ -317,10 +318,10 @@
},
},
async mounted() {
await this.restoreSession();
await this.restoreAuthSession();
},
methods: {
...mapActions(['updatePreferences', 'userLogout']),
...mapActions(['updatePreferences', 'userLogout', 'restoreAuthSession']),
handleClickGoStore() {
logTrackerEvent(this, 'site_header', 'site_header_click_store', '', 1);
},
Expand All @@ -338,7 +339,7 @@
);
await this.connectWallet();
} catch (err) {
console.error(err);

Check warning on line 342 in src/components/SiteHeader.vue

View workflow job for this annotation

GitHub Actions / CI

Unexpected console statement
this.alertPromptError(err);
}
},
Expand Down Expand Up @@ -366,7 +367,7 @@
this.$router.push(
this.localeLocation({
name: 'bookshelf',
params: { id: this.loginAddress },
params: { id: this.getSessionWallet },
query: { tab: 'collected' },
})
);
Expand All @@ -392,12 +393,12 @@
handleOpenSlider() {
this.isShowMobileMenu = true;

if (this.loginAddress) {
if (this.getSessionWallet) {
logTrackerEvent(
this,
'site_menu',
'site_menu_click_slider_menu',
this.loginAddress,
this.getSessionWallet,
1
);
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/mixins/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default {
'walletIsMatchedSession',
'walletIsLoggingIn',
'loginAddress',
'getToken',
'getSessionWallet',
'getIsRestoringSession',
]),
hasConnectedWallet() {
return !!this.getAddress && !!this.walletMethodType;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/_id/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ export default {
1
);

if (!this.getAddress) {
if (!this.getSessionWallet) {
const isConnected = await this.connectWallet();
if (!isConnected) return;
this.walletFetchLIKEBalance();
Expand Down
2 changes: 1 addition & 1 deletion src/pages/bookshelf/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export default {
'walletFollowees',
]),
wallet() {
return this.loginAddress;
return this.getSessionWallet;
},
},
watch: {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/nft/claim/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ export default {
);
},
claimingAddress() {
return this.loginAddress;
return this.getSessionWallet;
},
claimingEmail() {
return this.walletEmail || this.walletEmailUnverified;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/reader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ export default {
await this.fetchISCNMetadata();
await this.restoreSession();
// TODO: use loginAddress
if (this.getAddress) {
if (this.getSessionWallet) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a watch property in L95, should we change it too?

watch: {
// TODO: use loginAddress
async getAddress(address) {
if (address) {
await Promise.all([
this.fetchUserCollectedCount(),

await Promise.all([
this.fetchUserCollectedCount(),
this.updateNFTOwners(),
]);
if (
(!this.userCollectedCount && this.isLoginRequired) ||
(this.nftCollectorWalletAddress &&
this.nftCollectorWalletAddress !== this.getAddress)
this.nftCollectorWalletAddress !== this.getSessionWallet)
) {
this.$router.replace(
this.localeLocation({
Expand Down
2 changes: 1 addition & 1 deletion src/pages/shopping-cart/wnft.vue
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export default {
1
);
try {
if (!this.getAddress) {
if (!this.getSessionWallet) {
const isConnected = await this.connectWallet();
if (!isConnected) return;
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import staticData from './modules/staticData';
import wallet from './modules/wallet';
import nft from './modules/nft';
import cart from './modules/cart';
import bookStoreAPI from './modules/bookStoreApi';

const createStore = () =>
new Vuex.Store({
Expand Down Expand Up @@ -49,6 +50,7 @@ const createStore = () =>
wallet,
nft,
cart,
bookStoreAPI,
},
});

Expand Down
84 changes: 84 additions & 0 deletions src/store/modules/bookStoreApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint no-param-reassign: "off" */

import { postAuthenticate } from '~/util/api';
import {
saveAuthSession,
clearAuthSession,
loadAuthSession,
} from '~/util/auth';
import {
BOOKSTORE_API_SET_TOKEN,
BOOKSTORE_API_SET_SESSION_WALLET,
BOOKSTORE_API_SET_IS_LOADING,
} from '../mutation-types';

const initialState = () => ({
token: '',
sessionWallet: '',
isRestoringSession: false,
});

const mutations = {
[BOOKSTORE_API_SET_TOKEN](state, token) {
state.token = token;
},
[BOOKSTORE_API_SET_SESSION_WALLET](state, wallet) {
state.sessionWallet = wallet;
},
[BOOKSTORE_API_SET_IS_LOADING](state, isLoading) {
state.isRestoringSession = isLoading;
},
};

const actions = {
async authenticate({ commit }, { inputWallet = '', signature = {} }) {
try {
const { token } = await this.$api.$post(postAuthenticate(), signature);
if (!token) {
throw new Error('INVALID_SIGNATURE');
}
commit(BOOKSTORE_API_SET_TOKEN, token);
commit(BOOKSTORE_API_SET_SESSION_WALLET, inputWallet);
saveAuthSession({ wallet: inputWallet, token });
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
throw new Error('AUTHENTICATION_FAILED');
}
},

clearSession({ commit }) {
commit(BOOKSTORE_API_SET_TOKEN, '');
commit(BOOKSTORE_API_SET_SESSION_WALLET, '');
clearAuthSession();
},

async restoreAuthSession({ commit, dispatch }) {
try {
commit(BOOKSTORE_API_SET_IS_LOADING, true);
const session = loadAuthSession();
if (session) {
commit(BOOKSTORE_API_SET_TOKEN, session.token);
commit(BOOKSTORE_API_SET_SESSION_WALLET, session.wallet);
if (session.wallet) {
await dispatch('restoreSession');
}
}
} finally {
commit(BOOKSTORE_API_SET_IS_LOADING, false);
}
},
};

const getters = {
getToken: state => state.token,
getSessionWallet: state => state.sessionWallet,
getIsRestoringSession: state => state.isRestoringSession,
};

export default {
actions,
getters,
state: initialState,
mutations,
};
12 changes: 11 additions & 1 deletion src/store/modules/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ const actions = {
commit(WALLET_SET_SIGNER, null);
commit(WALLET_SET_CONNECTOR, null);
commit(WALLET_SET_LIKERINFO, null);
dispatch('clearSession');
await dispatch('walletLogout');
},

Expand Down Expand Up @@ -943,11 +944,18 @@ const actions = {
try {
commit(WALLET_SET_IS_LOGGING_IN, true);
const { signer, methodType } = state;
const data = await signLoginMessage(signer, address);
const data = await signLoginMessage(signer, address, 'authorize', [
'profile',
'read:nftbook',
'write:nftbook',
'read:nftcollection',
'write:nftcollection',
]);
const result = await this.$api.$post(postUserV2Login(), {
loginMethod: methodType,
...data,
});
await dispatch('authenticate', { inputWallet: address, signature: data });
await setLoggerUser(this, {
wallet: address,
method: methodType,
Expand All @@ -956,6 +964,8 @@ const actions = {
await dispatch('walletFetchSessionUserData');
} catch (error) {
commit(WALLET_SET_USER_INFO, null);
dispatch('disconnectWallet');
dispatch('clearSession');
if (error.message === 'Request rejected') {
// User rejected login request
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/store/mutation-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ export const WALLET_SET_TOTAL_RESALES = 'WALLET_SET_TOTAL_RESALES';
export const WALLET_SET_SALES_DETAILS = 'WALLET_SET_SALES_DETAILS';
export const WALLET_SET_ROYALTY_DETAILS = 'WALLET_SET_ROYALTY_DETAILS';
export const WALLET_SET_RESALE_DETAILS = 'WALLET_SET_RESALE_DETAILS';
export const BOOKSTORE_API_SET_TOKEN = 'BOOKSTORE_API_SET_TOKEN';
export const BOOKSTORE_API_SET_SESSION_WALLET =
'BOOKSTORE_API_SET_SESSION_WALLET';
export const BOOKSTORE_API_SET_IS_LOADING = 'BOOKSTORE_API_SET_IS_LOADING';
2 changes: 2 additions & 0 deletions src/util/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,5 @@ export const fetchGutenbergCsv = () =>

export const postSubstackSubscribe = () =>
`https://substackapi.com/api/subscribe`;

export const postAuthenticate = () => `${LIKECOIN_API_BASE}/wallet/authorize`;
38 changes: 38 additions & 0 deletions src/util/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const AUTH_SESSION_KEY = 'likecoin_nft_book_press_token';

export function loadAuthSession() {
try {
if (window.localStorage) {
const data = window.localStorage.getItem(AUTH_SESSION_KEY);
if (data) {
const { wallet, token } = JSON.parse(data);
return {
wallet,
token,
};
}
}
} catch {}

return null;
}

export function saveAuthSession(session) {
try {
if (!window.localStorage) {
return;
}

window.localStorage.setItem(AUTH_SESSION_KEY, JSON.stringify(session));
} catch {}
}

export function clearAuthSession() {
try {
if (!window.localStorage) {
return;
}

window.localStorage.removeItem(AUTH_SESSION_KEY);
} catch {}
}
Loading
Loading