Skip to content

Commit

Permalink
Run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed Mar 26, 2020
1 parent 5146663 commit 89d9df9
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 202 deletions.
96 changes: 50 additions & 46 deletions examples/with-next-connect/assets/style.css
Original file line number Diff line number Diff line change
@@ -1,75 +1,79 @@
*, :after, :before {
box-sizing: border-box;
*,
:after,
:before {
box-sizing: border-box;
}

a {
text-decoration: none !important;
cursor: pointer;
color: #0070f3;
text-decoration: none !important;
cursor: pointer;
color: #0070f3;
}

a:hover {
color: #0366d6;
color: #0366d6;
}

body {
margin: 0;
padding: 0;
color: #111;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
margin: 0;
padding: 0;
color: #111;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
}

main {
padding: 1rem;
max-width: 1040px;
margin: 0 auto;
padding: 1rem;
max-width: 1040px;
margin: 0 auto;
}

label {
display: flex;
margin-bottom: 0.5rem;
align-items: center;
width: 100%;
display: flex;
margin-bottom: 0.5rem;
align-items: center;
width: 100%;
}

form {
margin-bottom: 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-bottom: 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

input,
textarea {
font-family: monospace;
flex: 1 1 0%;
margin-left: 0.5rem;
box-shadow: none;
width: 100%;
color: #000;
background-color: transparent;
border: 1px solid #d8d8d8;
border-radius: 5px;
outline: 0px;
padding: 10px 25px;
font-family: monospace;
flex: 1 1 0%;
margin-left: 0.5rem;
box-shadow: none;
width: 100%;
color: #000;
background-color: transparent;
border: 1px solid #d8d8d8;
border-radius: 5px;
outline: 0px;
padding: 10px 25px;
}

button {
display: block;
margin-bottom: 0.5rem;
color: #fff;
border-radius: 5px;
border: none;
background-color: #000;
cursor: pointer;
transition: all 0.2s ease 0s;
padding: 10px 25px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.12);
display: block;
margin-bottom: 0.5rem;
color: #fff;
border-radius: 5px;
border: none;
background-color: #000;
cursor: pointer;
transition: all 0.2s ease 0s;
padding: 10px 25px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.12);
}

button:hover,
button:active {
transform: translate3d(0px, -1px, 0px);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}
transform: translate3d(0px, -1px, 0px);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}
62 changes: 33 additions & 29 deletions examples/with-next-connect/components/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Link from 'next/link';
import { useUser } from '../lib/hooks';
import Link from 'next/link'
import { useUser } from '../lib/hooks'

function Navbar() {
const [user, { mutate }] = useUser();
async function handleLogout() {
await fetch('/api/logout');
mutate(null);
}
const [user, { mutate }] = useUser()
async function handleLogout() {
await fetch('/api/logout')
mutate(null)
}

return (
<>
<style jsx>{`
return (
<>
<style jsx>{`
nav {
max-width: 1040px;
margin: auto;
Expand All @@ -36,28 +36,32 @@ function Navbar() {
clear: both;
display: table;
}
`}</style>
<nav>
<Link href="/">
<a>Next.js CRUD</a>
</Link>
<div>
{!user ? (
<Link href="/login">
<a>Sign in</a>
`}</style>
<nav>
<Link href="/">
<a>Next.js CRUD</a>
</Link>
) : (
<div>
{!user ? (
<Link href="/login">
<a>Sign in</a>
</Link>
) : (
<a role="button" onClick={handleLogout}>
Logout
</a>
)}
</div>
</nav></>)
Logout
</a>
)}
</div>
</nav>
</>
)
}

export default function Layout({ children }) {
return <>
<Navbar />
<main>{children}</main>
return (
<>
<Navbar />
<main>{children}</main>
</>
}
)
}
16 changes: 8 additions & 8 deletions examples/with-next-connect/lib/hooks.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import useSWR from 'swr';
import useSWR from 'swr'

const fetcher = (url) => fetch(url).then((r) => r.json());
const fetcher = (url) => fetch(url).then((r) => r.json())

export function usePost() {
const { data, mutate } = useSWR('/api/posts', fetcher);
const posts = data?.posts;
return [posts, { mutate }];
const { data, mutate } = useSWR('/api/posts', fetcher)
const posts = data?.posts
return [posts, { mutate }]
}

export function useUser() {
const { data, mutate } = useSWR('/api/user', fetcher);
const user = data?.user;
return [user, { mutate }];
const { data, mutate } = useSWR('/api/user', fetcher)
const user = data?.user
return [user, { mutate }]
}
20 changes: 10 additions & 10 deletions examples/with-next-connect/lib/passport.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import passport from 'passport';
import LocalStrategy from 'passport-local';
import passport from 'passport'
import LocalStrategy from 'passport-local'

passport.serializeUser(function (user, done) {
// In practice, we do not serialize the whole user object but just the id
// done(null, user.id)
done(null, user);
});
done(null, user)
})

passport.deserializeUser(function (id, done) {
// In practice, id will just be an id. In this case, it is the whole user object as shown in passport.serializeUser
// It is to used to query the database
// db.findUserById(id).then((user) => {
// done(null, user);
// });
done(null, id);
});
done(null, id)
})

passport.use(
new LocalStrategy((username, password, done) => {
// Here you should lookup for the user in your DB and compare the password
if (password === 'hackme') {
// return the user if the password is correct
done(null, { username });
done(null, { username })
} else {
// return null if the password is incorrect
done(null, null);
done(null, null)
}
})
);
)

export default passport;
export default passport
10 changes: 5 additions & 5 deletions examples/with-next-connect/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cookieSession from 'cookie-session';
import nextConnect from 'next-connect';
import passport from '../lib/passport';
import cookieSession from 'cookie-session'
import nextConnect from 'next-connect'
import passport from '../lib/passport'

const auth = nextConnect()
.use(
Expand All @@ -10,6 +10,6 @@ const auth = nextConnect()
})
)
.use(passport.initialize())
.use(passport.session());
.use(passport.session())

export default auth;
export default auth
4 changes: 2 additions & 2 deletions examples/with-next-connect/pages/_app.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Layout from '../components/Layout';
import Layout from '../components/Layout'
import '../assets/style.css'

export default function MyApp({ Component, pageProps }) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
);
)
}
16 changes: 8 additions & 8 deletions examples/with-next-connect/pages/api/login.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import nextConnect from 'next-connect';
import auth from '../../middleware/auth';
import passport from '../../lib/passport';
import nextConnect from 'next-connect'
import auth from '../../middleware/auth'
import passport from '../../lib/passport'

const handler = nextConnect();
const handler = nextConnect()

handler.use(auth).post(passport.authenticate('local'), (req, res) => {
if (req.user) {
// For demo purpose only. See pages/api/posts/
req.session.posts = [];
req.session.posts = []
}
res.json({ user: req.user });
});
res.json({ user: req.user })
})

export default handler;
export default handler
14 changes: 7 additions & 7 deletions examples/with-next-connect/pages/api/logout.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import nextConnect from 'next-connect';
import auth from '../../middleware/auth';
import nextConnect from 'next-connect'
import auth from '../../middleware/auth'

const handler = nextConnect();
const handler = nextConnect()

handler.use(auth).get((req, res) => {
req.logOut();
res.status(204).end();
});
req.logOut()
res.status(204).end()
})

export default handler;
export default handler
Loading

0 comments on commit 89d9df9

Please sign in to comment.