|
|
@@ -68,6 +68,7 @@ const factory = (crowi) => {
|
|
|
// email: { type: String, required: true, unique: true },
|
|
|
introduction: String,
|
|
|
password: String,
|
|
|
+ apiToken: { type: String, index: true },
|
|
|
lang: {
|
|
|
type: String,
|
|
|
enum: i18n.locales,
|
|
|
@@ -146,6 +147,12 @@ const factory = (crowi) => {
|
|
|
return hasher.digest('hex');
|
|
|
}
|
|
|
|
|
|
+ function generateApiToken(user) {
|
|
|
+ const hasher = crypto.createHash('sha256');
|
|
|
+ hasher.update((new Date()).getTime() + user._id);
|
|
|
+
|
|
|
+ return hasher.digest('base64');
|
|
|
+ }
|
|
|
|
|
|
userSchema.methods.isUniqueEmail = async function() {
|
|
|
const query = this.model('User').find();
|
|
|
@@ -206,6 +213,14 @@ const factory = (crowi) => {
|
|
|
return userData;
|
|
|
};
|
|
|
|
|
|
+ userSchema.methods.updateApiToken = async function() {
|
|
|
+ const self = this;
|
|
|
+
|
|
|
+ self.apiToken = generateApiToken(this);
|
|
|
+ const userData = await self.save();
|
|
|
+ return userData;
|
|
|
+ };
|
|
|
+
|
|
|
// TODO: create UserService and transplant this method because image uploading depends on AttachmentService
|
|
|
userSchema.methods.updateImage = async function(attachment) {
|
|
|
this.imageAttachment = attachment;
|
|
|
@@ -431,6 +446,13 @@ const factory = (crowi) => {
|
|
|
return this.findOne({ username });
|
|
|
};
|
|
|
|
|
|
+ userSchema.statics.findUserByApiToken = function(apiToken) {
|
|
|
+ if (apiToken == null) {
|
|
|
+ return Promise.resolve(null);
|
|
|
+ }
|
|
|
+ return this.findOne({ apiToken }).lean();
|
|
|
+ };
|
|
|
+
|
|
|
userSchema.statics.findUserByGoogleId = function(googleId, callback) {
|
|
|
if (googleId == null) {
|
|
|
callback(null, null);
|