Move login to oauth.
This commit is contained in:
parent
7f0e140a4f
commit
9af204b293
12 changed files with 1332 additions and 243 deletions
64
src/services/new_api/oauth.js
Normal file
64
src/services/new_api/oauth.js
Normal file
|
@ -0,0 +1,64 @@
|
|||
import {reduce} from 'lodash'
|
||||
|
||||
const getOrCreateApp = ({oauth, instance}) => {
|
||||
const url = `${instance}/api/v1/apps`
|
||||
const form = new window.FormData()
|
||||
|
||||
form.append('client_name', `PleromaFE_${Math.random()}`)
|
||||
form.append('redirect_uris', `${window.location.origin}/oauth-callback`)
|
||||
form.append('scopes', 'read write follow')
|
||||
|
||||
return window.fetch(url, {
|
||||
method: 'POST',
|
||||
body: form
|
||||
}).then((data) => data.json())
|
||||
}
|
||||
const login = (args) => {
|
||||
getOrCreateApp(args).then((app) => {
|
||||
args.commit('setClientData', app)
|
||||
|
||||
const data = {
|
||||
response_type: 'code',
|
||||
client_id: app.client_id,
|
||||
redirect_uri: app.redirect_uri,
|
||||
scope: 'read write follow'
|
||||
}
|
||||
|
||||
const dataString = reduce(data, (acc, v, k) => {
|
||||
const encoded = `${k}=${encodeURIComponent(v)}`
|
||||
if (!acc) {
|
||||
return encoded
|
||||
} else {
|
||||
return `${acc}&${encoded}`
|
||||
}
|
||||
}, false)
|
||||
|
||||
// Do the redirect...
|
||||
const url = `${args.instance}/oauth/authorize?${dataString}`
|
||||
|
||||
window.location.href = url
|
||||
})
|
||||
}
|
||||
|
||||
const getToken = ({app, instance, code}) => {
|
||||
const url = `${instance}/oauth/token`
|
||||
const form = new window.FormData()
|
||||
|
||||
form.append('client_id', app.client_id)
|
||||
form.append('client_secret', app.client_secret)
|
||||
form.append('grant_type', 'authorization_code')
|
||||
form.append('code', code)
|
||||
form.append('redirect_uri', `${window.location.origin}/oauth-callback`)
|
||||
|
||||
return window.fetch(url, {
|
||||
method: 'POST',
|
||||
body: form
|
||||
}).then((data) => data.json())
|
||||
}
|
||||
|
||||
const oauth = {
|
||||
login,
|
||||
getToken
|
||||
}
|
||||
|
||||
export default oauth
|
Loading…
Add table
Add a link
Reference in a new issue