Skip to content

Commit

Permalink
用 github id 取代 accesstoken 来确定唯一用户
Browse files Browse the repository at this point in the history
  • Loading branch information
alsotang committed Dec 24, 2013
1 parent da311a8 commit 72b8245
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions controllers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ var Models = require('../models');
var User = Models.User;

exports.callback = function (req, res, next) {
User.findOne({githubAccessToken: req.user.accessToken}, function (err, user) {
var profile = req.user;
User.findOne({githubId: profile.id}, function (err, user) {
if (err) {
return next(err);
}
if (user) {
sign.gen_session(user, res);
return res.redirect('/');
} else {
req.session.profile = req.user;
req.session.profile = profile;
return res.redirect('/auth/github/new');
}
});
Expand All @@ -32,7 +33,7 @@ exports.create = function (req, res, next) {
pass: profile.accessToken,
email: profile.emails[0].value,
avatar: profile._json.avatar_url,
githubAccessToken: profile.accessToken,
githubId: profile.id,
});
user.save(function (err) {
if (err) {
Expand All @@ -50,7 +51,7 @@ exports.create = function (req, res, next) {
if (!user) {
return res.render('sign/signin', { error: '账号名或密码错误。' });
}
user.githubAccessToken = profile.accessToken;
user.githubId = profile.id;
user.save(function (err) {
if (err) {
return next(err);
Expand Down
2 changes: 1 addition & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var UserSchema = new Schema({
profile: { type: String },
weibo: { type: String },
avatar: { type: String },
githubAccessToken: { type: String },
githubId: { type: String, unique: true },

score: { type: Number, default: 0 },
topic_count: { type: Number, default: 0 },
Expand Down

0 comments on commit 72b8245

Please sign in to comment.