-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
236 additions
and
92 deletions.
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.