-
Notifications
You must be signed in to change notification settings - Fork 13
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
AuroraHuang22
wants to merge
5
commits into
likecoin:develop
Choose a base branch
from
AuroraHuang22:feature/jwt-token
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c3f65f
👔 Add authorize permission to sign message
AuroraHuang22 1438c20
✨ Add book-api-store to retrieve token
AuroraHuang22 a98997f
✨Retrieve JWT token during signLogin
AuroraHuang22 5dad640
🚸 Improve login logout waiting time
AuroraHuang22 21ac94b
👔 Use session wallet to determine connected user
AuroraHuang22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
liker-land/src/pages/reader.vue
Lines 93 to 98 in 21ac94b