forked from colbyfayock/my-posts-app
-
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.
updating to use serverless functions and identity
- Loading branch information
1 parent
b555cf6
commit e9d1877
Showing
10 changed files
with
265 additions
and
35 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
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,49 @@ | ||
import { createContext, useContext, useEffect, useState } from 'react'; | ||
|
||
import { auth, init, logIn as authLogIn, logOut as authLogOut } from '../lib/auth.js'; | ||
|
||
export const AuthContext = createContext(); | ||
|
||
export const AuthProvider = ({ children }) => { | ||
const [user, setUser] = useState(); | ||
|
||
useEffect(() => { | ||
init((user) => { | ||
setUser(user) | ||
}); | ||
|
||
auth.on('login', setUser); | ||
|
||
return () => { | ||
auth.off('login', setUser); | ||
} | ||
}, []); | ||
|
||
function logIn() { | ||
authLogIn((user) => { | ||
setUser(user) | ||
}) | ||
} | ||
|
||
function logOut() { | ||
authLogOut(() => { | ||
setUser(undefined) | ||
}) | ||
} | ||
|
||
const contextValue = { | ||
user, | ||
logIn, | ||
logOut | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider value={contextValue}> | ||
{ children } | ||
</AuthContext.Provider> | ||
) | ||
} | ||
|
||
export function useAuth() { | ||
return useContext(AuthContext); | ||
} |
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,27 @@ | ||
import netlifyIdentity from 'netlify-identity-widget'; | ||
|
||
export const auth = netlifyIdentity; | ||
|
||
export function init(callback) { | ||
netlifyIdentity.on('init', user => { | ||
callback(user); | ||
}); | ||
netlifyIdentity.init({ | ||
APIUrl: process.env.NEXT_PUBLIC_AUTH_ENDPOINT | ||
}); | ||
} | ||
|
||
export function logIn(callback) { | ||
netlifyIdentity.open(); | ||
netlifyIdentity.on('login', user => { | ||
callback(user); | ||
netlifyIdentity.close(); | ||
}); | ||
} | ||
|
||
export function logOut(callback) { | ||
netlifyIdentity.logout(); | ||
netlifyIdentity.on('logout', () => { | ||
callback(); | ||
}); | ||
} |
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,19 @@ | ||
import { auth } from './auth'; | ||
|
||
export async function getAllPosts() { | ||
const response = await fetch(`${process.env.NEXT_PUBLIC_API_ENDPOINT}/api/posts`); | ||
const { posts } = await response.json(); | ||
return posts; | ||
} | ||
|
||
export async function createPost(data) { | ||
const user = auth.currentUser(); | ||
|
||
await fetch(`${process.env.NEXT_PUBLIC_API_ENDPOINT}/api/posts`, { | ||
method: 'POST', | ||
body: JSON.stringify(data), | ||
headers: { | ||
Authorization: `Bearer ${user.token.access_token}` | ||
} | ||
}); | ||
} |
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,13 @@ | ||
import { AuthProvider } from '../hooks/useAuth'; | ||
|
||
import '../styles/globals.scss' | ||
|
||
function MyApp({ Component, pageProps }) { | ||
return <Component {...pageProps} /> | ||
return ( | ||
<AuthProvider> | ||
<Component {...pageProps} /> | ||
</AuthProvider> | ||
); | ||
} | ||
|
||
export default MyApp |
This file was deleted.
Oops, something went wrong.
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,76 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
|
||
export default async (req, res) => { | ||
|
||
console.log('req', req.method); | ||
|
||
if ( req.method === 'GET' ) { | ||
const response = await fetch(`https://api.airtable.com/v0/${process.env.AIRTABLE_BASE_ID}/Posts`, { | ||
headers: { | ||
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}` | ||
} | ||
}); | ||
|
||
const { records } = await response.json(); | ||
|
||
const posts = records.map(record => { | ||
return { | ||
id: record.id, | ||
...record.fields | ||
} | ||
}) | ||
|
||
res.status(200).json({ posts }) | ||
|
||
return; | ||
} | ||
|
||
if ( req.method === 'POST' ) { | ||
|
||
const { authorization } = req.headers; | ||
|
||
const auth = await fetch(`${process.env.NEXT_PUBLIC_AUTH_ENDPOINT}/user`, { | ||
headers: { | ||
Authorization: authorization | ||
} | ||
}); | ||
|
||
const authJson = await auth.json(); | ||
|
||
if ( !authJson.id ) { | ||
res.status(401).json({ | ||
error: 'Invalid token' | ||
}); | ||
return; | ||
} | ||
|
||
const { content } = JSON.parse(req.body); | ||
|
||
const data = { | ||
records: [ | ||
{ | ||
fields: { | ||
content, | ||
date: new Date().toISOString() | ||
} | ||
} | ||
] | ||
} | ||
|
||
const response = await fetch(`https://api.airtable.com/v0/${process.env.AIRTABLE_BASE_ID}/Posts`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(data) | ||
}); | ||
|
||
console.log('response', response); | ||
|
||
res.status(201).json({ response }) | ||
|
||
return; | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -7681,6 +7681,11 @@ nested-error-stacks@^2.0.0, nested-error-stacks@^2.1.0: | |
resolved "https://registry.yarnpkg.com/nested-error-stacks/-/nested-error-stacks-2.1.0.tgz#0fbdcf3e13fe4994781280524f8b96b0cdff9c61" | ||
integrity sha512-AO81vsIO1k1sM4Zrd6Hu7regmJN1NSiAja10gc4bX3F0wd+9rQmcuHQaHVQCYIEC8iFXnE+mavh23GOt7wBgug== | ||
|
||
netlify-identity-widget@^1.9.1: | ||
version "1.9.1" | ||
resolved "https://registry.yarnpkg.com/netlify-identity-widget/-/netlify-identity-widget-1.9.1.tgz#9e716c4b92b9f0cc041074eb86fc962f35295b46" | ||
integrity sha512-9oIWjwUSdRk3SkREcZNjZaVuDDx9T/wSIXZNQsQeY4qoXic/FiXVEGgu2RU3IuA4OI3L2652xY1o+PpS03Ugaw== | ||
|
||
[email protected]: | ||
version "10.0.7" | ||
resolved "https://registry.yarnpkg.com/next/-/next-10.0.7.tgz#442f8e1da7454de33b0bbcc1ce5684b923597ee6" | ||
|