diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue
index 3a4f750..8d401d1 100644
--- a/src/pages/Settings.vue
+++ b/src/pages/Settings.vue
@@ -16,17 +16,17 @@
- MyAnimeList
- ({{malAuth.username}})
+ AniList
+ ({{alAuth.name}})
+
+
+ Authenticating...
+
+
-
-
+
@@ -50,12 +50,37 @@
selectedLocale: LOCALE()
}
},
+ async created () {
+ if (this.$route.hash === '') return;
+ const {access_token} = this.$route.hash
+ .replace(/^#/, '')
+ .split('&')
+ .map((el) => el.split('='))
+ .reduce((obj, val) => {
+ obj[val[0]] = val[1]
+ return obj
+ }, {});
+
+ if (!access_token) return;
+
+ await this.$store.dispatch('authenticateAniList', {
+ token: access_token
+ })
+
+ this.$router.replace('/settings')
+ },
computed: {
malAuth () {
return this.$store.state.malAuth
},
+ alAuth () {
+ return this.$store.state.alAuth
+ },
locales () {
return this.$store.state.locales
+ },
+ anilistUrl () {
+ return `https://anilist.co/api/v2/oauth/authorize?client_id=${process.env.NODE_ENV === 'production' ? '1234' : '1235'}&response_type=token`
}
},
watch: {
@@ -88,6 +113,9 @@
},
logoutMal () {
this.$store.commit('REMOVE_MAL_AUTH')
+ },
+ logoutAnilist () {
+ this.$store.commit('REMOVE_AL_AUTH')
}
}
}
diff --git a/src/store/index.js b/src/store/index.js
index a0d57cf..0353dad 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -19,6 +19,14 @@ function handleError (err, reject) {
}
}
+const ANILIST_AUTH_QUERY = `
+ query {
+ Viewer {
+ name
+ }
+ }
+`
+
const store = new Vuex.Store({
state: {
auth: localStorage.getItem('auth') ? (
@@ -31,6 +39,11 @@ const store = new Vuex.Store({
) : (
{}
),
+ alAuth: localStorage.getItem('alAuth') ? (
+ JSON.parse(localStorage.getItem('alAuth'))
+ ) : (
+ {}
+ ),
locales: [],
series: {},
seriesCollections: {},
@@ -154,6 +167,35 @@ const store = new Vuex.Store({
})
},
+ authenticateAniList ({commit}, {token}) {
+ return new Promise(async (resolve, reject) => {
+ try {
+ const {data: {data, errors}} = await axios({
+ method: 'post',
+ url: 'https://graphql.anilist.co',
+ headers: {
+ Authorization: `Bearer ${token}`
+ },
+ data: {
+ query: ANILIST_AUTH_QUERY
+ }
+ })
+ if (errors && errors.length > 0) {
+ reject(new Error(errors[0]))
+ } else {
+ console.log(data)
+ commit('UPDATE_AL', {
+ token,
+ name: data.Viewer.name
+ })
+ resolve()
+ }
+ } catch (err) {
+ reject(err)
+ }
+ })
+ },
+
getQueueInfo ({commit, state}, force) {
const params = {
session_id: state.auth.session_id,
@@ -527,6 +569,17 @@ const store = new Vuex.Store({
Vue.set(state, 'malAuth', {})
},
+ UPDATE_AL (state, obj) {
+ const updated = Object.assign({}, state.alAuth, obj)
+ localStorage.setItem('alAuth', JSON.stringify(updated))
+ Vue.set(state, 'alAuth', updated)
+ },
+
+ REMOVE_AL_AUTH (state) {
+ localStorage.removeItem('alAuth')
+ Vue.set(state, 'alAuth', {})
+ },
+
SET_SEARCH_IDS (state, arr) {
state.searchIds = arr
},