Comments (8)
Found a workaround using https://github.com/tj/co
getToken(code) {
let oauth2Client = this.oauth2Client;
oauth2Client.getToken(code, function (err, tokens) {
if (!err) {
co(function*() {
oauth2Client.setCredentials(tokens);
yield Database.from('admin').insert({'access_token': tokens.access_token});
return tokens.access_token;
}).then(function (token) {
console.info(`Token saved to database (${token}).`);
}, function (err) {
console.error(err.stack);
});
}
});
}
from core.
how are you calling getToken in your controller ?
from core.
This is the controller:
'use strict';
const GoogleCalendarService = use('App/Services/GoogleCalendarService');
class CalendarController {
constructor() {
this.calendarService = new GoogleCalendarService();
}
* authorize(request, response) {
let params = request.get();
this.calendarService.getToken(params.code);
const view = yield response.view('index.html');
response.ok(view);
}
* index(request, response) {
const view = yield response.view('index.html', {url: this.calendarService.generateAuthenticationUri()});
response.ok(view);
}
}
module.exports = CalendarController;
And here the appropriate route:
Route.get('/auth', 'CalendarController.authorize');
from core.
You will have to use the yield
while using getToken
since it is a generator method
yield this.calendarService.getToken(params.code);
from core.
I tried it already, but it did not work out. In this case i got the parse error:
yield Database.from('admin').insert({'access_token': tokens.access_token}); ^ ParseError: Unexpected token
getToken(code) {
let oauth2Client = this.oauth2Client;
oauth2Client.getToken(code, function (err, tokens) {
if (!err) {
oauth2Client.setCredentials(tokens);
yield Database.from('admin').insert({'access_token': tokens.access_token});
}
});
}
* authorize(request, response) {
let params = request.get();
yield this.calendarService.getToken(params.code);
const view = yield response.view('index.html');
response.ok(view);
}
from core.
Ohh got it, coz you are trying to run it inside the callback of oauth2Client.getToken
and generators needs to be at the first level of * method
.
I am not a big fan of callbacks and if you are interested you can make use of https://github.com/tj/node-thunkify. It will convert your callbacks to generator friendly methods. Something like this
const oauthToken = thunkify(oauth2Client.getToken)
try {
const tokens = yield oauthToken(code)
oauth2Client.setCredentials(tokens);
console.log(`Save ${tokens.access_token} to database`);
yield Database.from('admin').insert({'access_token': tokens.access_token});
}
catch (e) {
console.log(e)
}
from core.
I'll try that... :) Thank you for your fast and helpful support bro! 👍
from core.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from core.
Related Issues (20)
- make:controller --api & --resource descriptions are swapped HOT 2
- Cannot find module '#exceptions/handler' or its corresponding type declarations HOT 2
- `build` command fails when paths are passed via `--assets-args` HOT 1
- Getting error in Singleton import
- Problem Generating Routes with Router.builder in Imported Routes HOT 1
- unable to determine transport target for "pino-pretty"
- i cant use String for accessTokens id HOT 2
- Healthcheck HOT 2
- Adonis 6 migration with 'uuid' as primary key HOT 3
- The data obtained by the get parameter is incorrect
- Pluralization is broken HOT 6
- Issue when inertia build with example code HOT 1
- Error install dependencies new project core 6.6.0 HOT 2
- npm init adonis-ts-app@latest app-name error if eslint paramenter is set to true HOT 3
- Adonis js doesnt look filename with extension, build ERROR HOT 1
- Functionnal testing with auth HOT 7
- Incomplete project structure HOT 1
- guardFactory is not a function HOT 1
- Bullmq Worker not reusing configured active redis connection
- ENOSPC: System limit for number of file watchers reached, from `yarn dev` HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.