Skip to content

Commit

Permalink
feat: email verification
Browse files Browse the repository at this point in the history
  • Loading branch information
elpwc committed Jul 19, 2024
1 parent c038f09 commit 31beeea
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

/todo.txt
/src/privateConfig.ts

/public/api/plugin/*
3 changes: 2 additions & 1 deletion database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ create table `user`(
`auth` INT DEFAULT 0,
`create_date` DATETIME DEFAULT CURRENT_TIMESTAMP,
`update_date` DATETIME ON UPDATE CURRENT_TIMESTAMP,
`last_login` DATETIME DEFAULT CURRENT_TIMESTAMP
`last_login` DATETIME DEFAULT CURRENT_TIMESTAMP,
`email` NOT NULL,
)ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;

create table `collection`(
Expand Down
18 changes: 13 additions & 5 deletions public/api/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@
switch ($request_type) {
case 'POST':

@$name = trim((string)($data->name));
@$name_email = trim((string)($data->name));
@$pw = trim((string)($data->pw));

// user exist
$sql = 'SELECT `name` FROM `user`
WHERE `name`="' . $name . '" AND `pw`="' . $pw . '" AND `is_deleted`=0 AND `is_banned`=0
$usersql = 'SELECT `name` FROM `user`
WHERE `name`="' . $name_email . '" AND `pw`="' . $pw . '" AND `is_deleted`=0 AND `is_banned`=0
;';

$result = mysqli_query($sqllink, $sql);
if ($result->num_rows > 0) {
// email exist
$emailsql = 'SELECT `email` FROM `user`
WHERE `email`="' . $name_email . '" AND `pw`="' . $pw . '" AND `is_deleted`=0 AND `is_banned`=0
;';

$user_result = mysqli_query($sqllink, $usersql);

$email_result = mysqli_query($sqllink, $emailsql);

if (($user_result->num_rows > 0) || ($email_result->num_rows > 0)) {
// exist
@$token = md5(((string)time()) + $name);
$_SESSION["token"] = $token;
Expand Down
25 changes: 25 additions & 0 deletions public/api/mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* 邮件验证
*/

require './utils.php';

session_start();

switch ($request_type) {
case 'POST':
@$email = trim((string)($data->email));

$verify_code = mt_rand(100000, 999999);

$_SESSION["verify_code"] = $verify_code;

send_verification_mail($email, $verify_code);


break;
default:
break;
}
19 changes: 19 additions & 0 deletions public/api/private/emailcfg.example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
$i = 2;
if($i == 1){
define("HOST", "smtp.yajuu.com");
define("USER", "tadokoro");
define("PASS", "114514simokitazawa");
define("SECURE", 'TLS');
define("PORT", 587);
define("MAIL",'[email protected]');
}else if($i == 2){
define("HOST", "smtp.1919.jp");
define("USER", "kmr514");
define("PASS", "yametekureyo~");
define("SECURE", 'SSL');
define("PORT", 587);
define("MAIL",'[email protected]');
}else{

}
46 changes: 31 additions & 15 deletions public/api/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,48 @@
case 'POST':

@$name = trim((string)($data->name));
@$email = trim((string)($data->email));
@$verify_code = trim((string)($data->verify_code));
@$pw = trim((string)($data->pw));

