Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Display error messages #426

Merged
merged 5 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import registerUser from '@/functions/wordpress/auth/registerUser'
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'

const LOGIN_ERRORS = {
INVALID_USERNAME: 'invalid_username',
INCORRECT_PASSWORD: 'incorrect_password',
INVALID_EMAIL: 'invalid_email'
}

const userFields = [
'accessToken',
'userId',
Expand Down Expand Up @@ -78,8 +84,23 @@ const providers = [
const {username, password} = credentials
const response = await loginUser(username, password)

let errorMessage
if (response.error) {
throw `/login?error=${response.errorMessage}`
switch (response.errorMessage) {
case LOGIN_ERRORS.INVALID_USERNAME:
errorMessage = 'Username does not exists in our records.'
break
case LOGIN_ERRORS.INCORRECT_PASSWORD:
errorMessage = 'Incorrect password.'
break
case LOGIN_ERRORS.INVALID_EMAIL:
errorMessage = 'Email address does not exists in our records.'
break
default:
errorMessage = 'Error occured during login. Please try again.'
break
}
throw new Error(errorMessage)
}

return createUserObj(response)
Expand All @@ -106,7 +127,7 @@ const providers = [
})

if (response.error) {
throw `/register?error=${response.errorMessage}`
throw new Error(response.errorMessage)
}

return createUserObj(response)
Expand Down
34 changes: 23 additions & 11 deletions pages/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getPostTypeStaticProps from '@/functions/wordpress/postTypes/getPostTypeS
import {signIn, useSession} from 'next-auth/client'
import Link from 'next/link'
import {useRouter} from 'next/router'
import React, {useEffect} from 'react'
import React, {useEffect, useState} from 'react'

/**
* Render the Login component.
Expand All @@ -16,6 +16,7 @@ import React, {useEffect} from 'react'
* @return {Element} The Login component.
*/
export default function Login() {
const [errorMessage, setErrorMessage] = useState('')
const [session] = useSession()
const router = useRouter()

Expand All @@ -26,10 +27,30 @@ export default function Login() {
}
})

/**
* Submit login form.
*
* @author WebDevStudios
* @param {object} values Field values to submit.
*/
async function submitForm(values) {
const {username, password} = values
const response = await signIn('wpLogin', {
username,
password,
redirect: false
})

if (response.error) {
setErrorMessage(response.error)
}
}

return (
<Layout>
<Container>
<RichText tag="h1">Login</RichText>
{!!errorMessage && <div>{errorMessage}</div>}
<Form
className="login-form"
id="login-form"
Expand All @@ -38,16 +59,7 @@ export default function Login() {
username: '',
password: ''
}}
onSubmit={async (values, {setSubmitting}) => {
const {username, password} = values
signIn('wpLogin', {
username,
password,
callbackUrl: '/profile'
})

setSubmitting(false)
}}
onSubmit={submitForm}
>
<Text id="username" label="Username" isRequired type="text" />
<Text id="password" label="Password" isRequired type="password" />
Expand Down
40 changes: 26 additions & 14 deletions pages/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Form from '@/components/molecules/Form'
import getPostTypeStaticProps from '@/functions/wordpress/postTypes/getPostTypeStaticProps'
import {signIn, useSession} from 'next-auth/client'
import {useRouter} from 'next/router'
import React, {useEffect} from 'react'
import React, {useEffect, useState} from 'react'

/**
* Render the Register component.
Expand All @@ -15,6 +15,7 @@ import React, {useEffect} from 'react'
* @return {Element} The Register component.
*/
export default function Register() {
const [errorMessage, setErrorMessage] = useState('')
const [session] = useSession()
const router = useRouter()

Expand All @@ -25,10 +26,33 @@ export default function Register() {
}
})

/**
* Submit registration form.
*
* @author WebDevStudios
* @param {object} values Field values to submit.
*/
async function submitForm(values) {
const {firstName, lastName, email, password, username} = values
const response = await signIn('wpRegister', {
firstName,
lastName,
email,
password,
username,
redirect: false
})

if (response.error) {
setErrorMessage(response.error)
}
}

return (
<Layout>
<Container>
<RichText tag="h1">Register</RichText>
{!!errorMessage && <div>{errorMessage}</div>}
<Form
className="registration-form"
id="registration-form"
Expand All @@ -40,19 +64,7 @@ export default function Register() {
password: '',
username: ''
}}
onSubmit={async (values, {setSubmitting}) => {
const {firstName, lastName, email, password, username} = values
signIn('wpRegister', {
firstName,
lastName,
email,
password,
username,
callbackUrl: '/profile'
})

setSubmitting(false)
}}
onSubmit={submitForm}
>
<Text id="firstName" label="First Name" isRequired type="text" />
<Text id="lastName" label="Last Name" isRequired type="text" />
Expand Down