浏览代码

refactor: Changed recaptcha v2 to recaptcha v3

Kristian Vos 5 年之前
父节点
当前提交
93dc55b265
共有 4 个文件被更改,包括 34 次插入19 次删除
  1. 3 3
      frontend/api/auth.js
  2. 29 13
      frontend/components/Modals/Register.vue
  3. 0 1
      frontend/dist/index.tpl.html
  4. 2 2
      frontend/store/modules/user.js

+ 3 - 3
frontend/api/auth.js

@@ -3,9 +3,9 @@ import io from "../io";
 // when Vuex needs to interact with socket.io
 // when Vuex needs to interact with socket.io
 
 
 export default {
 export default {
-	register(user, recaptchaId) {
+	register(user) {
 		return new Promise((resolve, reject) => {
 		return new Promise((resolve, reject) => {
-			const { username, email, password } = user;
+			const { username, email, password, recaptchaToken } = user;
 
 
 			io.getSocket(socket => {
 			io.getSocket(socket => {
 				socket.emit(
 				socket.emit(
@@ -13,7 +13,7 @@ export default {
 					username,
 					username,
 					email,
 					email,
 					password,
 					password,
-					grecaptcha.getResponse(recaptchaId),
+					recaptchaToken,
 					res => {
 					res => {
 						if (res.status === "success") {
 						if (res.status === "success") {
 							if (res.SID) {
 							if (res.SID) {

+ 29 - 13
frontend/components/Modals/Register.vue

@@ -39,7 +39,6 @@
 						@keypress="$parent.submitOnEnter(submitModal, $event)"
 						@keypress="$parent.submitOnEnter(submitModal, $event)"
 					/>
 					/>
 				</p>
 				</p>
-				<div id="recaptcha" />
 				<p>
 				<p>
 					By logging in/registering you agree to our
 					By logging in/registering you agree to our
 					<router-link to="/terms"> Terms of Service </router-link
 					<router-link to="/terms"> Terms of Service </router-link
@@ -78,7 +77,8 @@ export default {
 			email: "",
 			email: "",
 			password: "",
 			password: "",
 			recaptcha: {
 			recaptcha: {
-				key: ""
+				key: "",
+				token: ""
 			}
 			}
 		};
 		};
 	},
 	},
@@ -86,21 +86,37 @@ export default {
 		let _this = this;
 		let _this = this;
 		lofig.get("recaptcha", obj => {
 		lofig.get("recaptcha", obj => {
 			_this.recaptcha.key = obj.key;
 			_this.recaptcha.key = obj.key;
-			_this.recaptcha.id = grecaptcha.render("recaptcha", {
-				sitekey: _this.recaptcha.key
-			});
+
+			let recaptchaScript = document.createElement("script");
+			recaptchaScript.onload = () => {
+				grecaptcha.ready(() => {
+					grecaptcha
+						.execute(this.recaptcha.key, { action: "login" })
+						.then(function(token) {
+							_this.recaptcha.token = token;
+						});
+				});
+			};
+
+			recaptchaScript.setAttribute(
+				"src",
+				`https://www.google.com/recaptcha/api.js?render=${
+					this.recaptcha.key
+				}`
+			);
+			document.head.appendChild(recaptchaScript);
 		});
 		});
 	},
 	},
 	methods: {
 	methods: {
 		submitModal: function() {
 		submitModal: function() {
-			this.register(
-				{
-					username: this.username,
-					email: this.email,
-					password: this.password
-				},
-				this.recaptcha.id
-			)
+			console.log(this.recaptcha.token);
+
+			this.register({
+				username: this.username,
+				email: this.email,
+				password: this.password,
+				recaptchaToken: this.recaptcha.token
+			})
 				.then(res => {
 				.then(res => {
 					if (res.status == "success") location.reload();
 					if (res.status == "success") location.reload();
 				})
 				})

+ 0 - 1
frontend/dist/index.tpl.html

@@ -51,6 +51,5 @@
 </head>
 </head>
 <body>
 <body>
 	<div id="root"></div>
 	<div id="root"></div>
-	<script src='https://www.google.com/recaptcha/api.js'></script>
 </body>
 </body>
 </html>
 </html>

+ 2 - 2
frontend/store/modules/user.js

@@ -14,7 +14,7 @@ const modules = {
 		getters: {},
 		getters: {},
 		actions: {
 		actions: {
 			/* eslint-disable-next-line no-unused-vars */
 			/* eslint-disable-next-line no-unused-vars */
-			register: ({ commit }, user, recaptchaId) => {
+			register: ({ commit }, user) => {
 				return new Promise((resolve, reject) => {
 				return new Promise((resolve, reject) => {
 					const { username, email, password } = user;
 					const { username, email, password } = user;
 
 
@@ -68,7 +68,7 @@ const modules = {
 								"Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character."
 								"Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character."
 						});
 						});
 
 
-					auth.register(user, recaptchaId)
+					auth.register(user)
 						.then(() => {
 						.then(() => {
 							return resolve({
 							return resolve({
 								status: "success",
 								status: "success",