// user exist
$sql = 'SELECT `name` FROM `user`
if ($verify_code == $_SESSION["verify_code"]) {

unset($_SESSION['verify_code']);

// user exist
$usersql = 'SELECT `name` FROM `user`
WHERE `name`="' . $name . '" AND `is_deleted`=0
;';
// email exist
$emailsql = 'SELECT `email` FROM `user`
WHERE `email`="' . $email . '" AND `is_deleted`=0
;';

$result = mysqli_query($sqllink, $sql);
$user_result = mysqli_query($sqllink, $usersql);

if ($result->num_rows > 0) {
// exist
echo json_encode(["res" => "exist"]);
} else {
// not exist
$sql = 'INSERT
$email_result = mysqli_query($sqllink, $emailsql);

if (($user_result->num_rows > 0) || ($email_result->num_rows > 0)) {
// exist
echo json_encode(["res" => "exist"]);
} else {
// not exist
$sql = 'INSERT
INTO `user` (`name`, `pw`)
VALUES ("' . $name . '","' . $pw . '");
';

$result = mysqli_query($sqllink, $sql);
if ($result == true) {
echo json_encode(["res" => "ok"]);
} else {
echo json_encode(["res" => "unknown_error"]);
$result = mysqli_query($sqllink, $sql);
if ($result == true) {
echo json_encode(["res" => "ok"]);
} else {
echo json_encode(["res" => "unknown_error"]);
}
}
} else {
echo json_encode(["res" => "verification_error"]);
}

break;
case 'PATCH':

Expand All @@ -61,7 +77,7 @@
WHERE `id`=$id;";

$result = mysqli_query($sqllink, $sql);

if ($result == true) {
echo json_encode(["res" => "ok"]);
} else {
Expand Down
23 changes: 23 additions & 0 deletions public/api/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

require('./private/illegal_words_list.php');
require "../private/emailcfg.php";
require './plugin/Lib_Smtp.php';

/**
* 防注入
Expand Down Expand Up @@ -37,3 +39,24 @@ function cator_to_cn_censorship($text)
}
return $res;
}



function send_verification_mail($target, $verify_code)
{
try {
$mail = new Lib_Smtp();

$mail->setServer(HOST, USER, PASS, PORT, true);
$mail->setFrom(MAIL);
$mail->setReceiver($target);
$mail->addAttachment("");
$mail->setMail(
"老头环地图 邮箱验证码",
'<h3>验证码是:<span>' . $verify_code . '</span></h3><p>有效期:5分钟</p>' . date('Y-m-d H:i:s')
);
echo $mail->send();
} catch (Exception $e) {
echo 'failed';
}
}
39 changes: 37 additions & 2 deletions src/components/MapView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,15 @@
let loginVisability: boolean = false;
let username: string = '';
let email: string = '';
let verification_code: string = '';
let password: string = '';
let password_2: string = '';
let recaptcha_res: string = '';
/** 邮箱验证码倒计时,如果是0的时候可以按下 发送验证码 的按钮,大于0时按钮变灰显示倒计时*/
let countdown_for_verification_code: number = 0;
let register_error: string = '';
let current_username: string = '';
Expand All @@ -210,10 +215,12 @@
let is_login: boolean = false;
/** 注册 */
const register = (uid: string, password_: string, onOK: () => void) => {
const register = (uid: string, password_: string, email: string, email_verification_code: string, onOK: () => void) => {
axios
.post('./user.php', {
name: uid,
email,
verify_code: email_verification_code,
pw: md5(password_),
})
.then(res => {
Expand All @@ -228,6 +235,9 @@
case 'unknown_error':
register_error = $t('map.modals.register.unknown_error');
break;
case 'verification_error':
register_error = $t('map.modals.register.verification_code_error');
break;
default:
break;
}
Expand Down Expand Up @@ -1983,7 +1993,7 @@
register_error = '';
if (username.length <= 20) {
if (password === password_2) {
register(username, password, () => {
register(username, password, email, verification_code, () => {
registerVisability = false;
loginVisability = true;
});
Expand All @@ -2004,6 +2014,8 @@
password = '';
password_2 = '';
recaptcha_res = '';
verification_code = '';
email = '';
}}
>
<div class="modalInner" style="align-items: center;">
Expand All @@ -2023,6 +2035,26 @@
);
}}
/>
<input type="email" placeholder={$t('map.modals.register.email')} bind:value={email} />
<div style="display: flex; justify-content: space-between;">
<input style="width: -webkit-fill-available;" type="text" placeholder={$t('map.modals.register.verification_code')} bind:value={verification_code} />
<button
on:click={() => {
countdown_for_verification_code = 60;
const countdown_verification_timer = setInterval(() => {
countdown_for_verification_code--;
if (countdown_for_verification_code <= 0) {
clearTimeout(countdown_verification_timer);
}
}, 1000);
}}
disabled={countdown_for_verification_code > 0}
>
{countdown_for_verification_code === 0
? $t('map.modals.register.send_verification_code')
: $t('map.modals.register.wait_for_one_minute').replace('{seconds}', countdown_for_verification_code.toString())}
</button>
</div>
<input type="password" placeholder={$t('map.modals.register.password')} bind:value={password} />
<input type="password" placeholder={$t('map.modals.register.password2')} bind:value={password_2} />
<p>{register_error}</p>
Expand All @@ -2049,6 +2081,9 @@
register_error = '';
username = '';
password = '';
password_2 = '';
verification_code = '';
email = '';
}}
onOKButtonClick={() => {
login(username, password, true, () => {
Expand Down
13 changes: 12 additions & 1 deletion src/locale/lang/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,24 @@ const lang = {
unknown_error: '服务器端发生了未知错误',
username_too_long: '用户id请不要超过20个字符',
tips: '注册後可以在多设备间同步收藏点和隐藏点、管理自己添加的地标',
email: '邮箱',
verification_code: '邮箱验证码',
send_verification_code: '发送验证码',
wait_for_one_minute: '请等待 {seconds} 秒',
email_blank: '还没有输入邮箱哦',
username_blank: '记得输入用户名哦',
password_blank: '密码还沒有输入哦',
verification_code_blank: '邮箱验证码还沒有输入哦',
verification_code_error: '邮箱验证码不正确哦',
verification_code_send: '已发送, 如果没有收到请检查垃圾邮件',
},
login: {
title: '登录',
email: '邮箱',
username: '用户id',
password: '密码',
remember: '在这台设备上保持登录状态',
error: '用户id或者密码错误',
error: '邮箱/用户id或者密码错误',
},
},
alert: {
Expand Down
15 changes: 13 additions & 2 deletions src/locale/lang/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,25 @@ const lang = {
password_not_the_same: '两次输入的密码不一致',
unknown_error: '服务器端发生了未知错误',
username_too_long: '用户id请不要超过20个字符',
tips: '创建账号後可以在多设备间同步收藏点和隐藏点、以及管理自己添加的地标',
tips: '创建账号後可以在多设备间同步收藏点和隐藏点、管理自己添加的地标、以及编辑地标信息',
email: '邮箱',
verification_code: '邮箱验证码',
send_verification_code: '发送验证码',
wait_for_one_minute: '请等待{seconds}秒',
email_blank: '还没有输入邮箱哦',
username_blank: '记得输入用户名哦',
password_blank: '密码还沒有输入哦',
verification_code_blank: '邮箱验证码还沒有输入哦',
verification_code_error: '邮箱验证码不正确哦',
verification_code_send: '已发送, 如果没有收到请检查垃圾邮件',
},
login: {
title: '登录',
email: '邮箱',
username: '用户id',
password: '密码',
remember: '在这台设备上保持登录状态',
error: '用户id或者密码错误',
error: '邮箱/用户id或者密码错误',
},
},
alert: {
Expand Down
13 changes: 12 additions & 1 deletion src/locale/lang/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,24 @@ const lang: langType = {
unknown_error: '伺服器端發生了未知錯誤',
username_too_long: '用戶id請不要超過20個字符',
tips: '建立賬號後可以在多設備閒同步收藏點和隱藏點、以及管理自己添加的地標',
email: '邮箱',
verification_code: '邮箱验证码',
send_verification_code: '发送验证码',
wait_for_one_minute: '请等待 {seconds} 秒',
email_blank: '还没有输入邮箱哦',
username_blank: '记得输入用户名哦',
password_blank: '密码还沒有输入哦',
verification_code_blank: '邮箱验证码还沒有输入哦',
verification_code_error: '邮箱验证码不正确哦',
verification_code_send: '已发送, 如果没有收到请检查垃圾邮件',
},
login: {
title: '登入',
email: '郵箱',
username: '用戶id',
password: '密碼',
remember: '在這臺設備上保持登入狀態',
error: '用戶id或者密碼錯誤',
error: '郵箱/用戶id或者密碼錯誤',
},
},
alert: {
Expand Down

0 comments on commit 31beeea

Please sign in to comment.