Skip to content

Commit

Permalink
Auth Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
roshanrajeev committed May 1, 2021
1 parent 771ec06 commit 6724cea
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
5 changes: 3 additions & 2 deletions components/Header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Link from 'next/link'
import styles from './header.module.scss'
import { useAuth } from '../../contexts/authContext'
import { clientRedirect } from '../../lib/redirect'
import { isValidUser } from '../../lib/user'

const Header = () => {
const [user, setUser] = useAuth()
Expand All @@ -11,14 +12,14 @@ const Header = () => {
method: 'GET',
credentials: 'include'
})
setUser()
setUser({})
clientRedirect('/')
}

return(
<nav className={styles.navbar}>
<Link href="/"><a className={styles.navbarBrand}>Logo</a></Link>
{user ?
{isValidUser(user) ?
<ul className={styles.navbarNav}>
<li className={styles.navbarLink}><Link href="/feeds"><a>Home</a></Link></li>
<li className={styles.navbarLink}><Link href="/messaging"><a>Messaging</a></Link></li>
Expand Down
5 changes: 3 additions & 2 deletions contexts/authContext.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { useContext, useEffect, useState } from 'react'
import { isValidUser } from '../lib/user'

const AuthContext = React.createContext()

export const AuthProvider = ({children}) => {
const [user, setUser] = useState()
const [user, setUser] = useState({})

useEffect(() => {
if(!user){
if(!isValidUser(user)){
fetch(`${process.env.API}/profile`, {
method: 'GET',
credentials: 'include'
Expand Down
3 changes: 3 additions & 0 deletions lib/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isValidUser = (user) => {
return Object.keys(user).length != 0
}
31 changes: 29 additions & 2 deletions pages/feeds.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
import Layout from '../components/Layout/layout'
import SEO from '../components/seo'
import { useAuth } from '../contexts/authContext'
import {clientRedirect, serverRedirect} from '../lib/redirect'
import styles from '../styles/pages/feeds.module.scss'

const Feeds = ({feeds}) => {
const [user, setUser] = useAuth()

return(
<Layout>
<SEO title="Feeds | Macebook"/>
<h1>Feeds</h1>
{feeds.map((f, idx) => <div key={idx}>{f}</div>)}
<div className={`row ${styles.container}`}>

<div className="col-2">
<div className={styles.profile}>
<img src={user.picture}/>
<h3>{user.username}</h3>
<p>My Description</p>
</div>
</div>

<div className="col-8">
<div className={styles.feeds}>
{feeds.map((f, idx) => <div className={styles.feed} key={idx}>{f}</div>)}
</div>
</div>

<div className="col-2">
<div className={styles.ads}>
Ads
</div>
</div>

</div>
</Layout>
)
}
Expand Down Expand Up @@ -39,6 +64,8 @@ const handleFetch = async (ctx, route) => {
"feed 2",
"feed 3",
"feed 4",
"feed 5",
"feed 6",
]
}
}
Expand Down
3 changes: 2 additions & 1 deletion pages/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import SEO from '../components/seo'
import Layout from '../components/Layout/layout'
import {useAuth} from '../contexts/authContext'
import {clientRedirect, serverRedirect} from '../lib/redirect'
import { isValidUser } from '../lib/user'

const Login = () => {
const [username, updateUsername] = useState("charlotteli")
const [password, updatePassword] = useState("charlotteli")
const [user, setUser] = useAuth()

useEffect(() => {
if(user) clientRedirect('/feeds')
if(isValidUser(user)) clientRedirect('/feeds')
}, [user])

const handleClick = async (e) => {
Expand Down
30 changes: 30 additions & 0 deletions styles/pages/feeds.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.container{
margin-top: 1rem;
}

.profile{
border: 1px solid black;
min-height: 250px;
text-align: center;
padding: 1rem 0;

img{
display: block;
width: 50%;
border-radius: 50%;
margin: 0 auto;
}
}

.ads{
border: 1px solid black;
min-height: 250px;
}

.feed{
border: 1px solid black;
padding: 1rem 0.5rem;
&:not(:last-child){
margin-bottom: 1rem;
}
}

0 comments on commit 6724cea

Please sign in to comment.