Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
quiz-competition
Commits
08ce2115
Commit
08ce2115
authored
Sep 28, 2020
by
Yoon, Daeki
😅
Browse files
auth provider hook 로 변경
parent
70ab25bf
Changes
5
Show whitespace changes
Inline
Side-by-side
src/client/src/App.js
View file @
08ce2115
import
React
,
{
useState
}
from
'
react
'
;
import
React
from
'
react
'
;
import
{
BrowserRouter
}
from
'
react-router-dom
'
import
{
AuthContext
}
from
'
./auth/auth-context
'
;
import
authHelpers
from
'
./auth/auth-helpers
'
;
import
{
AuthProvider
}
from
'
./auth/auth-context
'
;
import
MainRouter
from
'
./MainRouter
'
;
function
App
()
{
const
[
authState
,
setAuthState
]
=
useState
(
authHelpers
.
isAuthenticated
())
return
(
<
Auth
Context
.
Provider
value
=
{{
authState
,
setAuthState
}}
>
<
Auth
Provider
>
<
BrowserRouter
>
<
MainRouter
/>
<
/BrowserRouter
>
<
/Auth
Context.
Provider
>
<
/AuthProvider
>
);
}
...
...
src/client/src/MainRouter.jsx
View file @
08ce2115
import
React
from
"
react
"
;
import
{
BrowserRouter
,
Route
,
Switch
}
from
"
react-router-dom
"
;
import
{
Route
,
Switch
}
from
"
react-router-dom
"
;
import
Signin
from
"
./auth/Signin
"
;
import
Home
from
"
./core/Home
"
;
import
Menu
from
"
./core/Menu
"
;
...
...
src/client/src/auth/Signin.jsx
View file @
08ce2115
import
React
,
{
useState
,
useContext
}
from
"
react
"
;
import
React
,
{
useState
}
from
"
react
"
;
import
Button
from
"
react-bootstrap/Button
"
;
import
Form
from
"
react-bootstrap/Form
"
;
import
Container
from
"
react-bootstrap/Container
"
;
import
{
signin
}
from
"
./api-auth
"
;
import
auth
from
"
./auth-helpers
"
;
import
{
Redirect
,
useHistory
}
from
"
react-router-dom
"
;
import
{
Auth
Context
}
from
"
./auth-context
"
;
import
{
Redirect
}
from
"
react-router-dom
"
;
import
{
use
Auth
}
from
"
./auth-context
"
;
function
Signin
()
{
const
authContext
=
useContext
(
AuthContext
)
// const history = useHistory()
const
{
setAuthUser
}
=
useAuth
();
const
[
values
,
setValues
]
=
useState
({
email
:
""
,
password
:
""
,
...
...
@@ -22,20 +21,18 @@ function Signin() {
};
const
clickSubmit
=
(
event
)
=>
{
event
.
preventDefault
()
event
.
preventDefault
()
;
const
user
=
{
email
:
values
.
email
||
undefined
,
password
:
values
.
password
||
undefined
,
};
console
.
log
(
'
authContext in Signin.jsx:
'
,
authContext
)
signin
(
user
).
then
((
data
)
=>
{
if
(
data
.
error
)
{
setValues
({
...
values
,
error
:
data
.
error
});
}
else
{
auth
.
authenticate
(
data
,
()
=>
{
authContext
.
setAuth
State
(
auth
.
isAuthenticated
())
setAuth
User
(
auth
.
isAuthenticated
())
;
setValues
({
...
values
,
error
:
""
,
redirect
:
true
});
});
}
...
...
@@ -43,7 +40,7 @@ function Signin() {
};
if
(
values
.
redirect
)
{
return
<
Redirect
to
=
'/'
/>
return
<
Redirect
to
=
"/"
/>
;
}
return
(
...
...
@@ -54,7 +51,7 @@ function Signin() {
<
Form
.
Control
type
=
"email"
placeholder
=
"Enter email"
onChange
=
{
handleChange
(
'
email
'
)
}
onChange
=
{
handleChange
(
"
email
"
)
}
/>
<
Form
.
Text
className
=
"text-muted"
>
We'll never share your email with anyone else.
...
...
@@ -66,7 +63,7 @@ function Signin() {
<
Form
.
Control
type
=
"password"
placeholder
=
"Password"
onChange
=
{
handleChange
(
'
password
'
)
}
onChange
=
{
handleChange
(
"
password
"
)
}
/>
</
Form
.
Group
>
...
...
src/client/src/auth/auth-context.js
View file @
08ce2115
import
React
from
'
react
'
import
React
,
{
useState
,
useContext
}
from
'
react
'
import
authHelpers
from
'
./auth-helpers
'
export
const
AuthContext
=
React
.
createContext
({
authState
:
false
,
isAuthed
:
()
=>
{}
})
\ No newline at end of file
const
AuthContext
=
React
.
createContext
()
export
const
AuthProvider
=
({
children
})
=>
{
const
[
authUser
,
setAuthUser
]
=
useState
(
authHelpers
.
isAuthenticated
())
return
(
<
AuthContext
.
Provider
value
=
{{
authUser
,
setAuthUser
}}
>
{
children
}
<
/AuthContext.Provider
>
)
}
export
const
useAuth
=
()
=>
useContext
(
AuthContext
)
\ No newline at end of file
src/client/src/core/Menu.jsx
View file @
08ce2115
import
React
,
{
useState
,
useEffect
,
useContext
}
from
"
react
"
;
import
React
from
"
react
"
;
import
Navbar
from
"
react-bootstrap/Navbar
"
;
import
Nav
from
"
react-bootstrap/Nav
"
;
import
NavDropdown
from
"
react-bootstrap/NavDropdown
"
;
import
Form
from
"
react-bootstrap/Form
"
;
import
FormControl
from
"
react-bootstrap/FormControl
"
;
import
Button
from
"
react-bootstrap/Button
"
;
import
auth
from
"
../auth/auth-helpers
"
;
import
{
Link
,
useHistory
}
from
"
react-router-dom
"
;
import
{
AuthContext
}
from
"
../auth/auth-context
"
;
// import { Link } from "react-router-dom";
import
{
useAuth
}
from
"
../auth/auth-context
"
;
function
Menu
()
{
const
auth
Context
=
useContext
(
AuthContext
);
const
{
auth
User
,
setAuthUser
}
=
useAuth
()
const
history
=
useHistory
();
console
.
log
(
"
Menu.jsx
"
,
history
);
console
.
log
(
"
authContext in Menu.jsx:
"
,
authContext
);
return
(
<
Navbar
sticky
=
"top"
bg
=
"dark"
variant
=
"dark"
expand
=
"sm"
>
<
Navbar
.
Brand
href
=
"/"
>
...
...
@@ -40,12 +33,12 @@ function Menu() {
</
NavDropdown
.
Item
>
</
NavDropdown
>
</
Nav
>
{
auth
Context
.
authState
?
(
{
auth
User
?
(
<
Button
onClick
=
{
()
=>
auth
.
clearJwt
(()
=>
{
history
.
push
(
"
/
"
);
authContext
.
setAuth
State
(
false
);
setAuth
User
(
false
);
})
}
>
...
...
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