Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-1-CloudComputing
/
C_Team_KhuDrive
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
김재형
2020-06-14 19:19:35 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
569842a83e6de4214b7ad54fae1d6a80e6671835
569842a8
1 parent
4000fca7
Change login api
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
15 deletions
frontend/src/auth/useAuth.ts
frontend/src/auth/useAuth.ts
View file @
569842a
import
{
useState
,
useCallback
}
from
"react"
;
import
React
,
{
useState
,
useCallback
}
from
"react"
;
import
ky
from
"ky"
;
interface
LoginResponse
{
status
:
number
;
data
:
{
access_token
:
string
;
refresh_token
:
string
;
expiration
:
string
;
access
:
string
;
refresh
:
string
;
exp
:
number
;
user
:
{
int_id
:
number
;
user_id
:
string
;
name
:
string
;
total_size
:
number
;
current_size
:
number
;
root_folder
:
number
;
};
}
...
...
@@ -14,8 +19,18 @@ interface Token {
accessToken
:
string
;
refreshToken
:
string
;
expiration
:
Date
;
user
:
{
id
:
number
;
username
:
string
;
name
:
string
;
totalSize
:
number
;
currentSize
:
number
;
rootFolder
:
number
;
};
}
export
const
TokenContext
=
React
.
createContext
<
Token
>
({}
as
Token
);
export
function
useAuth
()
{
const
[
token
,
setToken
]
=
useState
<
Token
|
null
>
(()
=>
{
const
item
=
localStorage
.
getItem
(
"token"
);
...
...
@@ -29,19 +44,26 @@ export function useAuth() {
const
login
=
useCallback
(
async
(
username
:
string
,
password
:
string
,
remember
:
boolean
)
=>
{
const
body
=
new
URLSearchParams
();
body
.
set
(
"user_id"
,
username
);
body
.
set
(
"password"
,
password
);
const
response
=
await
ky
.
post
(
"/users/login"
,
{
json
:
{
user_id
:
username
,
password
:
password
,
},
})
.
post
(
"/users/login/"
,
{
body
})
.
json
<
LoginResponse
>
();
const
token
=
{
accessToken
:
response
.
data
.
access_token
,
refreshToken
:
response
.
data
.
refresh_token
,
expiration
:
new
Date
(
response
.
data
.
expiration
),
accessToken
:
response
.
access
,
refreshToken
:
response
.
refresh
,
expiration
:
new
Date
(
response
.
exp
*
1000
),
user
:
{
id
:
response
.
user
.
int_id
,
username
:
response
.
user
.
user_id
,
name
:
response
.
user
.
name
,
totalSize
:
response
.
user
.
total_size
,
currentSize
:
response
.
user
.
current_size
,
rootFolder
:
response
.
user
.
root_folder
,
},
};
setToken
(
token
);
...
...
Please
register
or
login
to post a comment