mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-01 07:21:15 +00:00
v1 api using api token
This commit is contained in:
parent
cd331dbca5
commit
b896865865
3 changed files with 40 additions and 20 deletions
|
|
@ -12,35 +12,37 @@ module.exports = (expressApp, root, derby) ->
|
||||||
expressApp.get '/terms', (req, res) ->
|
expressApp.get '/terms', (req, res) ->
|
||||||
staticPages.render 'terms', res
|
staticPages.render 'terms', res
|
||||||
|
|
||||||
# ---------- REST API ------------
|
# ---------- Deprecated Paths ------------
|
||||||
|
|
||||||
# Deprecated API (will remove soon)
|
deprecatedMessage = 'This API is no longer supported, see https://github.com/lefnire/habitrpg/wiki/API for new protocol'
|
||||||
deprecatedMessage = 'This REST resource is no longer supported, use /users/:uid/tasks/:taskId/:direction instead.'
|
expressApp.get '/:uid/up/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
||||||
expressApp.get '/:uid/up/:score?', (req, res) ->
|
expressApp.get '/:uid/down/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
||||||
res.send(200, deprecatedMessage)
|
expressApp.post '/v1/users/:uid/tasks/:taskId/:direction', (req, res) -> res.send(500, deprecatedMessage)
|
||||||
expressApp.get '/:uid/down/:score?', (req, res) ->
|
|
||||||
res.send(200, deprecatedMessage)
|
|
||||||
|
|
||||||
# New API
|
# ---------- v1 API ------------
|
||||||
# test with `curl -X POST -H "Content-Type:application/json" localhost:3000/users/{uid}/tasks/productivity/up`
|
|
||||||
expressApp.post '/users/:uid/tasks/:taskId/:direction', (req, res) ->
|
###
|
||||||
|
v1 API. Requires user-id and api_token, task-id, direction. Test with:
|
||||||
|
curl -X POST -H "Content-Type:application/json" -d '{"api_token":"{TOKEN}"}' localhost:3000/v1/users/{UID}/tasks/productivity/up
|
||||||
|
###
|
||||||
|
expressApp.post '/v1/users/:uid/tasks/:taskId/:direction', (req, res) ->
|
||||||
{uid, taskId, direction} = req.params
|
{uid, taskId, direction} = req.params
|
||||||
{title, service, icon} = req.body
|
{api_token, title, service, icon} = req.body
|
||||||
console.log {params:req.params, body:req.body} if process.env.NODE_ENV == 'development'
|
console.log {params:req.params, body:req.body} if process.env.NODE_ENV == 'development'
|
||||||
|
|
||||||
# Send error responses for improper API call
|
# Send error responses for improper API call
|
||||||
|
return res.send(500, 'request body "api_token" required') unless api_token
|
||||||
return res.send(500, ':uid required') unless uid
|
return res.send(500, ':uid required') unless uid
|
||||||
return res.send(500, ':taskId required') unless taskId
|
return res.send(500, ':taskId required') unless taskId
|
||||||
return res.send(500, ":direction must be 'up' or 'down'") unless direction in ['up','down']
|
return res.send(500, ":direction must be 'up' or 'down'") unless direction in ['up','down']
|
||||||
|
|
||||||
model = req.getModel()
|
model = req.getModel()
|
||||||
model.fetch "users.#{uid}", (err, user) ->
|
model.fetch model.query('users').withIdAndToken(uid, api_token), (err, result) ->
|
||||||
return res.send(500, err) if err
|
return res.send(500, err) if err
|
||||||
|
user = result.at(0)
|
||||||
userObj = user.get()
|
userObj = user.get()
|
||||||
# Server crashes without this, I think some users are entering non-guid userIds and/or trying to use the API without having an account
|
if _.isEmpty(userObj)
|
||||||
unless userObj && !_.isEmpty(userObj.stats)
|
return res.send(500, "User with uid=#{uid}, token=#{api_token} not found. Make sure you're not using your username, but your User Id")
|
||||||
console.log {taskId:taskId, direction:direction, user:userObj, error: 'non-user attempted to score'} if process.env.NODE_ENV == 'development'
|
|
||||||
return res.send(500, "User #{uid} not found")
|
|
||||||
|
|
||||||
model.ref('_user', user)
|
model.ref('_user', user)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,4 +14,17 @@ module.exports = (store) ->
|
||||||
return unless @session and @session.userId # https://github.com/codeparty/racer/issues/37
|
return unless @session and @session.userId # https://github.com/codeparty/racer/issues/37
|
||||||
next = arguments[arguments.length - 1]
|
next = arguments[arguments.length - 1]
|
||||||
isServer = not @req.socket
|
isServer = not @req.socket
|
||||||
next(isServer)
|
next(isServer)
|
||||||
|
|
||||||
|
###
|
||||||
|
Get user with API token
|
||||||
|
###
|
||||||
|
store.query.expose "users", "withIdAndToken", (id, api_token) ->
|
||||||
|
@where("id").equals(id)
|
||||||
|
.where('preferences.api_token').equals(api_token)
|
||||||
|
.limit(1)
|
||||||
|
|
||||||
|
store.queryAccess "users", "withIdAndToken", (id, token, next) ->
|
||||||
|
return next(false) unless @session and @session.userId # https://github.com/codeparty/racer/issues/37
|
||||||
|
isServer = not @req.socket
|
||||||
|
next(isServer)
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,15 @@
|
||||||
<modalDialogs:>
|
<modalDialogs:>
|
||||||
{#if _loggedIn}
|
{#if _loggedIn}
|
||||||
<app:myModal modalId="settings-modal" header="Settings">
|
<app:myModal modalId="settings-modal" header="Settings">
|
||||||
<strong>User ID</strong><br/>
|
<h4>API</h4>
|
||||||
<small>Copy this ID for use in third party applications.</small>
|
<small>Copy these for use in third party applications.</small>
|
||||||
<pre class=prettyprint>{_user.id}</pre><br/>
|
<h6>User ID</h6>
|
||||||
|
<pre class=prettyprint>{_user.id}</pre>
|
||||||
|
|
||||||
|
<h6>API Token</h6>
|
||||||
|
<pre class=prettyprint>{_user.preferences.api_token}</pre>
|
||||||
|
|
||||||
|
<hr/>
|
||||||
<h4>Gender</h4>
|
<h4>Gender</h4>
|
||||||
<label class="radio">
|
<label class="radio">
|
||||||
<input type="radio" name="genderRadios" value="m" x-bind="click:setMale" checked="{equal(_user.preferences.gender,'m')}">
|
<input type="radio" name="genderRadios" value="m" x-bind="click:setMale" checked="{equal(_user.preferences.gender,'m')}">
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue