Final gist integration + clean up

This commit is contained in:
Liyas Thomas
2020-12-08 17:50:32 +05:30
parent b1587950c6
commit ba08883623
6 changed files with 59 additions and 29 deletions

View File

@@ -18,16 +18,21 @@
</button> </button>
</div> </div>
<div <div
v-tooltip="{ v-tooltip.bottom="{
content: content: !fb.currentUser
this.$store.state.postwoman.providerInfo.providerId !== 'github.com' ? $t('login_with_github_to') + $t('create_secret_gist')
? $t('login_with_github_to') + $t('create_secret_gist') : fb.currentUser.provider !== 'github.com'
: null, ? $t('login_with_github_to') + $t('create_secret_gist')
: null,
}" }"
> >
<button <button
:disabled=" :disabled="
this.$store.state.postwoman.providerInfo.providerId !== 'github.com' !fb.currentUser
? true
: fb.currentUser.provider !== 'github.com'
? true
: false
" "
class="icon" class="icon"
@click="createCollectionGist" @click="createCollectionGist"
@@ -139,7 +144,7 @@ export default {
}, },
{ {
headers: { headers: {
Authorization: `token ${this.$store.state.postwoman.providerInfo.accessToken}`, Authorization: `token ${fb.currentUser.accessToken}`,
Accept: "application/vnd.github.v3+json", Accept: "application/vnd.github.v3+json",
}, },
} }

View File

@@ -18,16 +18,21 @@
</button> </button>
</div> </div>
<div <div
v-tooltip="{ v-tooltip.bottom="{
content: content: !fb.currentUser
this.$store.state.postwoman.providerInfo.providerId !== 'github.com' ? $t('login_with_github_to') + $t('create_secret_gist')
? $t('login_with_github_to') + $t('create_secret_gist') : fb.currentUser.provider !== 'github.com'
: null, ? $t('login_with_github_to') + $t('create_secret_gist')
: null,
}" }"
> >
<button <button
:disabled=" :disabled="
this.$store.state.postwoman.providerInfo.providerId !== 'github.com' !fb.currentUser
? true
: fb.currentUser.provider !== 'github.com'
? true
: false
" "
class="icon" class="icon"
@click="createEnvironmentGist" @click="createEnvironmentGist"
@@ -139,7 +144,7 @@ export default {
}, },
{ {
headers: { headers: {
Authorization: `token ${this.$store.state.postwoman.providerInfo.accessToken}`, Authorization: `token ${fb.currentUser.accessToken}`,
Accept: "application/vnd.github.v3+json", Accept: "application/vnd.github.v3+json",
}, },
} }

View File

@@ -62,6 +62,7 @@ export default {
this.showLoginSuccess() this.showLoginSuccess()
} catch (err) { } catch (err) {
console.log(err)
// An error happened. // An error happened.
if (err.code === "auth/account-exists-with-different-credential") { if (err.code === "auth/account-exists-with-different-credential") {
// Step 2. // Step 2.
@@ -80,22 +81,23 @@ export default {
// Asks the user their password. // Asks the user their password.
// In real scenario, you should handle this asynchronously. // In real scenario, you should handle this asynchronously.
const password = promptUserForPassword() // TODO: implement promptUserForPassword. const password = promptUserForPassword() // TODO: implement promptUserForPassword.
const user = await fb.signInWithEmailAndPassword(email, password)
const user = await fb.signInWithEmailAndPassword(email, password)
await user.linkWithCredential(pendingCred) await user.linkWithCredential(pendingCred)
this.showLoginSuccess() this.showLoginSuccess()
return return
} }
this.$toast.info(`${this.$t("login_with")}`, { this.$toast.info(`${this.$t("account_exists")}`, {
icon: "vpn_key", icon: "vpn_key",
duration: null, duration: null,
closeOnSwipe: false, closeOnSwipe: false,
action: { action: {
text: this.$t("yes"), text: this.$t("yes"),
onClick: async (e, toastObject) => { onClick: async (e, toastObject) => {
const user = await fb.signInWithGithub() const { user } = await fb.signInWithGithub()
await user.linkAndRetrieveDataWithCredential(pendingCred) await user.linkAndRetrieveDataWithCredential(pendingCred)
this.showLoginSuccess() this.showLoginSuccess()
@@ -111,10 +113,7 @@ export default {
try { try {
const { credential, additionalUserInfo } = await fb.signInUserWithGithub() const { credential, additionalUserInfo } = await fb.signInUserWithGithub()
this.$store.commit("postwoman/setProviderInfo", { fb.setProviderInfo(credential.providerId, credential.accessToken)
providerId: "github.com",
accessToken: credential.accessToken,
})
if (additionalUserInfo.isNewUser) { if (additionalUserInfo.isNewUser) {
this.$toast.info(`${this.$t("turn_on")} ${this.$t("sync")}`, { this.$toast.info(`${this.$t("turn_on")} ${this.$t("sync")}`, {
@@ -136,6 +135,7 @@ export default {
this.showLoginSuccess() this.showLoginSuccess()
} catch (err) { } catch (err) {
console.log(err)
// An error happened. // An error happened.
if (err.code === "auth/account-exists-with-different-credential") { if (err.code === "auth/account-exists-with-different-credential") {
// Step 2. // Step 2.
@@ -163,7 +163,7 @@ export default {
return return
} }
this.$toast.info(`${this.$t("login_with")}`, { this.$toast.info(`${this.$t("account_exists")}`, {
icon: "vpn_key", icon: "vpn_key",
duration: null, duration: null,
closeOnSwipe: false, closeOnSwipe: false,

View File

@@ -36,6 +36,7 @@ export class FirebaseInstance {
this.app.auth().onAuthStateChanged((user) => { this.app.auth().onAuthStateChanged((user) => {
if (user) { if (user) {
this.currentUser = user this.currentUser = user
this.currentUser.providerData.forEach((profile) => { this.currentUser.providerData.forEach((profile) => {
let us = { let us = {
updatedOn: new Date(), updatedOn: new Date(),
@@ -47,10 +48,15 @@ export class FirebaseInstance {
} }
this.usersCollection this.usersCollection
.doc(this.currentUser.uid) .doc(this.currentUser.uid)
.set(us) .set(us, { merge: true })
.catch((e) => console.error("error updating", us, e)) .catch((e) => console.error("error updating", us, e))
}) })
this.usersCollection.doc(this.currentUser.uid).onSnapshot((doc) => {
this.currentUser.provider = doc.data().provider
this.currentUser.accessToken = doc.data().accessToken
})
this.usersCollection this.usersCollection
.doc(this.currentUser.uid) .doc(this.currentUser.uid)
.collection("feeds") .collection("feeds")
@@ -288,6 +294,24 @@ export class FirebaseInstance {
throw e throw e
} }
} }
async setProviderInfo(id, token) {
const us = {
updatedOn: new Date(),
provider: id,
accessToken: token,
}
try {
await this.usersCollection
.doc(this.currentUser.uid)
.update(us)
.catch((e) => console.error("error updating", us, e))
} catch (e) {
console.error("error updating", ev, e)
throw e
}
}
} }
export const fb = new FirebaseInstance(firebase.initializeApp(firebaseConfig), authProviders) export const fb = new FirebaseInstance(firebase.initializeApp(firebaseConfig), authProviders)

View File

@@ -299,5 +299,6 @@
"create_secret_gist": "Create secret Gist", "create_secret_gist": "Create secret Gist",
"gist_created": "Gist created", "gist_created": "Gist created",
"import_from_gist": "Import from Gist", "import_from_gist": "Import from Gist",
"enter_gist_url": "Enter Gist URL" "enter_gist_url": "Enter Gist URL",
"account_exists": "Account exists with different credential - Login to link both accounts"
} }

View File

@@ -64,7 +64,6 @@ export const state = () => ({
editingEnvironment: {}, editingEnvironment: {},
selectedRequest: {}, selectedRequest: {},
editingRequest: {}, editingRequest: {},
providerInfo: {},
}) })
export const mutations = { export const mutations = {
@@ -336,10 +335,6 @@ export const mutations = {
} }
} }
}, },
setProviderInfo(state, value) {
state.providerInfo = value
},
} }
function testValue(myValue) { function testValue(myValue) {