summaryrefslogtreecommitdiff
path: root/src/api/endpoints
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-08-28 23:47:43 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-08-28 23:47:43 +0900
commitffaec0b9712df9a5024c0883a154442f02b72a03 (patch)
tree004e54ee0115860d8fa009ad8f5e2e14479ef239 /src/api/endpoints
parentWIP #738 (diff)
downloadsharkey-ffaec0b9712df9a5024c0883a154442f02b72a03.tar.gz
sharkey-ffaec0b9712df9a5024c0883a154442f02b72a03.tar.bz2
sharkey-ffaec0b9712df9a5024c0883a154442f02b72a03.zip
#497
Diffstat (limited to 'src/api/endpoints')
-rw-r--r--src/api/endpoints/i/regenerate_token.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/api/endpoints/i/regenerate_token.ts b/src/api/endpoints/i/regenerate_token.ts
new file mode 100644
index 0000000000..ccebbc8101
--- /dev/null
+++ b/src/api/endpoints/i/regenerate_token.ts
@@ -0,0 +1,42 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import * as bcrypt from 'bcryptjs';
+import User from '../../models/user';
+import event from '../../event';
+import generateUserToken from '../../common/generate-native-user-token';
+
+/**
+ * Regenerate native token
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'password' parameter
+ const [password, passwordErr] = $(params.password).string().$;
+ if (passwordErr) return rej('invalid password param');
+
+ // Compare password
+ const same = bcrypt.compareSync(password, user.password);
+
+ if (!same) {
+ return rej('incorrect password');
+ }
+
+ // Generate secret
+ const secret = generateUserToken();
+
+ await User.update(user._id, {
+ $set: {
+ token: secret
+ }
+ });
+
+ res();
+
+ // Publish i updated event
+ event(user._id, 'my_token_regenerated');
+});