- );
-};
+ )
+}
diff --git a/examples/auth/nextjs/pages/_app.tsx b/examples/auth/nextjs/pages/_app.tsx
index 5e7f0c6ded..0b61139c4a 100644
--- a/examples/auth/nextjs/pages/_app.tsx
+++ b/examples/auth/nextjs/pages/_app.tsx
@@ -7,7 +7,9 @@ import '../css/app.scss'
export default function MyApp({ Component, pageProps }: AppProps) {
return (
-
+ // The `AuthProvider` can be used with either REST or GraphQL APIs
+ // Just change the `api` prop to "graphql" or "rest", that's it!
+
diff --git a/examples/auth/nextjs/pages/account/index.module.css b/examples/auth/nextjs/pages/account/index.module.css
index 4c74f26f6a..9a5decbddb 100644
--- a/examples/auth/nextjs/pages/account/index.module.css
+++ b/examples/auth/nextjs/pages/account/index.module.css
@@ -3,8 +3,9 @@
}
.success,
-.error {
- margin-bottom: 15px;
+.error,
+.message {
+ margin-bottom: 30px;
}
.success {
@@ -13,4 +14,4 @@
.error {
color: red;
-}
\ No newline at end of file
+}
diff --git a/examples/auth/nextjs/pages/account/index.tsx b/examples/auth/nextjs/pages/account/index.tsx
index e019d08ce2..b2678c71b2 100644
--- a/examples/auth/nextjs/pages/account/index.tsx
+++ b/examples/auth/nextjs/pages/account/index.tsx
@@ -83,6 +83,7 @@ const Account: React.FC = () => {
return (
Account
+ {router.query.message &&
{router.query.message}
}
{error &&
{error}
}
{success &&
{success}
}
{'Visit the '}
Login
{' page to start the authentication flow. Once logged in, you will be redirected to the '}
Account
- {" page which is restricted to user's only."}
+ {` page which is restricted to user's only. To toggle APIs, simply toggle the "api" prop in _app.tsx between "rest" and "gql".`}
)
diff --git a/examples/auth/nextjs/pages/login/index.tsx b/examples/auth/nextjs/pages/login/index.tsx
index d42153ca02..dec13ecdd7 100644
--- a/examples/auth/nextjs/pages/login/index.tsx
+++ b/examples/auth/nextjs/pages/login/index.tsx
@@ -29,8 +29,8 @@ const Login: React.FC = () => {
try {
await login(data)
router.push('/account')
- } catch (_) {
- setError('There was an error with the credentials provided. Please try again.')
+ } catch (err) {
+ setError(err?.message || 'An error occurred while attempting to login.')
}
},
[login, router],
diff --git a/examples/auth/nextjs/pages/logout/index.module.css b/examples/auth/nextjs/pages/logout/index.module.css
index b4fefe9e4c..7d2f0a0af1 100644
--- a/examples/auth/nextjs/pages/logout/index.module.css
+++ b/examples/auth/nextjs/pages/logout/index.module.css
@@ -1,4 +1,4 @@
.error {
color: red;
- margin-bottom: 15px;
-}
\ No newline at end of file
+ margin-bottom: 30px;
+}
diff --git a/examples/auth/nextjs/pages/logout/index.tsx b/examples/auth/nextjs/pages/logout/index.tsx
index 2f7b5ac10a..bebbef3435 100644
--- a/examples/auth/nextjs/pages/logout/index.tsx
+++ b/examples/auth/nextjs/pages/logout/index.tsx
@@ -15,8 +15,8 @@ const Logout: React.FC = () => {
try {
await logout()
setSuccess('Logged out successfully.')
- } catch (_) {
- setError('You are already logged out.')
+ } catch (err) {
+ setError(err?.message || 'An error occurred while attempting to logout.')
}
}
diff --git a/examples/auth/nextjs/pages/recover-password/index.module.css b/examples/auth/nextjs/pages/recover-password/index.module.css
index b4fefe9e4c..7d2f0a0af1 100644
--- a/examples/auth/nextjs/pages/recover-password/index.module.css
+++ b/examples/auth/nextjs/pages/recover-password/index.module.css
@@ -1,4 +1,4 @@
.error {
color: red;
- margin-bottom: 15px;
-}
\ No newline at end of file
+ margin-bottom: 30px;
+}
diff --git a/examples/auth/nextjs/pages/recover-password/index.tsx b/examples/auth/nextjs/pages/recover-password/index.tsx
index c254d45589..fac6f3099b 100644
--- a/examples/auth/nextjs/pages/recover-password/index.tsx
+++ b/examples/auth/nextjs/pages/recover-password/index.tsx
@@ -1,6 +1,7 @@
import React, { useCallback, useState } from 'react'
import { useForm } from 'react-hook-form'
+import { useAuth } from '../../components/Auth'
import { Gutter } from '../../components/Gutter'
import { Input } from '../../components/Input'
import classes from './index.module.css'
@@ -12,6 +13,7 @@ type FormData = {
const RecoverPassword: React.FC = () => {
const [error, setError] = useState('')
const [success, setSuccess] = useState(false)
+ const { forgotPassword } = useAuth()
const {
register,
@@ -19,27 +21,21 @@ const RecoverPassword: React.FC = () => {
formState: { errors },
} = useForm()
- const onSubmit = useCallback(async (data: FormData) => {
- const response = await fetch(`${process.env.NEXT_PUBLIC_CMS_URL}/api/users/forgot-password`, {
- method: 'POST',
- body: JSON.stringify(data),
- headers: {
- 'Content-Type': 'application/json',
- },
- })
+ const onSubmit = useCallback(
+ async (data: FormData) => {
+ try {
+ const user = await forgotPassword(data as Parameters[0])
- if (response.ok) {
- // Set success message for user
- setSuccess(true)
-
- // Clear any existing errors
- setError('')
- } else {
- setError(
- 'There was a problem while attempting to send you a password reset email. Please try again later.',
- )
- }
- }, [])
+ if (user) {
+ setSuccess(true)
+ setError('')
+ }
+ } catch (err) {
+ setError(err?.message || 'An error occurred while attempting to recover password.')
+ }
+ },
+ [forgotPassword],
+ )
return (
diff --git a/examples/auth/nextjs/pages/reset-password/index.module.css b/examples/auth/nextjs/pages/reset-password/index.module.css
index e69de29bb2..7d2f0a0af1 100644
--- a/examples/auth/nextjs/pages/reset-password/index.module.css
+++ b/examples/auth/nextjs/pages/reset-password/index.module.css
@@ -0,0 +1,4 @@
+.error {
+ color: red;
+ margin-bottom: 30px;
+}
diff --git a/examples/auth/nextjs/pages/reset-password/index.tsx b/examples/auth/nextjs/pages/reset-password/index.tsx
index b3c40bb52e..1030e5bb53 100644
--- a/examples/auth/nextjs/pages/reset-password/index.tsx
+++ b/examples/auth/nextjs/pages/reset-password/index.tsx
@@ -14,7 +14,7 @@ type FormData = {
const ResetPassword: React.FC = () => {
const [error, setError] = useState('')
- const { login } = useAuth()
+ const { login, resetPassword } = useAuth()
const router = useRouter()
const token = typeof router.query.token === 'string' ? router.query.token : undefined
@@ -28,31 +28,23 @@ const ResetPassword: React.FC = () => {
const onSubmit = useCallback(
async (data: FormData) => {
- const response = await fetch(`${process.env.NEXT_PUBLIC_CMS_URL}/api/users/reset-password`, {
- method: 'POST',
- body: JSON.stringify(data),
- headers: {
- 'Content-Type': 'application/json',
- },
- })
+ try {
+ const user = await resetPassword(data as Parameters[0])
- if (response.ok) {
- const json = await response.json()
-
- // Automatically log the user in after they successfully reset password
- await login({ email: json.user.email, password: data.password })
-
- // Redirect them to /account with success message in URL
- router.push('/account?success=Password reset successfully.')
- } else {
- setError('There was a problem while resetting your password. Please try again later.')
+ if (user) {
+ // Automatically log the user in after they successfully reset password
+ // Then redirect them to /account with success message in URL
+ await login({ email: user.email, password: data.password })
+ router.push('/account?success=Password reset successfully.')
+ }
+ } catch (err) {
+ setError(err?.message || 'An error occurred while attempting to reset password.')
}
},
- [router, login],
+ [router, login, resetPassword],
)
- // when NextJS populates token within router,
- // reset form with new token value
+ // When Next.js populates token within router, reset form with new token value
useEffect(() => {
reset({ token })
}, [reset, token])