refs #387 Block to login the same account of the same domain

This commit is contained in:
AkiraFukushima 2018-06-12 08:48:01 +09:00
parent 83bc88cd7d
commit a669dcf93f
4 changed files with 77 additions and 16 deletions

View File

@ -62,6 +62,17 @@ export default class Account {
})
}
searchAccounts (obj) {
return new Promise((resolve, reject) => {
this.db.find(
obj,
(err, docs) => {
if (err) return reject(err)
resolve(docs)
})
})
}
updateAccount (id, obj) {
return new Promise((resolve, reject) => {
this.db.update(
@ -179,10 +190,36 @@ export default class Account {
return this.updateAccount(account._id, json)
})
}
}
class EmptyRecordError {
constructor (message) {
this.message = message
// Confirm the access token, and check duplicate
async fetchAccount (account, accessToken) {
const client = new Mastodon(
accessToken,
account.baseURL + '/api/v1'
)
const data = await client.get('/accounts/verify_credentials')
const query = {
baseURL: account.baseURL,
username: data.username
}
const duplicates = await this.searchAccounts(query)
if (duplicates.length > 0) {
throw new DuplicateRecordError(`${data.username}@${account.baseURL} is duplicated`)
}
return data
}
}
class EmptyRecordError extends Error {
constructor (msg) {
super(msg)
this.name = 'EmptyRecordError'
}
}
class DuplicateRecordError extends Error {
constructor (msg) {
super(msg)
this.name = 'DuplicateRecordError'
}
}

View File

@ -59,7 +59,13 @@ export default class Authentication {
}
const rec = await this.db.searchAccount(search)
const accessToken = token.access_token
await this.db.updateAccount(rec._id, { accessToken: accessToken })
const data = await this.db.fetchAccount(rec, accessToken)
await this.db.updateAccount(rec._id, {
username: data.username,
accountId: data.id,
avatar: data.avatar,
accessToken: accessToken
})
return accessToken
}
// TODO: Refresh access token when expired

View File

@ -279,10 +279,6 @@ ipcMain.on('get-auth-url', (event, domain) => {
ipcMain.on('get-access-token', (event, code) => {
auth.getAccessToken(code)
.catch((err) => {
log.error(err)
event.sender.send('error-get-access-token', err)
})
.then((token) => {
accountDB.findOne({
accessToken: token
@ -292,6 +288,10 @@ ipcMain.on('get-access-token', (event, code) => {
event.sender.send('response-get-access-token', doc._id)
})
})
.catch((err) => {
log.error(err)
event.sender.send('error-get-access-token', err)
})
})
// environments

View File

@ -18,7 +18,13 @@
<el-input></el-input>
</el-form-item>
<el-form-item class="submit">
<el-button type="primary" @click="authorizeSubmit">Submit</el-button>
<el-button
type="primary"
@click="authorizeSubmit"
v-loading="submitting"
element-loading-background="rgba(0, 0, 0, 0.8)">
Submit
</el-button>
</el-form-item>
</el-form>
</el-container>
@ -32,20 +38,32 @@ export default {
return {
authorizeForm: {
code: ''
}
},
submitting: false
}
},
methods: {
authorizeSubmit () {
this.submitting = true
this.$store.dispatch('Authorize/submit', this.authorizeForm.code)
.finally(() => {
this.submitting = false
})
.then((id) => {
this.$router.push({ path: `/${id}/home` })
})
.catch(() => {
this.$message({
message: 'Could not authorize the code',
type: 'error'
})
.catch((err) => {
if (err.name === 'DuplicateRecordError') {
this.$message({
message: 'Can not login the same account of the same domain',
type: 'error'
})
} else {
this.$message({
message: 'Failed to authorize',
type: 'error'
})
}
})
},
close () {