-
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.
add: Navbar, react-router-dom, AuthGuard and Login, Logout, User pages (
#16) * add: Navbar and AuthGuard * add: react router, login, logout and user pages, auth guard * update readme + minor changes * fix: lints
- Loading branch information
1 parent
e417b64
commit 6fbba74
Showing
24 changed files
with
288 additions
and
32 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.app-container { | ||
padding: 16px; | ||
width: 840px; | ||
max-width: 1040px; | ||
margin: 0 auto; | ||
|
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,28 @@ | ||
import { useNavigate } from 'react-router-dom'; | ||
import { ApiContext } from '../../providers'; | ||
import { PropsWithChildren, useContext, useEffect } from 'react'; | ||
|
||
export type AuthGuardProps = PropsWithChildren & { | ||
redirectTo?: string; | ||
fallback?: React.ReactNode; | ||
}; | ||
|
||
export const AuthGuard: React.FC<AuthGuardProps> = ({ | ||
children, | ||
redirectTo, | ||
fallback, | ||
}) => { | ||
const { api } = useContext(ApiContext); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (api !== null && api.getUser() === null && redirectTo !== undefined) | ||
navigate(redirectTo); | ||
}, [api, navigate, redirectTo]); | ||
|
||
if (api !== null && api.getUser() === null && fallback) return fallback; | ||
|
||
if (api !== null && api.getUser() === null) return null; | ||
|
||
return children; | ||
}; |
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,6 @@ | ||
.navbar { | ||
display: flex; | ||
gap: 16px; | ||
flex-direction: column; | ||
margin-bottom: 16px; | ||
} |
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 { Link } from 'react-router-dom'; | ||
import styles from './Navbar.module.scss'; | ||
import { AuthGuard } from '../AuthGuard/AuthGuard'; | ||
|
||
export const Navbar: React.FC = () => { | ||
return ( | ||
<nav className={styles.navbar}> | ||
<ul> | ||
<li> | ||
<Link to={'/'}>Home</Link> | ||
</li> | ||
<li> | ||
<AuthGuard fallback={<Link to={'/login'}>Login</Link>}> | ||
<Link to={'/user'}>User</Link> | ||
</AuthGuard> | ||
</li> | ||
<li> | ||
<AuthGuard> | ||
<Link to={'/logout'}>Logout</Link> | ||
</AuthGuard> | ||
</li> | ||
</ul> | ||
</nav> | ||
); | ||
}; |
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,3 @@ | ||
import { PropsWithChildren } from 'react'; | ||
|
||
export type NavbarProps = PropsWithChildren; |
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 +1 @@ | ||
export * from './Posts/Posts'; | ||
export * from '../pages/Posts/Posts'; |
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,3 @@ | ||
.login-page { | ||
margin: 0 auto; | ||
} |
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,77 @@ | ||
import styles from './Login.module.scss'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { ApiContext } from '../../providers'; | ||
import { LoginProps } from './types'; | ||
import { Button } from '@asdf/ui'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
export const Login: React.FC<LoginProps> = () => { | ||
const { api } = useContext(ApiContext); | ||
const navigate = useNavigate(); | ||
const [isAuthenticating, setIsAuthenticating] = useState(false); | ||
const [error, setError] = useState(''); | ||
|
||
useEffect(() => { | ||
if (api !== null && api.getUser() !== null) navigate('/'); | ||
}, [api, navigate]); | ||
|
||
if (api === null) return <p>Loading...</p>; | ||
|
||
return ( | ||
<div className={styles['login-page']}> | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
if (isAuthenticating || api === null) return; | ||
setIsAuthenticating(true); | ||
|
||
api | ||
.login({ | ||
username: e.currentTarget.username.value, | ||
password: e.currentTarget.password.value, | ||
}) | ||
.then((user) => { | ||
if (user !== null) navigate('/'); | ||
}) | ||
.catch((err) => { | ||
setError(err.message); | ||
}) | ||
.finally(() => { | ||
setIsAuthenticating(false); | ||
}); | ||
}} | ||
> | ||
<h2>Login</h2> | ||
<p> | ||
<pre style={{ padding: 0 }}> | ||
Username: kminchelle | ||
<br /> | ||
Password: 0lelplR | ||
</pre> | ||
</p> | ||
<label> | ||
Username | ||
<input | ||
type="text" | ||
name="username" | ||
style={{ display: 'block', width: '100%' }} | ||
defaultValue={'kminchelle'} | ||
/> | ||
</label> | ||
<label> | ||
Password | ||
<input | ||
type="password" | ||
name="password" | ||
style={{ display: 'block', width: '100%' }} | ||
defaultValue={'0lelplR'} | ||
/> | ||
</label> | ||
<Button type="submit" fullWidth disabled={isAuthenticating}> | ||
{isAuthenticating ? 'Logging in...' : 'Login'} | ||
</Button> | ||
{error && <p style={{ color: 'red' }}>{error}</p>} | ||
</form> | ||
</div> | ||
); | ||
}; |
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,3 @@ | ||
import { PropsWithChildren } from 'react'; | ||
|
||
export type LoginProps = PropsWithChildren; |
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,3 @@ | ||
.logout-page { | ||
margin: 0 auto; | ||
} |
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,23 @@ | ||
import styles from './Logout.module.scss'; | ||
import { useContext, useEffect } from 'react'; | ||
import { ApiContext } from '../../providers'; | ||
import { LogoutProps } from './types'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
export const Logout: React.FC<LogoutProps> = () => { | ||
const { api } = useContext(ApiContext); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
api?.logout(); | ||
navigate('/'); | ||
}, [api, navigate]); | ||
|
||
if (api === null) return <p>Loading...</p>; | ||
|
||
return ( | ||
<div className={styles['logout-page']}> | ||
<p>Logging out...</p> | ||
</div> | ||
); | ||
}; |
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,3 @@ | ||
import { PropsWithChildren } from 'react'; | ||
|
||
export type LogoutProps = PropsWithChildren; |
1 change: 0 additions & 1 deletion
1
...df/src/components/Posts/Posts.module.scss → apps/asdf/src/pages/Posts/Posts.module.scss
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,7 +1,6 @@ | ||
.posts-container { | ||
display: flex; | ||
gap: 16px; | ||
padding: 16px; | ||
flex-direction: column; | ||
|
||
> article { | ||
|
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
File renamed without changes.
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,3 @@ | ||
.user-page { | ||
margin: 0 auto; | ||
} |
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,33 @@ | ||
import styles from './User.module.scss'; | ||
import { useContext } from 'react'; | ||
import { ApiContext } from '../../providers'; | ||
import { UserProps } from './types'; | ||
|
||
export const User: React.FC<UserProps> = () => { | ||
const { api } = useContext(ApiContext); | ||
|
||
if (api === null) return <p>Loading...</p>; | ||
|
||
return ( | ||
<div className={styles['user-page']}> | ||
<pre | ||
style={{ | ||
wordWrap: 'break-word', | ||
// whiteSpace: 'break-spaces', | ||
wordBreak: 'break-all', | ||
lineHeight: '1.8', | ||
letterSpacing: '0.01rem', | ||
margin: 0, | ||
padding: 0, | ||
}} | ||
> | ||
<code> | ||
<br /> | ||
{JSON.stringify(api.getUser(), null, 4)} | ||
<br /> | ||
<br /> | ||
</code> | ||
</pre> | ||
</div> | ||
); | ||
}; |
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,3 @@ | ||
import { PropsWithChildren } from 'react'; | ||
|
||
export type UserProps = PropsWithChildren; |
Oops, something went wrong.