Skip to content

Commit 4479c08

Browse files
committed
1.6 Redirect on Sign-in and Sign-out
1 parent fcca8d6 commit 4479c08

4 files changed

Lines changed: 79 additions & 7 deletions

File tree

‎src/config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const ROUTE_HOME = '/profile'
2+
export const ROUTE_AUTH = '/'

‎src/lib/auth/AuthContext.tsx‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
import { createContext, FunctionComponent, useState } from 'react'
1+
import { createContext, FunctionComponent, useState, useEffect } from 'react'
2+
import Router from 'next/router'
3+
import { User } from '@supabase/supabase-js'
24
import { supabase } from '~/lib/supabase'
35
import { useMessage } from '~/lib/message'
46
import { SupabaseAuthPayload } from './auth.types'
7+
import { ROUTE_HOME, ROUTE_AUTH } from '~/config'
58

69
export type AuthContextProps = {
10+
user: User,
711
signUp: (payload: SupabaseAuthPayload) => void,
812
signIn: (payload: SupabaseAuthPayload) => void,
9-
loading: boolean
13+
signOut: () => void,
14+
loggedIn: boolean,
15+
loading: boolean,
16+
userLoading: boolean
1017
}
1118

1219
export const AuthContext = createContext<Partial<AuthContextProps>>({})
1320

1421
export const AuthProvider: FunctionComponent = ({
1522
children,
1623
}) => {
24+
const [ user, setUser ] = useState<User>(null)
1725
const [ loading, setLoading ] = useState(false)
26+
const [ userLoading, setUserLoading ] = useState(true)
27+
const [ loggedIn, setLoggedin ] = useState(false)
1828
const { handleMessage } = useMessage()
1929

2030
const signUp = async (payload: SupabaseAuthPayload) => {
@@ -37,11 +47,11 @@ export const AuthProvider: FunctionComponent = ({
3747
const signIn = async (payload: SupabaseAuthPayload) => {
3848
try {
3949
setLoading(true)
40-
const { error } = await supabase.auth.signIn(payload)
50+
const { error, user } = await supabase.auth.signIn(payload)
4151
if (error) {
4252
handleMessage({ message: error.message, type: 'error' })
4353
} else {
44-
handleMessage({ message: 'Log in successful. I\'ll redirect you once I\'m done', type: 'success' })
54+
handleMessage({ message: `Welcome, ${user.email}`, type: 'success' })
4555
}
4656
} catch (error) {
4757
handleMessage({ message: error.error_description || error, type: 'error' })
@@ -50,11 +60,45 @@ export const AuthProvider: FunctionComponent = ({
5060
}
5161
}
5262

63+
const signOut = async () => await supabase.auth.signOut()
64+
65+
useEffect(() => {
66+
const user = supabase.auth.user()
67+
68+
if (user) {
69+
setUser(user)
70+
setUserLoading(false)
71+
setLoggedin(true)
72+
Router.push(ROUTE_HOME)
73+
}
74+
75+
const { data: authListener } = supabase.auth.onAuthStateChange(
76+
async (event, session) => {
77+
const user = session?.user! ?? null
78+
setUserLoading(false)
79+
if (user) {
80+
setLoggedin(true)
81+
Router.push(ROUTE_HOME)
82+
} else {
83+
Router.push(ROUTE_AUTH)
84+
}
85+
}
86+
)
87+
88+
return () => {
89+
authListener.unsubscribe()
90+
}
91+
}, [])
92+
5393

5494
return (<AuthContext.Provider value={{
95+
user,
5596
signUp,
5697
signIn,
57-
loading
98+
signOut,
99+
loggedIn,
100+
loading,
101+
userLoading
58102
}}>
59103
{children}
60104
</AuthContext.Provider>

‎src/pages/index.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react'
1+
import { useState, useEffect } from 'react'
22
import { NextPage } from 'next'
33
import { FaLock } from 'react-icons/fa'
44
import { NextAppPageProps } from '~/types/app'
@@ -20,7 +20,7 @@ const FORM_VALUES: SignUpFieldProps = {
2020

2121
const IndexPage: NextPage<NextAppPageProps> = ({}) => {
2222
const [ isSignIn, setIsSignIn ] = useState(true)
23-
const { loading, signIn, signUp } = useAuth()
23+
const { loading, signIn, signUp, user, loggedIn } = useAuth()
2424
// Now since we have our form ready, what we're gonna need for signing up our users
2525
// 1. let users provide email and password
2626
const [ values, handleChange, resetFormFields ] = useFormFields<SignUpFieldProps>(FORM_VALUES)

‎src/pages/profile.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Link from 'next/link'
2+
import { useAuth } from '~/lib/auth'
3+
import Layout from '~/components/Layout'
4+
import { SpinnerFullPage } from '~/components/Spinner'
5+
6+
const ProfilePage = ({}) => {
7+
const { user, loading, signOut } = useAuth()
8+
9+
if(loading) {
10+
return <SpinnerFullPage/>
11+
}
12+
13+
return (
14+
<Layout useBackdrop={false}>
15+
<div className="h-screen flex flex-col justify-center items-center relative">
16+
<h2 className="text-3xl my-4">Howdie, { user && user.email ? user.email : 'Explorer' }!</h2>
17+
{!user && <small>You've landed on a protected page. Please <Link href="/">log in</Link> to view the page's full content </small>}
18+
{user && <div>
19+
<button onClick={signOut} className="border bg-gray-500 border-gray-600 text-white px-3 py-2 rounded w-full text-center transition duration-150 shadow-lg">Sign Out</button>
20+
</div> }
21+
</div>
22+
</Layout>
23+
)
24+
}
25+
26+
export default ProfilePage

0 commit comments

Comments
 (0)