mongodb - Combining koa-passport with koa-router (getting user data) -
i have created login able login user , store user if new in database.
the user redirected /
, checked if authenticated or not, see below (app.js):
.get('/', function* () { if (this.isauthenticated()) { yield this.render('homesecure', {}); // <-- need user data here } else { yield this.render('homepublic', {}); }
as commented in code, send user object of logged in. have no idea how hold of id of person logged in documentation koa in general not complete of express.
i using koa-generic-session-mongo
handle sessions. here googlestrategy (auth.js):
var user = null; // ... var googlestrategy = require('passport-google').strategy; passport.use(new googlestrategy({ returnurl: 'http://localhost:' + (process.env.port || 3000) + '/auth/google/callback', realm: 'http://localhost:' + (process.env.port || 3000) }, function (identifier, profile, done) { var emails = new array(); (var = 0; < profile.emails.length; i++) { emails.push(profile.emails[i].value); } co(function* () { yield users.findone({ emails: emails }); }); if (user === null) { // first time signin, create account co(function* () { user = { id: 1, name: profile.displayname, emails: emails }; yield users.insert(user); }); } console.log(user); done(null, user); }));
publicrouter .get('/', function* () { if (this.isauthenticated()) { yield this.render('homesecure', { user: this.req.user }); } else { yield this.render('homepublic', {}); } })...
Comments
Post a Comment