Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
b20741cc
Commit
b20741cc
authored
Dec 12, 2021
by
steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update users table with unique tag
parent
9db1f573
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
103 additions
and
20 deletions
+103
-20
auth.go
api/auth.go
+23
-3
user.go
api/user.go
+36
-0
initial_db.sql
resources/initial_db.sql
+2
-1
memos.db
resources/memos.db
+0
-0
user.go
store/user.go
+17
-0
api.ts
web/src/helpers/api.ts
+10
-8
Signin.tsx
web/src/pages/Signin.tsx
+3
-4
userService.ts
web/src/services/userService.ts
+12
-4
No files found.
api/auth.go
View file @
b20741cc
...
...
@@ -29,6 +29,16 @@ func handleUserSignUp(w http.ResponseWriter, r *http.Request) {
return
}
usernameUsable
,
_
:=
store
.
CheckUsernameUsable
(
userSignup
.
Username
)
if
!
usernameUsable
{
json
.
NewEncoder
(
w
)
.
Encode
(
Response
{
Succeed
:
false
,
Message
:
"Username is existed"
,
Data
:
nil
,
})
return
}
user
,
err
:=
store
.
CreateNewUser
(
userSignup
.
Username
,
userSignup
.
Password
,
""
,
""
)
if
err
!=
nil
{
...
...
@@ -65,7 +75,16 @@ func handleUserSignIn(w http.ResponseWriter, r *http.Request) {
user
,
err
:=
store
.
GetUserByUsernameAndPassword
(
userSignin
.
Username
,
userSignin
.
Password
)
if
err
!=
nil
{
e
.
ErrorHandler
(
w
,
"DATABASE_ERROR"
,
err
.
Error
())
if
err
==
sql
.
ErrNoRows
{
json
.
NewEncoder
(
w
)
.
Encode
(
Response
{
Succeed
:
false
,
Message
:
"Username and password not allowed"
,
Data
:
nil
,
})
}
else
{
e
.
ErrorHandler
(
w
,
"DATABASE_ERROR"
,
err
.
Error
())
}
return
}
...
...
@@ -202,8 +221,9 @@ func handleGithubAuthCallback(w http.ResponseWriter, r *http.Request) {
if
err
==
sql
.
ErrNoRows
{
username
:=
githubUser
.
Name
usernameUsable
,
_
:=
store
.
CheckUsernameUsable
(
username
)
if
!
usernameUsable
{
username
=
username
+
common
.
GenUUID
()
for
!
usernameUsable
{
username
=
githubUser
.
Name
+
common
.
GenUUID
()
usernameUsable
,
_
=
store
.
CheckUsernameUsable
(
username
)
}
user
,
_
=
store
.
CreateNewUser
(
username
,
username
,
githubUser
.
Login
,
""
)
}
...
...
api/user.go
View file @
b20741cc
...
...
@@ -37,6 +37,42 @@ func handleUpdateMyUserInfo(w http.ResponseWriter, r *http.Request) {
return
}
if
*
userPatch
.
Username
!=
""
{
usernameUsable
,
_
:=
store
.
CheckUsernameUsable
(
*
userPatch
.
Username
)
if
!
usernameUsable
{
json
.
NewEncoder
(
w
)
.
Encode
(
Response
{
Succeed
:
false
,
Message
:
"Username is existed"
,
Data
:
nil
,
})
return
}
}
if
*
userPatch
.
GithubName
!=
""
{
githubNameUsable
,
_
:=
store
.
CheckGithubNameUsable
(
*
userPatch
.
GithubName
)
if
!
githubNameUsable
{
json
.
NewEncoder
(
w
)
.
Encode
(
Response
{
Succeed
:
false
,
Message
:
"GitHub name is existed"
,
Data
:
nil
,
})
return
}
}
if
*
userPatch
.
WxOpenId
!=
""
{
wxOpenIdUsable
,
_
:=
store
.
CheckWxOpenIdUsable
(
*
userPatch
.
GithubName
)
if
!
wxOpenIdUsable
{
json
.
NewEncoder
(
w
)
.
Encode
(
Response
{
Succeed
:
false
,
Message
:
"Wx open id is existed"
,
Data
:
nil
,
})
return
}
}
user
,
err
:=
store
.
UpdateUser
(
userId
,
&
userPatch
)
if
err
!=
nil
{
...
...
resources/initial_db.sql
View file @
b20741cc
...
...
@@ -29,7 +29,8 @@ CREATE TABLE `users` (
`github_name`
TEXT
DEFAULT
''
,
`wx_open_id`
TEXT
DEFAULT
''
,
`created_at`
TEXT
NOT
NULL
DEFAULT
CURRENT_TIMESTAMP
,
`updated_at`
TEXT
NOT
NULL
DEFAULT
CURRENT_TIMESTAMP
`updated_at`
TEXT
NOT
NULL
DEFAULT
CURRENT_TIMESTAMP
,
UNIQUE
(
`username`
,
`github_name`
,
`wx_open_id`
)
);
INSERT
INTO
`users`
...
...
resources/memos.db
View file @
b20741cc
No preview for this file type
store/user.go
View file @
b20741cc
...
...
@@ -133,6 +133,23 @@ func CheckGithubNameUsable(githubName string) (bool, error) {
}
}
func
CheckWxOpenIdUsable
(
wxOpenId
string
)
(
bool
,
error
)
{
query
:=
`SELECT * FROM users WHERE wx_open_id=?`
query
=
fmt
.
Sprintf
(
"SELECT COUNT(*) FROM (%s)"
,
query
)
var
count
uint
err
:=
DB
.
QueryRow
(
query
,
wxOpenId
)
.
Scan
(
&
count
)
if
err
!=
nil
&&
err
!=
sql
.
ErrNoRows
{
return
false
,
FormatDBError
(
err
)
}
if
count
>
0
{
return
false
,
nil
}
else
{
return
true
,
nil
}
}
func
CheckPasswordValid
(
id
string
,
password
string
)
(
bool
,
error
)
{
query
:=
`SELECT * FROM users WHERE id=? AND password=?`
query
=
fmt
.
Sprintf
(
"SELECT COUNT(*) FROM (%s)"
,
query
)
...
...
web/src/helpers/api.ts
View file @
b20741cc
...
...
@@ -6,7 +6,7 @@ type ResponseType<T = unknown> = {
data
:
T
;
};
async
function
request
<
T
>
(
method
:
string
,
url
:
string
,
data
?:
BasicType
):
Promise
<
ResponseType
<
T
>>
{
async
function
request
<
T
>
(
method
:
string
,
url
:
string
,
data
?:
any
):
Promise
<
ResponseType
<
T
>>
{
const
requestConfig
:
RequestInit
=
{
method
,
};
...
...
@@ -55,13 +55,15 @@ namespace api {
return
request
<
boolean
>
(
"POST"
,
"/api/user/validpassword"
,
{
password
});
}
export
function
updateUserinfo
(
username
?:
string
,
password
?:
string
,
githubName
?:
string
,
wxOpenId
?:
string
)
{
return
request
(
"PATCH"
,
"/api/user/me"
,
{
username
,
password
,
githubName
,
wxOpenId
,
});
interface
UserInfoPatch
{
username
?:
string
;
password
?:
string
;
githubName
?:
string
;
wxOpenId
?:
string
;
}
export
function
updateUserinfo
(
userinfo
:
UserInfoPatch
)
{
return
request
(
"PATCH"
,
"/api/user/me"
,
userinfo
);
}
export
function
getMyMemos
()
{
...
...
web/src/pages/Signin.tsx
View file @
b20741cc
...
...
@@ -4,7 +4,6 @@ import { validate, ValidatorConfig } from "../helpers/validator";
import
useLoading
from
"../hooks/useLoading"
;
import
{
locationService
,
userService
}
from
"../services"
;
import
Only
from
"../components/common/OnlyWhen"
;
import
showAboutSiteDialog
from
"../components/AboutSiteDialog"
;
import
toastHelper
from
"../components/Toast"
;
import
"../less/signin.less"
;
...
...
@@ -50,8 +49,8 @@ const Signin: React.FC<Props> = () => {
setPassword
(
text
);
};
const
handle
AboutBtnClick
=
()
=>
{
showAboutSiteDialog
(
);
const
handle
SignUpBtnClick
=
async
()
=>
{
toastHelper
.
info
(
"注册已关闭"
);
};
const
handleSignInBtnClick
=
async
()
=>
{
...
...
@@ -186,7 +185,7 @@ const Signin: React.FC<Props> = () => {
体验一下
</
button
>
<
span
className=
"split-text"
>
/
</
span
>
<
button
className=
"btn signup-btn
disabled"
onClick=
{
()
=>
toastHelper
.
info
(
"注册已关闭"
)
}
>
<
button
className=
"btn signup-btn
"
onClick=
{
handleSignUpBtnClick
}
>
注册
</
button
>
<
span
className=
"split-text"
>
/
</
span
>
...
...
web/src/services/userService.ts
View file @
b20741cc
...
...
@@ -35,11 +35,15 @@ class UserService {
}
public
async
updateUsername
(
username
:
string
):
Promise
<
void
>
{
await
api
.
updateUserinfo
(
username
);
await
api
.
updateUserinfo
({
username
,
});
}
public
async
removeGithubName
():
Promise
<
void
>
{
await
api
.
updateUserinfo
(
undefined
,
undefined
,
""
);
await
api
.
updateUserinfo
({
githubName
:
""
,
});
}
public
async
checkPasswordValid
(
password
:
string
):
Promise
<
boolean
>
{
...
...
@@ -48,11 +52,15 @@ class UserService {
}
public
async
updatePassword
(
password
:
string
):
Promise
<
void
>
{
await
api
.
updateUserinfo
(
undefined
,
password
);
await
api
.
updateUserinfo
({
password
,
});
}
public
async
updateWxOpenId
(
wxOpenId
:
string
):
Promise
<
void
>
{
await
api
.
updateUserinfo
(
undefined
,
undefined
,
undefined
,
wxOpenId
);
await
api
.
updateUserinfo
({
wxOpenId
,
});
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment