-
Notifications
You must be signed in to change notification settings - Fork 152
[next]: Add Authentication POC #2173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,599
−131
Merged
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d20cb31
init preview example
ahuseyn 404576d
update gql client, add headers param
ahuseyn 2437c6a
update template-hierarchy, add id and asPreview
ahuseyn f66dcc0
add preview utility to @faustjs/nextjs package
ahuseyn ebe7a5d
initial
ahuseyn 01c7132
new templates
ahuseyn b5bf801
add homepage
ahuseyn fdb6f0c
style fixes
ahuseyn 79e2a37
update uri expression
ahuseyn 14be433
rename example name, add readme
ahuseyn d3003d4
init project
ahuseyn bb8966a
update templates, add data fetching
ahuseyn 5272a82
remove logs, add comments, minor fixes
ahuseyn 87a4605
add kitchen-sink example
ahuseyn 83e2f25
add seedNode to templateHierarchy
ahuseyn 6e6365e
add preview
ahuseyn d25ab18
add preview to sveltekit templateHierarchy
ahuseyn ec2a3c3
Merge branch 'next' into next-sveltekit-kitchen-sink
ahuseyn a93275c
add auth package and example usage
ahuseyn 86d68bb
Merge branch 'next' into next-auth-poc
ahuseyn 21f19de
add alternative preview with auth package, add login page, client upd…
ahuseyn 5feafd4
remove token handler
ahuseyn efcbfb6
Update examples/nextjs/kitchen-sink/src/components/Header.js
ahuseyn 676dfea
todo updates
ahuseyn b338d32
Update packages/nextjs/package.json
ahuseyn 74f9ea9
init: toolbar integration (#2202)
ahuseyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
58 changes: 34 additions & 24 deletions
58
examples/nextjs/kitchen-sink/src/components/BlogPostItem.js
This file contains hidden or 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,32 +1,42 @@ | ||
| import Link from "next/link"; | ||
| import Link from 'next/link'; | ||
|
|
||
| export function BlogPostItem({ post }) { | ||
| const { title, date, excerpt, uri, featuredImage } = post ?? {}; | ||
| const { title, date, excerpt, uri, featuredImage } = post ?? {}; | ||
|
|
||
| return ( | ||
| <article className='container max-w-4xl px-10 py-6 mx-auto rounded-lg shadow-sm bg-gray-50 mb-4'> | ||
| <time dateTime={date} className='text-sm text-gray-600'> | ||
| {new Date(date).toLocaleDateString("en-US", { | ||
| year: "numeric", | ||
| month: "long", | ||
| })} | ||
| </time> | ||
| return ( | ||
| <article className="container max-w-4xl px-10 py-6 mx-auto rounded-lg shadow-sm bg-gray-50 mb-4"> | ||
| <time | ||
| dateTime={date} | ||
| className="text-sm text-gray-600" | ||
| suppressHydrationWarning> | ||
| {new Date(date).toLocaleDateString('en-US', { | ||
| year: 'numeric', | ||
| month: 'long', | ||
| })} | ||
| </time> | ||
|
|
||
| {featuredImage && ( | ||
| <img src={featuredImage?.node?.sourceUrl} alt='' className='w-full h-48 object-cover rounded-t-lg mt-2 mb-4' /> | ||
| )} | ||
| {featuredImage && ( | ||
| <img | ||
| src={featuredImage?.node?.sourceUrl} | ||
| alt="" | ||
| className="w-full h-48 object-cover rounded-t-lg mt-2 mb-4" | ||
| /> | ||
| )} | ||
|
|
||
| <h2 className='mt-3'> | ||
| <Link href={uri} className='text-2xl font-bold hover:underline'> | ||
| {title} | ||
| </Link> | ||
| </h2> | ||
| <h2 className="mt-3"> | ||
| <Link href={uri} className="text-2xl font-bold hover:underline"> | ||
| {title} | ||
| </Link> | ||
| </h2> | ||
|
|
||
| <div className='mt-2 mb-4' dangerouslySetInnerHTML={{ __html: excerpt }} /> | ||
| <div | ||
| className="mt-2 mb-4" | ||
| dangerouslySetInnerHTML={{ __html: excerpt }} | ||
| /> | ||
|
|
||
| <Link href={uri} className='hover:underline text-orange-600 mt-4'> | ||
| Read more | ||
| </Link> | ||
| </article> | ||
| ); | ||
| <Link href={uri} className="hover:underline text-orange-600 mt-4"> | ||
| Read more | ||
| </Link> | ||
| </article> | ||
| ); | ||
| } | ||
This file contains hidden or 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,20 +1,99 @@ | ||
| /* eslint-disable @next/next/no-html-link-for-pages */ | ||
| import Link from 'next/link'; | ||
| import { useState } from 'react'; | ||
| import Login from './Login'; | ||
| import { useLogout, useUser } from '@faustjs/nextjs/pages'; | ||
| import { useRouter } from 'next/router'; | ||
|
|
||
| export default function Header() { | ||
| const { user, refetch, isAuthenticated } = useUser(); | ||
| const { logout } = useLogout(); | ||
| const [isLoginModalOpen, setIsLoginModalOpen] = useState(false); | ||
| const route = useRouter(); | ||
|
|
||
| const openLoginModal = () => setIsLoginModalOpen(true); | ||
| const closeLoginModal = () => setIsLoginModalOpen(false); | ||
|
|
||
| const onLoggedIn = () => { | ||
| closeLoginModal(); | ||
| refetch(); | ||
| }; | ||
|
|
||
| const logoutUser = () => { | ||
| logout({ | ||
| onSuccess: () => { | ||
| refetch(); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <header className="bg-gray-800 text-white py-4 px-8"> | ||
| <div className="flex justify-between items-center max-w-4xl mx-auto"> | ||
| <div className="text-3xl font-semibold"> | ||
| <Link href="/">Headless</Link> | ||
| <> | ||
| <header className="bg-gray-800 text-white py-4 px-8"> | ||
| <div className="flex justify-between items-center max-w-4xl mx-auto"> | ||
| <div className="text-3xl font-semibold"> | ||
| <Link href="/">Headless</Link> | ||
| </div> | ||
|
|
||
| <nav className="flex items-center gap-4"> | ||
| <Link href="/" className="text-lg hover:underline"> | ||
| Home | ||
| </Link> | ||
|
|
||
| {isAuthenticated ? ( | ||
| <div className="relative group"> | ||
| <button className="text-lg hover:bg-gray-700 px-3 py-2 rounded-md transition-colors duration-200 flex items-center space-x-1"> | ||
| <span> | ||
| Welcome, <strong>{user.name || user.username}</strong> | ||
ahuseyn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </span> | ||
| <svg | ||
| className="w-4 h-4" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| viewBox="0 0 24 24"> | ||
| <path | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| strokeWidth={2} | ||
| d="M19 9l-7 7-7-7" | ||
| /> | ||
| </svg> | ||
| </button> | ||
|
|
||
| <div className="absolute right-0 mt-2 w-48 bg-white rounded-md shadow-lg opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all duration-200 z-50"> | ||
| <div className="py-1"> | ||
| <button | ||
| onClick={logoutUser} | ||
| className="block w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 transition-colors duration-200"> | ||
| Logout | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) : route.asPath !== '/login' ? ( | ||
| <button | ||
| onClick={openLoginModal} | ||
| className="text-lg hover:underline cursor-pointer bg-transparent border-none text-white"> | ||
| Login | ||
| </button> | ||
| ) : null} | ||
| </nav> | ||
| </div> | ||
| </header> | ||
|
|
||
| <nav className="space-x-6"> | ||
| <Link href="/" className="text-lg hover:underline"> | ||
| Home | ||
| </Link> | ||
| </nav> | ||
| </div> | ||
| </header> | ||
| {isLoginModalOpen && ( | ||
| <div className="fixed inset-0 bg-gray-900/30 flex items-center justify-center z-50"> | ||
| <div className="bg-white rounded-lg p-8 max-w-md w-full mx-4 relative"> | ||
| <button | ||
| onClick={closeLoginModal} | ||
| className="absolute top-4 right-4 text-gray-500 hover:text-gray-700 text-xl font-bold"> | ||
| × | ||
| </button> | ||
| <h2 className="text-2xl font-bold mb-6 text-gray-800">Login</h2> | ||
|
|
||
| <Login closeModal={onLoggedIn} /> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
This file contains hidden or 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,64 @@ | ||
| import { useLogin } from '@faustjs/nextjs/pages'; | ||
| import { useState } from 'react'; | ||
|
|
||
| export default function Login({ closeModal = () => {}, onSuccess = () => {} }) { | ||
| const [usernameEmail, setUsernameEmail] = useState(''); | ||
| const [password, setPassword] = useState(''); | ||
|
|
||
| const { login, isLoading, error } = useLogin(); | ||
|
|
||
| const submitForm = (e) => { | ||
| e.preventDefault(); | ||
|
|
||
| login({ | ||
| input: { | ||
| credentials: { | ||
| password, | ||
| username: usernameEmail, | ||
| }, | ||
| }, | ||
| onSuccess: () => { | ||
| onSuccess(); | ||
| closeModal(); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <form onSubmit={submitForm} className="space-y-4"> | ||
| <div> | ||
| <input | ||
| type="text" | ||
| name="username" | ||
| placeholder="Username or Email" | ||
| value={usernameEmail} | ||
| onChange={(e) => setUsernameEmail(e.target.value)} | ||
| className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" | ||
| required | ||
| /> | ||
| </div> | ||
| <div> | ||
| <input | ||
| type="password" | ||
| name="password" | ||
| placeholder="Password" | ||
| value={password} | ||
| onChange={(e) => setPassword(e.target.value)} | ||
| className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" | ||
| required | ||
| /> | ||
| </div> | ||
| {error && ( | ||
| <div className="text-red-600 text-sm bg-red-50 border border-red-200 rounded-md p-2"> | ||
| {error.message || 'Login failed. Please try again.'} | ||
| </div> | ||
| )} | ||
| <button | ||
| type="submit" | ||
| disabled={isLoading} | ||
| className="w-full bg-blue-600 hover:bg-blue-700 disabled:bg-blue-400 disabled:cursor-not-allowed text-white font-medium py-2 px-4 rounded-md transition-colors duration-200"> | ||
| {isLoading ? 'Signing in...' : 'Sign In'} | ||
| </button> | ||
| </form> | ||
| ); | ||
| } |
This file contains hidden or 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,7 @@ | ||
| import { defaultIronOptions } from '@faustjs/auth'; | ||
|
|
||
| export const sessionConfig = { | ||
| ...defaultIronOptions, | ||
| password: process.env.SESSION_PASSWORD, | ||
| cookieName: 'faust-auth', | ||
| }; |
This file contains hidden or 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,52 @@ | ||
| import availableTemplates from '@/wp-templates'; | ||
| import availableQueries from '@/wp-templates/templateQueries'; | ||
| import { fetchTemplateQueries } from '@faustjs/data-fetching'; | ||
| import { uriToTemplate } from '@faustjs/nextjs/pages'; | ||
|
|
||
| // Resolves the WordPress route based on the identifier (slug or ID) and fetches the necessary data | ||
| // for the corresponding template. It returns the props needed for rendering the page. | ||
| // If the route or template is not found, it returns a 404 response. | ||
|
|
||
| export async function resolveWpRoute(identifier, isPreview, client) { | ||
| const uri = identifier ? `/${identifier.join('/')}/` : '/'; | ||
|
|
||
| const variables = isPreview | ||
| ? { | ||
| id: identifier?.[0], | ||
| asPreview: true, | ||
| } | ||
| : { uri }; | ||
|
|
||
| try { | ||
| const templateData = await uriToTemplate({ | ||
| ...variables, | ||
| availableTemplates: Object.keys(availableTemplates), | ||
| wordpressUrl: process.env.NEXT_PUBLIC_WORDPRESS_URL, | ||
| }); | ||
|
|
||
| if ( | ||
| !templateData?.template?.id || | ||
| templateData?.template?.id === '404 Not Found' | ||
| ) { | ||
| return { notFound: true }; | ||
| } | ||
|
|
||
| const queriesData = await fetchTemplateQueries({ | ||
| availableQueries, | ||
| templateData, | ||
| client, | ||
| locale: templateData?.seedNode?.locale, | ||
| }); | ||
|
|
||
| return { | ||
| props: { | ||
| uri, | ||
| templateData: JSON.parse(JSON.stringify(templateData)), | ||
| queriesData, | ||
| }, | ||
| }; | ||
| } catch (error) { | ||
| console.error('Error resolving template:', error); | ||
| return { notFound: true }; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.