summaryrefslogtreecommitdiff
path: root/src/api/endpoints/auth/accept.js
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2016-12-29 07:49:51 +0900
committersyuilo <syuilotan@yahoo.co.jp>2016-12-29 07:49:51 +0900
commitb3f42e62af698a67c2250533c437569559f1fdf9 (patch)
treecdf6937576e99cccf85e6fa3aa8860a1173c7cfb /src/api/endpoints/auth/accept.js
downloadmisskey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.gz
misskey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.bz2
misskey-b3f42e62af698a67c2250533c437569559f1fdf9.zip
Initial commit :four_leaf_clover:
Diffstat (limited to 'src/api/endpoints/auth/accept.js')
-rw-r--r--src/api/endpoints/auth/accept.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/api/endpoints/auth/accept.js b/src/api/endpoints/auth/accept.js
new file mode 100644
index 0000000000..7c45650c6b
--- /dev/null
+++ b/src/api/endpoints/auth/accept.js
@@ -0,0 +1,64 @@
+'use strict';
+
+/**
+ * Module dependencies
+ */
+import rndstr from 'rndstr';
+import AuthSess from '../../models/auth-session';
+import Userkey from '../../models/userkey';
+
+/**
+ * Accept
+ *
+ * @param {Object} params
+ * @param {Object} user
+ * @return {Promise<object>}
+ */
+module.exports = (params, user) =>
+ new Promise(async (res, rej) =>
+{
+ // Get 'token' parameter
+ const token = params.token;
+ if (token == null) {
+ return rej('token is required');
+ }
+
+ // Fetch token
+ const session = await AuthSess
+ .findOne({ token: token });
+
+ if (session === null) {
+ return rej('session not found');
+ }
+
+ // Generate userkey
+ const key = rndstr('a-zA-Z0-9', 32);
+
+ // Fetch exist userkey
+ const exist = await Userkey.findOne({
+ app_id: session.app_id,
+ user_id: user._id,
+ });
+
+ if (exist === null) {
+ // Insert userkey doc
+ await Userkey.insert({
+ created_at: new Date(),
+ app_id: session.app_id,
+ user_id: user._id,
+ key: key
+ });
+ }
+
+ // Update session
+ await AuthSess.updateOne({
+ _id: session._id
+ }, {
+ $set: {
+ user_id: user._id
+ }
+ });
+
+ // Response
+ res();
+});