Skip to content

Commit

Permalink
高速化
Browse files Browse the repository at this point in the history
  • Loading branch information
tako0614 committed Feb 20, 2024
1 parent be22fa4 commit 5d9330e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
4 changes: 4 additions & 0 deletions islands/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function RegisterForm({ text, color,tako }: { text: string, color
};
const handleSubmit = async (event: JSX.TargetedEvent<HTMLFormElement, Event>) => {
event.preventDefault();
const startTime = Date.now();
const token = await fetch("http://localhost:8000/api/token?origin=http://localhost:8000")
const csrftoken = await token.json();
const data = {
Expand All @@ -38,6 +39,9 @@ export default function RegisterForm({ text, color,tako }: { text: string, color
})
const response = await res.json()
if(response.status == true) {

const endTime = Date.now();
alert(endTime - startTime);
alert("ueeeee")
} else {
alert("takotako")
Expand Down
47 changes: 27 additions & 20 deletions routes/api/logins/register.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// deno-lint-ignore-file
import { Client } from "https://deno.land/x/mysql@v2.12.1/mod.ts";
import { isMail, isUserDuplication, isMailDuplication, isMailDuplicationTemp, hashPassword, sendMail,client} from "../../../util/takoFunction.ts";
import { isMail, isUserDuplication, isMailDuplication, isMailDuplicationTemp, isCsrftoken, sendMail,client} from "../../../util/takoFunction.ts";

export const handler = {
async POST(req) {
Expand All @@ -12,32 +12,39 @@ export const handler = {
}
const ismail = isMail(email)
const ismailduplication = await isMailDuplication(email)
if(ismail) {
if(!ismailduplication) {
try {
await isMailDuplicationTemp(email)
const key = generateRandomString(255);
await client.query(`INSERT INTO temp_users (id,created_at,mail, kye) VALUES (default,default,"${email}",'${key}');`)
sendMail(email,"本登録を完了してください",`https://takos.jp/register?key=${key}`)
return new Response(JSON.stringify({status: true}), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
return new Response(JSON.stringify({"status": "error"}), {
const iscsrftoken = await isCsrftoken(CsrfToken)
if(isCsrftoken) {
client.execute(`DELETE FROM csrftoken WHERE csrftoken = "${CsrfToken}";`)
}
if(iscsrftoken) {
if(ismail) {
if(!ismailduplication) {
try {
const key = generateRandomString(255);
if(isMailDuplicationTemp(email)) {
client.execute(`DELETE FROM temp_users WHERE mail = "${email}";`)
}
client.query(`INSERT INTO temp_users (id,created_at,mail, kye) VALUES (default,default,"${email}",'${key}');`)
sendMail(email,"本登録を完了してください",`https://takos.jp/register?key=${key}`)
return new Response(JSON.stringify({status: true}), {
headers: { "Content-Type": "application/json" },
});
}
} catch (error) {
return new Response(JSON.stringify({"status": "error"}), {
headers: { "Content-Type": "application/json" },
});
}
}else {
return new Response(JSON.stringify({"status": "error"}), {
headers: { "Content-Type": "application/json" },
});
}
}else {
return new Response(JSON.stringify({"status": "error"}), {
headers: { "Content-Type": "application/json" },
});
}
}else {
return new Response(JSON.stringify({"status": "error"}), {
headers: { "Content-Type": "application/json" },
});
}

}
}
};
function generateRandomString(length) {
Expand Down
16 changes: 12 additions & 4 deletions util/takoFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,22 @@ async function isUserDuplication(userid: string): Promise<boolean> {
return result.length > 0;
}
async function isMailDuplication(mail: string): Promise<boolean> {
const result = await client.query(`SELECT * FROM users WHERE name = "${mail}"`);
const result = await client.query(`SELECT * FROM users WHERE mail = "${mail}"`);
return result.length > 0;
}
async function isMailDuplicationTemp(mail: string): Promise<any>/*Promise<boolean>*/ {
async function isCsrftoken(token:string):Promise<any> {
const query = `SELECT * FROM csrftoken WHERE csrftoken = "${token}"`
const result = await client.query(query);
if(result.length > 0) {
return true
} else {
return false
}
}
async function isMailDuplicationTemp(mail: string): Promise<boolean> {
const query = `SELECT * FROM temp_users WHERE mail = "${mail}"`
const result = await client.query(query);
if(result.length > 0) {
await client.execute(`DELETE FROM temp_users WHERE mail = "${mail}";`)
return true
} else {
return false
Expand Down Expand Up @@ -108,4 +116,4 @@ async function hashPassword(password: string, salt: string): Promise<string> {
password: string;
userName: string;
}
export { envRoader,client,sql, isMail, isUserDuplication, isMailDuplication, isSavePassword, sendMail, generateSalt, hashPassword,hostname,username,db,password,isMailDuplicationTemp};
export { isCsrftoken,envRoader,client,sql, isMail, isUserDuplication, isMailDuplication, isSavePassword, sendMail, generateSalt, hashPassword,hostname,username,db,password,isMailDuplicationTemp};

0 comments on commit 5d9330e

Please sign in to comment.