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
aad97c4c
Unverified
Commit
aad97c4c
authored
Feb 11, 2023
by
boojack
Committed by
GitHub
Feb 11, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update signup api (#1067)
parent
3590d3f8
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
66 additions
and
81 deletions
+66
-81
auth.go
api/auth.go
+0
-1
common.go
api/common.go
+0
-0
user.go
api/user.go
+4
-10
docker-compose.uffizzi.yml
docker-compose.uffizzi.yml
+0
-1
auth.go
server/auth.go
+32
-32
user.go
server/user.go
+25
-25
api.ts
web/src/helpers/api.ts
+1
-2
Auth.tsx
web/src/pages/Auth.tsx
+4
-10
No files found.
api/auth.go
View file @
aad97c4c
...
@@ -8,5 +8,4 @@ type SignIn struct {
...
@@ -8,5 +8,4 @@ type SignIn struct {
type
SignUp
struct
{
type
SignUp
struct
{
Username
string
`json:"username"`
Username
string
`json:"username"`
Password
string
`json:"password"`
Password
string
`json:"password"`
Role
Role
`json:"role"`
}
}
api/
api
.go
→
api/
common
.go
View file @
aad97c4c
File moved
api/user.go
View file @
aad97c4c
...
@@ -60,15 +60,12 @@ type UserCreate struct {
...
@@ -60,15 +60,12 @@ type UserCreate struct {
}
}
func
(
create
UserCreate
)
Validate
()
error
{
func
(
create
UserCreate
)
Validate
()
error
{
if
len
(
create
.
Username
)
<
4
{
if
len
(
create
.
Username
)
<
3
{
return
fmt
.
Errorf
(
"username is too short, minimum length is
4
"
)
return
fmt
.
Errorf
(
"username is too short, minimum length is
3
"
)
}
}
if
len
(
create
.
Username
)
>
32
{
if
len
(
create
.
Username
)
>
32
{
return
fmt
.
Errorf
(
"username is too long, maximum length is 32"
)
return
fmt
.
Errorf
(
"username is too long, maximum length is 32"
)
}
}
if
len
(
create
.
Password
)
<
4
{
return
fmt
.
Errorf
(
"password is too short, minimum length is 4"
)
}
if
len
(
create
.
Nickname
)
>
64
{
if
len
(
create
.
Nickname
)
>
64
{
return
fmt
.
Errorf
(
"nickname is too long, maximum length is 64"
)
return
fmt
.
Errorf
(
"nickname is too long, maximum length is 64"
)
}
}
...
@@ -102,15 +99,12 @@ type UserPatch struct {
...
@@ -102,15 +99,12 @@ type UserPatch struct {
}
}
func
(
patch
UserPatch
)
Validate
()
error
{
func
(
patch
UserPatch
)
Validate
()
error
{
if
patch
.
Username
!=
nil
&&
len
(
*
patch
.
Username
)
<
4
{
if
patch
.
Username
!=
nil
&&
len
(
*
patch
.
Username
)
<
3
{
return
fmt
.
Errorf
(
"username is too short, minimum length is
4
"
)
return
fmt
.
Errorf
(
"username is too short, minimum length is
3
"
)
}
}
if
patch
.
Username
!=
nil
&&
len
(
*
patch
.
Username
)
>
32
{
if
patch
.
Username
!=
nil
&&
len
(
*
patch
.
Username
)
>
32
{
return
fmt
.
Errorf
(
"username is too long, maximum length is 32"
)
return
fmt
.
Errorf
(
"username is too long, maximum length is 32"
)
}
}
if
patch
.
Password
!=
nil
&&
len
(
*
patch
.
Password
)
<
4
{
return
fmt
.
Errorf
(
"password is too short, minimum length is 4"
)
}
if
patch
.
Nickname
!=
nil
&&
len
(
*
patch
.
Nickname
)
>
64
{
if
patch
.
Nickname
!=
nil
&&
len
(
*
patch
.
Nickname
)
>
64
{
return
fmt
.
Errorf
(
"nickname is too long, maximum length is 64"
)
return
fmt
.
Errorf
(
"nickname is too long, maximum length is 64"
)
}
}
...
...
docker-compose.uffizzi.yml
View file @
aad97c4c
...
@@ -15,4 +15,3 @@ services:
...
@@ -15,4 +15,3 @@ services:
volumes
:
volumes
:
memos_volume
:
memos_volume
:
server/auth.go
View file @
aad97c4c
...
@@ -62,44 +62,45 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
...
@@ -62,44 +62,45 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signup request"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signup request"
)
.
SetInternal
(
err
)
}
}
userCreate
:=
&
api
.
UserCreate
{
Username
:
signup
.
Username
,
// The new signup user should be normal user by default.
Role
:
api
.
NormalUser
,
Nickname
:
signup
.
Username
,
Password
:
signup
.
Password
,
OpenID
:
common
.
GenUUID
(),
}
hostUserType
:=
api
.
Host
hostUserType
:=
api
.
Host
hostUserFind
:=
api
.
UserFind
{
existedHostUsers
,
err
:=
s
.
Store
.
FindUserList
(
ctx
,
&
api
.
UserFind
{
Role
:
&
hostUserType
,
Role
:
&
hostUserType
,
}
hostUser
,
err
:=
s
.
Store
.
FindUser
(
ctx
,
&
hostUserFind
)
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find host user"
)
.
SetInternal
(
err
)
}
if
signup
.
Role
==
api
.
Host
&&
hostUser
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Site Host existed, please contact the site host to signin account firstly"
)
.
SetInternal
(
err
)
}
systemSettingAllowSignUpName
:=
api
.
SystemSettingAllowSignUpName
allowSignUpSetting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
&
systemSettingAllowSignUpName
,
})
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find system setting"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Failed to find users"
)
.
SetInternal
(
err
)
}
}
if
len
(
existedHostUsers
)
==
0
{
// Change the default role to host if there is no host user.
userCreate
.
Role
=
api
.
Host
}
else
{
systemSettingAllowSignUpName
:=
api
.
SystemSettingAllowSignUpName
allowSignUpSetting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
&
systemSettingAllowSignUpName
,
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find system setting"
)
.
SetInternal
(
err
)
}
allowSignUpSettingValue
:=
false
allowSignUpSettingValue
:=
false
if
allowSignUpSetting
!=
nil
{
if
allowSignUpSetting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
allowSignUpSetting
.
Value
),
&
allowSignUpSettingValue
)
err
=
json
.
Unmarshal
([]
byte
(
allowSignUpSetting
.
Value
),
&
allowSignUpSettingValue
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting allow signup"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting allow signup"
)
.
SetInternal
(
err
)
}
}
if
!
allowSignUpSettingValue
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"signup is disabled"
)
.
SetInternal
(
err
)
}
}
}
if
!
allowSignUpSettingValue
&&
hostUser
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Site Host existed, please contact the site host to signin account firstly"
)
.
SetInternal
(
err
)
}
}
userCreate
:=
&
api
.
UserCreate
{
Username
:
signup
.
Username
,
Role
:
api
.
Role
(
signup
.
Role
),
Nickname
:
signup
.
Username
,
Password
:
signup
.
Password
,
OpenID
:
common
.
GenUUID
(),
}
if
err
:=
userCreate
.
Validate
();
err
!=
nil
{
if
err
:=
userCreate
.
Validate
();
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid user create format"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid user create format"
)
.
SetInternal
(
err
)
}
}
...
@@ -110,7 +111,6 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
...
@@ -110,7 +111,6 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
}
}
userCreate
.
PasswordHash
=
string
(
passwordHash
)
userCreate
.
PasswordHash
=
string
(
passwordHash
)
user
,
err
:=
s
.
Store
.
CreateUser
(
ctx
,
userCreate
)
user
,
err
:=
s
.
Store
.
CreateUser
(
ctx
,
userCreate
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to create user"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to create user"
)
.
SetInternal
(
err
)
...
...
server/user.go
View file @
aad97c4c
...
@@ -87,61 +87,61 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
...
@@ -87,61 +87,61 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return
nil
return
nil
})
})
// GET /api/user/me is used to check if the user is logged in.
g
.
POST
(
"/user/setting"
,
func
(
c
echo
.
Context
)
error
{
g
.
GET
(
"/user/me"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
!
ok
{
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing auth session"
)
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing auth session"
)
}
}
userFind
:=
&
api
.
UserFind
{
userSettingUpsert
:=
&
api
.
UserSettingUpsert
{}
ID
:
&
userID
,
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
userSettingUpsert
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post user setting upsert request"
)
.
SetInternal
(
err
)
}
}
user
,
err
:=
s
.
Store
.
FindUser
(
ctx
,
userFind
)
if
err
:=
userSettingUpsert
.
Validate
();
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid user setting format"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find user"
)
.
SetInternal
(
err
)
}
}
userSettingList
,
err
:=
s
.
Store
.
FindUserSettingList
(
ctx
,
&
api
.
UserSettingFind
{
userSettingUpsert
.
UserID
=
userID
UserID
:
userID
,
userSetting
,
err
:=
s
.
Store
.
UpsertUserSetting
(
ctx
,
userSettingUpsert
)
})
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to
find userSettingList
"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to
upsert user setting
"
)
.
SetInternal
(
err
)
}
}
user
.
UserSettingList
=
userSettingList
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderContentType
,
echo
.
MIMEApplicationJSONCharsetUTF8
)
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderContentType
,
echo
.
MIMEApplicationJSONCharsetUTF8
)
if
err
:=
json
.
NewEncoder
(
c
.
Response
()
.
Writer
)
.
Encode
(
composeResponse
(
user
));
err
!=
nil
{
if
err
:=
json
.
NewEncoder
(
c
.
Response
()
.
Writer
)
.
Encode
(
composeResponse
(
user
Setting
));
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to encode user response"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to encode user
setting
response"
)
.
SetInternal
(
err
)
}
}
return
nil
return
nil
})
})
g
.
POST
(
"/user/setting"
,
func
(
c
echo
.
Context
)
error
{
// GET /api/user/me is used to check if the user is logged in.
g
.
GET
(
"/user/me"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
!
ok
{
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing auth session"
)
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing auth session"
)
}
}
userSettingUpsert
:=
&
api
.
UserSettingUpsert
{}
userFind
:=
&
api
.
UserFind
{
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
userSettingUpsert
);
err
!=
nil
{
ID
:
&
userID
,
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post user setting upsert request"
)
.
SetInternal
(
err
)
}
}
if
err
:=
userSettingUpsert
.
Validate
();
err
!=
nil
{
user
,
err
:=
s
.
Store
.
FindUser
(
ctx
,
userFind
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid user setting format"
)
.
SetInternal
(
err
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find user"
)
.
SetInternal
(
err
)
}
}
userSettingUpsert
.
UserID
=
userID
userSettingList
,
err
:=
s
.
Store
.
FindUserSettingList
(
ctx
,
&
api
.
UserSettingFind
{
userSetting
,
err
:=
s
.
Store
.
UpsertUserSetting
(
ctx
,
userSettingUpsert
)
UserID
:
userID
,
})
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to
upsert user setting
"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to
find userSettingList
"
)
.
SetInternal
(
err
)
}
}
user
.
UserSettingList
=
userSettingList
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderContentType
,
echo
.
MIMEApplicationJSONCharsetUTF8
)
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderContentType
,
echo
.
MIMEApplicationJSONCharsetUTF8
)
if
err
:=
json
.
NewEncoder
(
c
.
Response
()
.
Writer
)
.
Encode
(
composeResponse
(
user
Setting
));
err
!=
nil
{
if
err
:=
json
.
NewEncoder
(
c
.
Response
()
.
Writer
)
.
Encode
(
composeResponse
(
user
));
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to encode user
setting
response"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to encode user response"
)
.
SetInternal
(
err
)
}
}
return
nil
return
nil
})
})
...
...
web/src/helpers/api.ts
View file @
aad97c4c
...
@@ -25,11 +25,10 @@ export function signin(username: string, password: string) {
...
@@ -25,11 +25,10 @@ export function signin(username: string, password: string) {
});
});
}
}
export
function
signup
(
username
:
string
,
password
:
string
,
role
:
UserRole
)
{
export
function
signup
(
username
:
string
,
password
:
string
)
{
return
axios
.
post
<
ResponseObject
<
User
>>
(
"/api/auth/signup"
,
{
return
axios
.
post
<
ResponseObject
<
User
>>
(
"/api/auth/signup"
,
{
username
,
username
,
password
,
password
,
role
,
});
});
}
}
...
...
web/src/pages/Auth.tsx
View file @
aad97c4c
...
@@ -84,7 +84,7 @@ const Auth = () => {
...
@@ -84,7 +84,7 @@ const Auth = () => {
actionBtnLoadingState
.
setFinish
();
actionBtnLoadingState
.
setFinish
();
};
};
const
handleSignUpBtnsClick
=
async
(
role
:
UserRole
)
=>
{
const
handleSignUpBtnsClick
=
async
()
=>
{
if
(
actionBtnLoadingState
.
isLoading
)
{
if
(
actionBtnLoadingState
.
isLoading
)
{
return
;
return
;
}
}
...
@@ -103,7 +103,7 @@ const Auth = () => {
...
@@ -103,7 +103,7 @@ const Auth = () => {
try
{
try
{
actionBtnLoadingState
.
setLoading
();
actionBtnLoadingState
.
setLoading
();
await
api
.
signup
(
username
,
password
,
role
);
await
api
.
signup
(
username
,
password
);
const
user
=
await
userStore
.
doSignIn
();
const
user
=
await
userStore
.
doSignIn
();
if
(
user
)
{
if
(
user
)
{
navigate
(
"/"
);
navigate
(
"/"
);
...
@@ -157,10 +157,7 @@ const Auth = () => {
...
@@ -157,10 +157,7 @@ const Auth = () => {
{
actionBtnLoadingState
.
isLoading
&&
<
Icon
.
Loader
className=
"w-4 h-auto mr-2 animate-spin dark:text-gray-300"
/>
}
{
actionBtnLoadingState
.
isLoading
&&
<
Icon
.
Loader
className=
"w-4 h-auto mr-2 animate-spin dark:text-gray-300"
/>
}
{
systemStatus
?.
allowSignUp
&&
(
{
systemStatus
?.
allowSignUp
&&
(
<>
<>
<
button
<
button
className=
{
`btn-text ${actionBtnLoadingState.isLoading ? "requesting" : ""}`
}
onClick=
{
handleSignUpBtnsClick
}
>
className=
{
`btn-text ${actionBtnLoadingState.isLoading ? "requesting" : ""}`
}
onClick=
{
()
=>
handleSignUpBtnsClick
(
"USER"
)
}
>
{
t
(
"common.sign-up"
)
}
{
t
(
"common.sign-up"
)
}
</
button
>
</
button
>
<
span
className=
"mr-2 font-mono text-gray-200"
>
/
</
span
>
<
span
className=
"mr-2 font-mono text-gray-200"
>
/
</
span
>
...
@@ -172,10 +169,7 @@ const Auth = () => {
...
@@ -172,10 +169,7 @@ const Auth = () => {
</>
</>
)
:
(
)
:
(
<>
<>
<
button
<
button
className=
{
`btn-primary ${actionBtnLoadingState.isLoading ? "requesting" : ""}`
}
onClick=
{
handleSignUpBtnsClick
}
>
className=
{
`btn-primary ${actionBtnLoadingState.isLoading ? "requesting" : ""}`
}
onClick=
{
()
=>
handleSignUpBtnsClick
(
"HOST"
)
}
>
{
t
(
"auth.signup-as-host"
)
}
{
t
(
"auth.signup-as-host"
)
}
</
button
>
</
button
>
</>
</>
...
...
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