Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f3ebe460
authored
Apr 26, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: redirect to login page if login expired (close #18)
parent
512f9e3a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
6 deletions
+14
-6
middleware/auth.go
+1
-1
web/src/components/LoginForm.js
+5
-1
web/src/helpers/utils.js
+8
-4
No files found.
middleware/auth.go
View file @
f3ebe460
...
...
@@ -16,7 +16,7 @@ func authHelper(c *gin.Context, minRole int) {
id
:=
session
.
Get
(
"id"
)
status
:=
session
.
Get
(
"status"
)
if
username
==
nil
{
c
.
JSON
(
http
.
Status
OK
,
gin
.
H
{
c
.
JSON
(
http
.
Status
Unauthorized
,
gin
.
H
{
"success"
:
false
,
"message"
:
"无权进行此操作,未登录"
,
})
...
...
web/src/components/LoginForm.js
View file @
f3ebe460
...
...
@@ -10,7 +10,7 @@ import {
Modal
,
Segment
,
}
from
'semantic-ui-react'
;
import
{
Link
,
useNavigate
}
from
'react-router-dom'
;
import
{
Link
,
useNavigate
,
useSearchParams
}
from
'react-router-dom'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
API
,
showError
,
showSuccess
}
from
'../helpers'
;
...
...
@@ -20,6 +20,7 @@ const LoginForm = () => {
password
:
''
,
wechat_verification_code
:
''
,
});
const
[
searchParams
,
setSearchParams
]
=
useSearchParams
();
const
[
submitted
,
setSubmitted
]
=
useState
(
false
);
const
{
username
,
password
}
=
inputs
;
const
[
userState
,
userDispatch
]
=
useContext
(
UserContext
);
...
...
@@ -28,6 +29,9 @@ const LoginForm = () => {
const
[
status
,
setStatus
]
=
useState
({});
useEffect
(()
=>
{
if
(
searchParams
.
get
(
"expired"
))
{
showError
(
'未登录或登录已过期,请重新登录!'
);
}
let
status
=
localStorage
.
getItem
(
'status'
);
if
(
status
)
{
status
=
JSON
.
parse
(
status
);
...
...
web/src/helpers/utils.js
View file @
f3ebe460
...
...
@@ -54,14 +54,18 @@ export function showError(error) {
console
.
error
(
error
);
if
(
error
.
message
)
{
if
(
error
.
name
===
'AxiosError'
)
{
switch
(
error
.
message
)
{
case
'Request failed with status code 429'
:
switch
(
error
.
response
.
status
)
{
case
401
:
// toast.error('错误:未登录或登录已过期,请重新登录!', showErrorOptions);
window
.
location
.
href
=
'/login?expired=true'
;
break
;
case
429
:
toast
.
error
(
'错误:请求次数过多,请稍后再试!'
,
showErrorOptions
);
break
;
case
'Request failed with status code 500'
:
case
500
:
toast
.
error
(
'错误:服务器内部错误,请联系管理员!'
,
showErrorOptions
);
break
;
case
'Request failed with status code 405'
:
case
405
:
toast
.
info
(
'本站仅作演示之用,无服务端!'
);
break
;
default
:
...
...
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