Last active
June 1, 2016 11:17
-
-
Save nsipplswezey/80c6fb8adcf4385b702c709a34943d6d to your computer and use it in GitHub Desktop.
Users, Access Tokens and Authenticated POST requests to create Tweets!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install -g nodal | |
nodal new users-access-tokens | |
cd users-access-tokens | |
nodal g:model --user | |
nodal g:controller --for Users | |
nodal g:model --access_token | |
nodal g:controller --for Access_tokens | |
nodal g:model Tweet user_id:int body:string | |
nodal g:controller --for Tweets | |
nodal db:create | |
nodal db:prepare | |
nodal db:migrate | |
nodal s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./app/controllers/access_tokens_controller.js | |
module.exports = (function() { | |
'use strict'; | |
const Nodal = require('nodal'); | |
const AccessToken = Nodal.require('app/models/access_token.js'); | |
class AccessTokensController extends Nodal.Controller { | |
//We only need to implement create | |
create() { | |
AccessToken.login(this.params, (err, accessToken) => { | |
this.respond(err || accessToken); | |
}); | |
} | |
} | |
return AccessTokensController; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//app/controllers/auth_controller.js | |
module.exports = (function() { | |
'use strict'; | |
const Nodal = require('nodal'); | |
//Import access token model | |
const AccessToken = Nodal.require('app/models/access_token.js'); | |
class AuthController extends Nodal.Controller { | |
authorize(callback) { | |
this.setHeader('Cache-Control', 'no-store'); | |
this.setHeader('Pragma', 'no-cache'); | |
//implement .verify method in auth controller authorize | |
AccessToken.verify(this.params, callback); | |
} | |
} | |
return AuthController; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//app/controllers/tweets_controller.js | |
//import AuthController | |
const AuthController = Nodal.require('app/controllers/auth_controller.js'); | |
//extend TweetsController from AuthController | |
class TweetsController extends AuthController { | |
index(){...} | |
show(){...} | |
//implement the authorize method from the AuthController | |
create(){ | |
this.authorize((err, accessToken, user) => { | |
if (err) { | |
return this.respond(err); | |
} | |
Tweet.create(this.params.body, (err, model) => { | |
this.respond(err || model); | |
}); | |
}); | |
} | |
update(){...} | |
destroy(){...} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If you haven't yet, consider quickly building https://medium.com/@nsipplswezey/tl-dr-nodal-users-and-access-tokens-25f8f5ee6dbe#.ebt3m5z57 | |
POST localhost:3000/users username:nsipplswezey email:nsipplswezey@gmail.com password:password | |
{ | |
"meta": { | |
"total": 1, | |
"count": 1, | |
"offset": 0, | |
"error": null | |
}, | |
"data": [ | |
{ | |
"id": 1, | |
"email": "nsipplswezey@gmail.com", | |
"password": "$2a$10$O9rNtf7nR.7dbXpvDle9N.9mdknObIP.gpHAkN0iouAkL3wka1.Vy", | |
"username": "nsipplswezey", | |
"created_at": "2016-05-29T02:35:08.394Z", | |
"updated_at": "2016-05-29T02:35:08.512Z" | |
} | |
] | |
} | |
POST localhost:3000/access_tokens username:nsipplswezey grant_type:password password:password | |
{ | |
"meta": { | |
"total": 1, | |
"count": 1, | |
"offset": 0, | |
"error": null | |
}, | |
"data": [ | |
{ | |
"id": 1, | |
"user_id": 1, | |
"access_token": "887e97143e745a45252f4c07aa7e5960", | |
"token_type": "bearer", | |
"expires_at": "2016-06-28T02:38:32.709Z", | |
"ip_address": null, | |
"created_at": "2016-05-29T02:38:32.709Z", | |
"updated_at": "2016-05-29T02:38:32.711Z" | |
} | |
] | |
} | |
POST localhost:3000/tweets user_id:1 body:Hello I don't have a token | |
{ | |
"meta": { | |
"total": 0, | |
"count": 0, | |
"offset": 0, | |
"error": { | |
"message": "Your access token is invalid." | |
} | |
}, | |
"data": [] | |
} | |
POST localhost:3000/tweets?access_token=887e97143e745a45252f4c07aa7e5960 user_id:1 body:Hello I have a token | |
{ | |
"meta": { | |
"total": 1, | |
"count": 1, | |
"offset": 0, | |
"error": null | |
}, | |
"data": [ | |
{ | |
"id": 1, | |
"user_id": 1, | |
"body": "Hello I have a token", | |
"created_at": "2016-06-01T03:35:40.015Z", | |
"updated_at": "2016-06-01T03:35:40.019Z" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment