Skip to content

Commit 86214db

Browse files
committed
1.5 Re-factor for good
1 parent cafee97 commit 86214db

6 files changed

Lines changed: 94 additions & 43 deletions

File tree

‎src/lib/auth/AuthContext.tsx‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { createContext, FunctionComponent, useState } from 'react'
2+
import { supabase } from '~/lib/supabase'
3+
import { useMessage } from '~/lib/message'
4+
import { SupabaseAuthPayload } from './auth.types'
5+
6+
export type AuthContextProps = {
7+
signUp: (payload: SupabaseAuthPayload) => void,
8+
signIn: (payload: SupabaseAuthPayload) => void,
9+
loading: boolean
10+
}
11+
12+
export const AuthContext = createContext<Partial<AuthContextProps>>({})
13+
14+
export const AuthProvider: FunctionComponent = ({
15+
children,
16+
}) => {
17+
const [ loading, setLoading ] = useState(false)
18+
const { handleMessage } = useMessage()
19+
20+
const signUp = async (payload: SupabaseAuthPayload) => {
21+
try {
22+
setLoading(true)
23+
const { error } = await supabase.auth.signUp(payload)
24+
if (error) {
25+
handleMessage({ message: error.message, type: 'error' })
26+
}
27+
else {
28+
handleMessage({ message: 'Signup successful. Please check your inbox for a confirmation email!', type: 'success' })
29+
}
30+
} catch (error) {
31+
handleMessage({ message: error.error_description || error, type: 'error' })
32+
} finally {
33+
setLoading(false)
34+
}
35+
}
36+
37+
const signIn = async (payload: SupabaseAuthPayload) => {
38+
try {
39+
setLoading(true)
40+
const { error } = await supabase.auth.signIn(payload)
41+
if (error) {
42+
handleMessage({ message: error.message, type: 'error' })
43+
} else {
44+
handleMessage({ message: 'Log in successful. I\'ll redirect you once I\'m done', type: 'success' })
45+
}
46+
} catch (error) {
47+
handleMessage({ message: error.error_description || error, type: 'error' })
48+
} finally {
49+
setLoading(false)
50+
}
51+
}
52+
53+
54+
return (<AuthContext.Provider value={{
55+
signUp,
56+
signIn,
57+
loading
58+
}}>
59+
{children}
60+
</AuthContext.Provider>
61+
)
62+
}

‎src/lib/auth/auth.types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export type SupabaseAuthPayload = {
3+
email: string,
4+
password: string
5+
}

‎src/lib/auth/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './auth.types'
2+
export * from './AuthContext'
3+
export * from './useAuth'

‎src/lib/auth/useAuth.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useContext } from 'react'
2+
import { AuthContext } from './AuthContext'
3+
4+
export const useAuth = () => {
5+
const context = useContext(AuthContext)
6+
7+
if (context === undefined) {
8+
throw new Error(
9+
'useAuth must be used within a AuthContext.Provider'
10+
)
11+
}
12+
13+
return context
14+
}

‎src/pages/_app.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DefaultSeo } from 'next-seo';
88

99
import SEO from '../../next-seo.config';
1010
import { MessageProvider } from '~/lib/message'
11+
import { AuthProvider } from '~/lib/auth'
1112

1213
function MyApp({ Component, pageProps }: AppProps) {
1314
const pageMeta = (Component as any)?.defaultProps?.meta || {}
@@ -19,7 +20,9 @@ function MyApp({ Component, pageProps }: AppProps) {
1920
</Head>
2021
<DefaultSeo {...pageSEO } />
2122
<MessageProvider>
22-
<Component {...pageProps} />
23+
<AuthProvider>
24+
<Component {...pageProps} />
25+
</AuthProvider>
2326
</MessageProvider>
2427
</React.Fragment>
2528
}

‎src/pages/index.tsx‎

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,34 @@ import { NextPage } from 'next'
33
import { FaLock } from 'react-icons/fa'
44
import { NextAppPageProps } from '~/types/app'
55
import Layout from '~/components/Layout'
6-
import { useMessage } from '~/lib/message'
7-
import { useFormFields } from '~/lib/utils'
8-
import { supabase } from '~/lib/supabase'
96
import Spinner from '~/components/Spinner'
10-
7+
import { useFormFields } from '~/lib/utils'
8+
import { useAuth } from '~/lib/auth'
119

1210

1311
type SignUpFieldProps = {
1412
email: string,
1513
password: string
1614
}
1715

18-
type SupabaseAuthPayload = SignUpFieldProps
19-
2016
const FORM_VALUES: SignUpFieldProps = {
2117
email: '',
2218
password: ''
2319
}
2420

2521
const IndexPage: NextPage<NextAppPageProps> = ({}) => {
26-
const [loading, setLoading] = useState(false)
27-
const [isSignIn, setIsSignIn] = useState(true)
28-
const { handleMessage } = useMessage()
22+
const [ isSignIn, setIsSignIn ] = useState(true)
23+
const { loading, signIn, signUp } = useAuth()
2924
// Now since we have our form ready, what we're gonna need for signing up our users
3025
// 1. let users provide email and password
31-
const [values, handleChange ] = useFormFields<SignUpFieldProps>(FORM_VALUES)
26+
const [ values, handleChange ] = useFormFields<SignUpFieldProps>(FORM_VALUES)
3227
// 2. send the provided details to Supabase
33-
const signUp = async (payload: SupabaseAuthPayload) => {
34-
try {
35-
setLoading(true)
36-
const { error } = await supabase.auth.signUp(payload)
37-
if (error) {
38-
handleMessage({ message: error.message, type: 'error' })
39-
}
40-
else {
41-
handleMessage({ message: 'Signup successful. Please check your inbox for a confirmation email!', type: 'success' })
42-
}
43-
} catch (error) {
44-
handleMessage({ message: error.error_description || error, type: 'error' })
45-
} finally {
46-
setLoading(false)
47-
}
48-
}
49-
50-
const signIn = async (payload: SupabaseAuthPayload) => {
51-
try {
52-
const { error } = await supabase.auth.signIn(payload)
53-
if (error) {
54-
handleMessage({ message: error.message, type: 'error' })
55-
} else {
56-
handleMessage({ message: 'Log in successful. I\'ll redirect you once I\'m done', type: 'success' })
57-
}
58-
} catch (error) {
59-
handleMessage({ message: error.error_description || error, type: 'error' })
60-
}
61-
}
62-
6328

6429
const handleSumbit = (event: React.FormEvent) => {
6530
event.preventDefault()
66-
isSignIn ? signIn(values) : signUp(values)
31+
isSignIn ? signIn(values) : signUp(values)
6732
}
6833

69-
7034
return (
7135
<Layout useBackdrop={true} usePadding={false}>
7236
<div className="h-screen flex flex-col justify-center items-center relative">

0 commit comments

Comments
 (0)