-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Jhonatan2022/soporte
Auth
- Loading branch information
Showing
8 changed files
with
205 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import { useAuth } from './auth'; | ||
import { Navigate } from 'react-router-dom'; | ||
|
||
function Login() { | ||
const auth = useAuth(); | ||
const [username, setUsername] = React.useState(''); | ||
|
||
const login = (e) => { | ||
e.preventDefault(); | ||
auth.login({ username }); | ||
}; | ||
|
||
if (auth.user) { | ||
return <Navigate to="/profile" /> | ||
} | ||
|
||
return ( | ||
<> | ||
<h1>Login</h1> | ||
|
||
<form onSubmit={login}> | ||
<label>Escribe tu nombre de usuario:</label> | ||
<input | ||
value={username} | ||
onChange={e => setUsername(e.target.value)} | ||
/> | ||
|
||
<button type="submit">Entrar</button> | ||
</form> | ||
</> | ||
); | ||
} | ||
|
||
export { Login }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { useAuth } from './auth'; | ||
|
||
function Logout() { | ||
const auth = useAuth(); | ||
|
||
const logout = (e) => { | ||
e.preventDefault(); | ||
auth.logout(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<h1>Logout</h1> | ||
|
||
<form onSubmit={logout}> | ||
<label>¿Segura de que quieras salir?</label> | ||
|
||
<button type="submit">Salir</button> | ||
</form> | ||
</> | ||
); | ||
} | ||
|
||
export { Logout }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
import { Navigate, useNavigate } from 'react-router-dom'; | ||
|
||
const AuthContext = React.createContext(); | ||
|
||
function AuthProvider({ children }) { | ||
const navigate = useNavigate(); | ||
const [user, setUser] = React.useState(null); | ||
|
||
const login = ({ username }) => { | ||
setUser({ username }); | ||
navigate('/profile'); | ||
}; | ||
|
||
const logout = () => { | ||
setUser(null); | ||
navigate('/'); | ||
}; | ||
|
||
const auth = { user, login, logout }; | ||
|
||
return ( | ||
<AuthContext.Provider value={auth}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
} | ||
|
||
function useAuth() { | ||
const auth = React.useContext(AuthContext); | ||
return auth; | ||
} | ||
|
||
function AuthRoute(props) { | ||
const auth = useAuth(); | ||
|
||
if (!auth.user) { | ||
return <Navigate to="/login" />; | ||
} | ||
|
||
return props.children; | ||
} | ||
|
||
export { | ||
AuthProvider, | ||
useAuth, | ||
AuthRoute | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import React from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
import { blogdata } from './blogData' | ||
import React from "react"; | ||
import { useNavigate, useParams } from "react-router-dom"; | ||
import { blogdata } from "./blogData"; | ||
|
||
function BlogPost() { | ||
const { slug } = useParams(); | ||
const navigate = useNavigate(); | ||
const { slug } = useParams(); | ||
|
||
const blogpost = blogdata.find(post => post.slug === slug); | ||
const blogpost = blogdata.find((post) => post.slug === slug); | ||
|
||
return ( | ||
<> | ||
<h2>{blogpost.title}</h2> | ||
<p>{blogpost.author}</p> | ||
<p>{blogpost.content}</p> | ||
</> | ||
); | ||
const returnBlogPage = () => { | ||
// navigate(-1); // Vuelve a la página anterior | ||
navigate("/blog"); | ||
} | ||
|
||
return ( | ||
<> | ||
<h2>{blogpost.title}</h2> | ||
<button onClick={returnBlogPage}>Volver</button> | ||
<p>{blogpost.author}</p> | ||
<p>{blogpost.content}</p> | ||
</> | ||
); | ||
} | ||
|
||
export { BlogPost }; | ||
export { BlogPost }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
import React from "react"; | ||
import { Link } from "react-router-dom"; | ||
import { Link, Outlet } from "react-router-dom"; | ||
import { blogdata } from "./blogData"; | ||
|
||
function BlogPage() { | ||
return ( | ||
<> | ||
<h1>BlogPage</h1> | ||
return ( | ||
<> | ||
<h1>BlogPage</h1> | ||
|
||
<ul> | ||
{blogdata.map((post) => ( | ||
<BlogLink post={post} /> | ||
))} | ||
</ul> | ||
</> | ||
); | ||
<Outlet /> | ||
<ul> | ||
{blogdata.map((post) => ( | ||
<BlogLink post={post} key={post.slug} /> | ||
))} | ||
</ul> | ||
</> | ||
); | ||
} | ||
|
||
function BlogLink({ post }) { | ||
return ( | ||
<li> | ||
<Link to={`/blog/${post.slug}`}>{post.title}</Link> | ||
</li> | ||
); | ||
return ( | ||
<li> | ||
<Link to={`/blog/${post.slug}`}>{post.title}</Link> | ||
</li> | ||
); | ||
} | ||
|
||
export { BlogPage }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters