Migrate oauth store to pinia
This commit is contained in:
parent
a5cc7351ec
commit
216d318bb5
12 changed files with 663 additions and 145 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||
|
||||
const PASSWORD_STRATEGY = 'password'
|
||||
const TOKEN_STRATEGY = 'token'
|
||||
|
||||
|
|
@ -68,8 +70,8 @@ const mutations = {
|
|||
// actions
|
||||
const actions = {
|
||||
|
||||
async login ({ state, dispatch, commit }, { access_token: accessToken }) {
|
||||
commit('setToken', accessToken, { root: true })
|
||||
async login ({ state, dispatch }, { access_token: accessToken }) {
|
||||
useOAuthStore().setToken(accessToken)
|
||||
await dispatch('loginUser', accessToken, { root: true })
|
||||
resetState(state)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import config from './config.js'
|
|||
import profileConfig from './profileConfig.js'
|
||||
import serverSideStorage from './serverSideStorage.js'
|
||||
import adminSettings from './adminSettings.js'
|
||||
import oauth from './oauth.js'
|
||||
import authFlow from './auth_flow.js'
|
||||
import oauthTokens from './oauth_tokens.js'
|
||||
import drafts from './drafts.js'
|
||||
|
|
@ -23,7 +22,6 @@ export default {
|
|||
profileConfig,
|
||||
serverSideStorage,
|
||||
adminSettings,
|
||||
oauth,
|
||||
authFlow,
|
||||
oauthTokens,
|
||||
drafts,
|
||||
|
|
|
|||
|
|
@ -1,151 +0,0 @@
|
|||
import { createApp, getClientToken, verifyAppToken } from 'src/services/new_api/oauth.js'
|
||||
|
||||
// status codes about verifyAppToken (GET /api/v1/apps/verify_credentials)
|
||||
const isAppTokenRejected = error => (
|
||||
// Pleroma API docs say it returns 422 when unauthorized
|
||||
error.statusCode === 422 ||
|
||||
// but it actually returns 400 (as of 2.9.0)
|
||||
// NOTE: don't try to match against the error message, it is translatable
|
||||
error.statusCode === 400 ||
|
||||
// and Mastodon docs say it returns 401
|
||||
error.statusCode === 401
|
||||
)
|
||||
|
||||
// status codes about getAppToken (GET /oauth/token)
|
||||
const isClientDataRejected = error => (
|
||||
// Mastodon docs say it returns 401
|
||||
error.statusCode === 401 ||
|
||||
// but Pleroma actually returns 400 (as of 2.9.0)
|
||||
// NOTE: don't try to match against the error message, it is translatable
|
||||
error.statusCode === 400
|
||||
)
|
||||
|
||||
const oauth = {
|
||||
state: () => ({
|
||||
clientId: false,
|
||||
clientSecret: false,
|
||||
/* App token is authentication for app without any user, used mostly for
|
||||
* MastoAPI's registration of new users, stored so that we can fall back to
|
||||
* it on logout
|
||||
*/
|
||||
appToken: false,
|
||||
/* User token is authentication for app with user, this is for every calls
|
||||
* that need authorized user to be successful (i.e. posting, liking etc)
|
||||
*/
|
||||
userToken: false
|
||||
}),
|
||||
mutations: {
|
||||
setClientData (state, { clientId, clientSecret }) {
|
||||
state.clientId = clientId
|
||||
state.clientSecret = clientSecret
|
||||
},
|
||||
setAppToken (state, token) {
|
||||
state.appToken = token
|
||||
},
|
||||
setToken (state, token) {
|
||||
state.userToken = token
|
||||
},
|
||||
clearToken (state) {
|
||||
state.userToken = false
|
||||
// state.token is userToken with older name, coming from persistent state
|
||||
// let's clear it as well, since it is being used as a fallback of state.userToken
|
||||
delete state.token
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
getToken: state => () => {
|
||||
// state.token is userToken with older name, coming from persistent state
|
||||
// added here for smoother transition, otherwise user will be logged out
|
||||
return state.userToken || state.token || state.appToken
|
||||
},
|
||||
getUserToken: state => () => {
|
||||
// state.token is userToken with older name, coming from persistent state
|
||||
// added here for smoother transition, otherwise user will be logged out
|
||||
return state.userToken || state.token
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async createApp ({ rootState, commit }) {
|
||||
const instance = rootState.instance.server
|
||||
const app = await createApp(instance)
|
||||
commit('setClientData', app)
|
||||
return app
|
||||
},
|
||||
/// Use this if you want to get the client id and secret but are not interested
|
||||
/// in whether they are valid.
|
||||
/// @return {{ clientId: string, clientSecret: string }} An object representing the app
|
||||
ensureApp ({ state, dispatch }) {
|
||||
if (state.clientId && state.clientSecret) {
|
||||
return {
|
||||
clientId: state.clientId,
|
||||
clientSecret: state.clientSecret
|
||||
}
|
||||
} else {
|
||||
return dispatch('createApp')
|
||||
}
|
||||
},
|
||||
async getAppToken ({ state, rootState, commit }) {
|
||||
const res = await getClientToken({
|
||||
clientId: state.clientId,
|
||||
clientSecret: state.clientSecret,
|
||||
instance: rootState.instance.server
|
||||
})
|
||||
commit('setAppToken', res.access_token)
|
||||
return res.access_token
|
||||
},
|
||||
/// Use this if you want to ensure the app is still valid to use.
|
||||
/// @return {string} The access token to the app (not attached to any user)
|
||||
async ensureAppToken ({ state, rootState, dispatch, commit }) {
|
||||
if (state.appToken) {
|
||||
try {
|
||||
await verifyAppToken({
|
||||
instance: rootState.instance.server,
|
||||
appToken: state.appToken
|
||||
})
|
||||
return state.appToken
|
||||
} catch (e) {
|
||||
if (!isAppTokenRejected(e)) {
|
||||
// The server did not reject our token, but we encountered other problems. Maybe the server is down.
|
||||
throw e
|
||||
} else {
|
||||
// The app token is rejected, so it is no longer useful.
|
||||
commit('setAppToken', false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// appToken is not available, or is rejected: try to get a new one.
|
||||
// First, make sure the client id and client secret are filled.
|
||||
try {
|
||||
await dispatch('ensureApp')
|
||||
} catch (e) {
|
||||
console.error('Cannot create app', e)
|
||||
throw e
|
||||
}
|
||||
|
||||
// Note that at this step, the client id and secret may be invalid
|
||||
// (because the backend may have already deleted the app due to no user login)
|
||||
try {
|
||||
return await dispatch('getAppToken')
|
||||
} catch (e) {
|
||||
if (!isClientDataRejected(e)) {
|
||||
// Non-credentials problem, fail fast
|
||||
console.error('Cannot get app token', e)
|
||||
throw e
|
||||
} else {
|
||||
// the client id and secret are invalid, so we should clear them
|
||||
// and re-create our app
|
||||
commit('setClientData', {
|
||||
clientId: false,
|
||||
clientSecret: false
|
||||
})
|
||||
await dispatch('createApp')
|
||||
// try once again to get the token
|
||||
return await dispatch('getAppToken')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default oauth
|
||||
|
|
@ -5,6 +5,7 @@ import oauthApi from '../services/new_api/oauth.js'
|
|||
import { compact, map, each, mergeWith, last, concat, uniq, isArray } from 'lodash'
|
||||
import { registerPushNotifications, unregisterPushNotifications } from '../services/sw/sw.js'
|
||||
import { useInterfaceStore } from 'src/stores/interface.js'
|
||||
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||
|
||||
// TODO: Unify with mergeOrAdd in statuses.js
|
||||
export const mergeOrAdd = (arr, obj, item) => {
|
||||
|
|
@ -526,17 +527,18 @@ const users = {
|
|||
})
|
||||
},
|
||||
async signUp (store, userInfo) {
|
||||
const oauthStore = useOAuthStore()
|
||||
store.commit('signUpPending')
|
||||
|
||||
try {
|
||||
const token = await store.dispatch('ensureAppToken')
|
||||
const token = await oauthStore.ensureAppToken()
|
||||
const data = await apiService.register(
|
||||
{ credentials: token, params: { ...userInfo } }
|
||||
)
|
||||
|
||||
if (data.access_token) {
|
||||
store.commit('signUpSuccess')
|
||||
store.commit('setToken', data.access_token)
|
||||
oauthStore.setToken(data.access_token)
|
||||
store.dispatch('loginUser', data.access_token)
|
||||
return 'ok'
|
||||
} else { // Request succeeded, but user cannot login yet.
|
||||
|
|
@ -554,21 +556,16 @@ const users = {
|
|||
},
|
||||
|
||||
logout (store) {
|
||||
const { oauth, instance } = store.rootState
|
||||
|
||||
const data = {
|
||||
...oauth,
|
||||
commit: store.commit,
|
||||
instance: instance.server
|
||||
}
|
||||
const oauth = useOAuthStore()
|
||||
const { instance } = store.rootState
|
||||
|
||||
// NOTE: No need to verify the app still exists, because if it doesn't,
|
||||
// the token will be invalid too
|
||||
return store.dispatch('ensureApp')
|
||||
return oauth.ensureApp()
|
||||
.then((app) => {
|
||||
const params = {
|
||||
app,
|
||||
instance: data.instance,
|
||||
instance: instance.server,
|
||||
token: oauth.userToken
|
||||
}
|
||||
|
||||
|
|
@ -577,9 +574,9 @@ const users = {
|
|||
.then(() => {
|
||||
store.commit('clearCurrentUser')
|
||||
store.dispatch('disconnectFromSocket')
|
||||
store.commit('clearToken')
|
||||
oauth.clearToken()
|
||||
store.dispatch('stopFetchingTimeline', 'friends')
|
||||
store.commit('setBackendInteractor', backendInteractorService(store.getters.getToken()))
|
||||
store.commit('setBackendInteractor', backendInteractorService(oauth.getToken))
|
||||
store.dispatch('stopFetchingNotifications')
|
||||
store.dispatch('stopFetchingLists')
|
||||
store.dispatch('stopFetchingBookmarkFolders')
|
||||
|
|
@ -674,7 +671,7 @@ const users = {
|
|||
|
||||
// remove authentication token on client/authentication errors
|
||||
if ([400, 401, 403, 422].includes(response.status)) {
|
||||
commit('clearToken')
|
||||
useOAuthStore().clearToken()
|
||||
}
|
||||
|
||||
if (response.status === 401) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue